当我使用"优先级"时,TESTNG程序中的问题标签

时间:2017-09-14 03:05:12

标签: java selenium-webdriver testng

代码:使用带有优先级标记的@Test方法进行编程。

package testng1;
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
    @Test 
    public class testng {
    public static WebDriver driver;
    public void verifytitle()
    {
        System.setProperty("webdriver.ie.driver", "C:\\Users\\rprem\\Downloads\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe");
        WebDriver driver = new InternetExplorerDriver();
        driver.navigate().to("https://google.com");
        String title = driver.getTitle();
        System.out.println(title);
        Assert.assertEquals(title, "Google");
    }
    @Test (priority = 1)
    public void verifytitle2()
    {
    driver.get("https://gmail.com");
    String titlee = driver.getTitle();
    Assert.assertEquals(titlee, "Gmail - Free Storage and Email from Google");
    }
}

当我运行程序时,我得到了#34; Java空指针异常"问题。根据逻辑方法将按字母顺序执行,当我放置优先级时,它们将以给定的顺序运行。但它并没有那样发生。

错误讯息:

Google
PASSED: verifytitle
FAILED: verifytitle2
java.lang.NullPointerException
    at testng1.testng.verifytitle2(testng.java:22)
    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:108)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)


===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

1 个答案:

答案 0 :(得分:1)

问题在于您的测试代码。

在方法verifytitle()中,请将WebDriver driver = new InternetExplorerDriver();更改为driver = new InternetExplorerDriver();

由于您在此方法中将InternetExplorerDriver实例分配给局部变量,因此它会影响类级WebDriver,因此当verifytitle2()尝试访问它时,它会触发NullPointerException

要解决此问题,我建议您将webdriver实例化逻辑提取到@BeforeClass方法。

@BeforeClass() 
public void beforeClass() {
    System.setProperty("webdriver.ie.driver", "C:\\Users\\rprem\\Downloads\\IEDriverServer_x64_3.4.0\\IEDriverServer.exe");
    driver = new InternetExplorerDriver();
}