2015-10-31 74 views
0

我想用TestNG运行这段代码,但它给我的只是java.lang.NullPointerException错误。 Firefox确实打开了,但之后出现错误。出了什么问题。请帮忙! 也是强制使用XML运行testNG。运行测试用例时发生java.lang.NullPointerException错误

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 
+0

,请复制粘贴详细的错误日志。 –

回答

2

既然你已经固化在当地webdriver的方式,并试图使用它作为一个全球性的实例,以便您获得空指针异常。以这种方式做到这一点:

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

不,它不是强制性的用TestNG XML,如果您使用的是一些智能IDE像Eclipse那么你就可以不用XML运行测试运行。

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

+0

非常感谢Priyanshu..that工作!由于我是新来测试吴你可以请求我从哪里可以得到开始学习的建议 –

+0

http://testng.org/doc/documentation-main.html学习testng的最佳去处。 –

+0

请您接受我的回答。 –

0

因为使用webdriver的...尝试使用公共静态

public static webdriver driver

每个类文件

相关问题