0

I am trying to run this code with TestNG but all it gives me is java.lang.NullPointerException error. Firefox does get opened but after that error comes up. What's going wrong. Please help! Also is it mandatory to run testNG with XML.

 public class GuestCheckout 
  {

        public WebDriver driver ;
        public String baseURL = "http://www.google.com";

    @BeforeTest
    public void setBaseURL(){
      WebDriver driver = new FirefoxDriver();
      driver.get(baseURL);
    }

    @Test(priority = 0)
    public void EmailPasswordEntry() {
        // Entering Email and Password values

        driver.findElement(By.id("loginRegister")).click();
        WebElement email = driver.findElement(By.id("head_username"));
        email.clear();
        email.sendKeys("[email protected]");

        WebElement password = driver.findElement(By.id("head_password"));
        password.clear();
        password.sendKeys("123456");

        driver.findElement(By.name("loginsubmit")).click();
    }

    @Test(priority = 1)
    void AssertionVerification() {
        // Verifying "My Account" text after login
        String bodyText = driver.findElement(By.xpath("//span[@id='loginHeader']/descendant::a[text()='My Account']")).getText();
        Assert.assertTrue(bodyText.contains("My Accountii"), "Not matched");
    }   
  }



Error:
--------

    FAILED: EmailPasswordEntry
    java.lang.NullPointerException
        at package_1.GuestCheckout.EmailPasswordEntry(GuestCheckout.java:31)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at `enter code here`org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

 and same for the second Test method
1
  • Please paste detailed error log. Commented Oct 31, 2015 at 13:51

2 Answers 2

2

Since you have instantiated WebDriver in local way and trying use that as a global instance so you are getting Null Pointer Exception. Do this in this way:

public WebDriver driver ;
    public String baseURL = "http://www.ggolge.com";

@BeforeTest
public void setBaseURL(){
driver = new FirefoxDriver(); // Now this is a global instance for this class
driver.get(baseURL);
}

@Test(priority = 0)
public void EmailPasswordEntry() {
    // Entering Email and Password values

    driver.findElement(By.id("loginRegister")).click();
    WebElement email = driver.findElement(By.id("head_username"));
    email.clear();
    email.sendKeys("[email protected]");

    WebElement password = driver.findElement(By.id("head_password"));
    password.clear();
    password.sendKeys("123456");

    driver.findElement(By.name("loginsubmit")).click();
}

@Test(priority = 1)
void AssertionVerification() {
    // Verifying "My Account" text after login
    String bodyText = driver.findElement(By.xpath("//span[@id='loginHeader']/descendant::a[text()='My Account']")).getText();
    Assert.assertTrue(bodyText.contains("My Accountii"), "Not matched");
}   

No it is not mandatory to run using TestNg xml, if you are using some intelligent IDE like Eclipse then you can run your tests without xml.

XMLs are beneficial when you have multiple classes to run all together as a suite.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot Priyanshu..that worked! As i am new to test Ng can you pls advice from where can I get start learning
0

since using webdriver... try using public static

public static webdriver driver

in each class files

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.