运行test ng case时出现java.lang.NullPointerException错误

时间:2015-10-31 13:44:39

标签: selenium selenium-webdriver testng

我正在尝试使用TestNG运行此代码,但它给我的全部是java.lang.NullPointerException错误。 Firefox确实打开但在出现错误之后。出了什么问题。请帮忙! 还必须使用XML运行testNG。

 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("abcd@gmail.com");

        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

2 个答案:

答案 0 :(得分:2)

由于您已经以本地方式实例化WebDriver并尝试将其用作全局实例,因此您将获得Null指针异常。以这种方式这样做:

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("abcd@gmail.com");

    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");
}   

不是没有必要使用TestNg xml运行,如果您使用像Eclipse这样的智能IDE,那么您可以在没有xml的情况下运行测试。

当您有多个类作为套件一起运行时,XML是有益的。

答案 1 :(得分:0)

因为使用webdriver ...尝试使用public static

  

public static webdriver driver

每个类文件中的