0
package browserhandling;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class allwithxpath {
    public static WebDriver driver;
    @Test(priority=1)
    public void browserLaunch() throws InterruptedException{
        System.setProperty("webdriver.chrome.driver","f:\\chromedriver.exe");
        //Launch empty browser

        WebDriver driver=new ChromeDriver();

         driver.manage().window().maximize();

         //Open the bowser with respective Shopkio Url 
                driver.get("https://www.shopkio.com/");
                Thread.sleep(3000);

        //Click on Sarees link 
                WebElement ele=driver.findElement(By.id("clicked62"));
                JavascriptExecutor js=(JavascriptExecutor)driver;
                js.executeScript("arguments[0].click()", driver.findElement(By.xpath("//*[@id='clicked62']")));

        //Click on on Item
                driver.findElement(By.xpath("//*[@id='bestselling']/div/div/div/div[1]/div/div[1]/div/div[2]/a")).click();;

Thread.sleep(3000);

    }
    @Test(priority=2)
    public void addandcheckout() throws Exception{

        //Click on Add to Cart
                //driver.findElement(By.xpath("/html/body/section[2]/div[1]/div[1]/div/div/div[2]/div[6]/div[2]/div/a[1]")).click();
                JavascriptExecutor js4=(JavascriptExecutor)driver;
                js4.executeScript("arguments[0].click()", driver.findElement(By.xpath("//div[@class='mhide']")));
        //Click on Checkout
                JavascriptExecutor js1=(JavascriptExecutor)driver;
                js1.executeScript("arguments[0].click()", driver.findElement(By.xpath("//*[@id='shopping-data']/div[3]/div[2]/a")));

    }

    @Test(priority=3)
    public void guestlogindetails() throws InterruptedException{
    //Click on Guest Login

                driver.findElement(By.xpath("//*[@id='LoginDiv']/div[2]/a")).click();

        //Enter name in name field
                driver.findElement(By.xpath("//*[@id='first_name_guest']")).sendKeys("narasimha");

         //Enter phonenumber in ph.no field
                driver.findElement(By.xpath("//*[@id='mobile_no_guest']")).sendKeys("8688991234");

         //Enter Address in Address field
                driver.findElement(By.xpath("//*[@id='address_guest']")).sendKeys("hopefarm");

         //Enter zipcode in zipcode field
                driver.findElement(By.xpath("//*[@id='zip_code_guest']")).sendKeys("560066");

         //Select DropdownBox for State
                Select state=new Select(driver.findElement(By.xpath("//*[@id='state_id_guest']")));
                state.selectByVisibleText("Karnataka");
                Thread.sleep(3000);

         //Select Dropdownbox for city

                Select cityname=new Select(driver.findElement(By.xpath("//*[@id='city_id_guest']")));

                cityname.selectByVisibleText("Bengaluru");

         //Click on Submit Button      


                driver.findElement(By.xpath("//*[@id='SaveAddress']")).click();
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

         //Click on Next Button

                JavascriptExecutor js2=(JavascriptExecutor)driver;
                js2.executeScript("arguments[0].click()", driver.findElement(By.xpath("//*[@id='SaveAddress1']")));

    }

    @Test(priority=4)
    public void placeorder(){
         //Click on Place Order

                driver.findElement(By.xpath("//*[@id='cod']")).click();
        System.out.println("Order placed Successfuly");
        }
    }

Error as follows

PASSED: browserLaunch FAILED: addandcheckout java.lang.NullPointerException at browserhandling.allwithxpath.addandcheckout(allwithxpath.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: guestlogindetails java.lang.NullPointerException at browserhandling.allwithxpath.guestlogindetails(allwithxpath.java:57) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: placeorder java.lang.NullPointerException at browserhandling.allwithxpath.placeorder(allwithxpath.java:99) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

=============================================== Default test

Tests run: 4, Failures: 3, Skips: 0

=============================================== Default suite Total tests run: 4, Failures: 3, Skips: 0

1
  • 2
    Hi there. What's your actual question? You've posted the error, but without a question to answer.
    – dvniel
    Commented Sep 21, 2018 at 14:38

2 Answers 2

1

I can see one issue in your code and likely it is the root-cause of your problem.

  1. You have a static field driver
  2. You're trying to initialize (probably that static) driver in your first test. However you're creating an object WebDriver driver=new ChromeDriver();that is only locally-visible in your first test. Thus your static driver field remains uninitialized
  3. You refer to that static driver field in your other tests.

So here are few points I'd like to adivce

  1. It is worth reading a book or pass a course on Java before starting to implement auto-tests. This time you get the exception because Java cares about you but the next time you will get no errors but just a false-positive/false negative result
  2. Do not make your tests dependent on each other. Rather set up your driver in preconditions.
0

In addition to what Alexey R. mentioned, do you also need a JavascriptExecutor? you can easily click on links, e.g.:

driver.findElement(By.id("clicked62")).click();

But mainly, since you are already using testNg, it would be better to initialize the browser (driver = new ChromeDriver();) in a @Before method (you can call this as:

public void initBrowser(){} 

If you wish that way, the same driver will be initialized before each @Test.

And a sidenote: try to avoid using '*' in your xpath as the search scope in DOM is to wide. Its always preferrable to use a particular DOM element like //input[] or //div[].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.