无法在TestNG中执行第二个@Test方法

时间:2018-08-08 14:48:49

标签: testng

以下代码片段无法点击method2()

public class project 
{
    static WebDriver driver;

    @Test()
    public void method1()
    {
    System.setProperty("webdriver.chrome.driver","C:\\Users\\52013597\\Desktop\\Selenium\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("url");
    }

    @Test()
    public void method2()
        {
            driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
            driver.findElement(By.xpath("//*[@id=\"login-panel\"]/div[2]/form/div[3]/input")).clear();
            driver.findElement(By.xpath("//*[@id=\"login-panel\"]/div[2]/form/div[3]/input")).sendKeys("TOYVA30U5VC");
        }
}
  

[RemoteTestNG]检测到TestNG版本6.14.2   在端口10902上启动ChromeDriver 2.38.551601(edb21f07fc70e9027c746edd3201443e011a61ed)   仅允许本地连接。   2018年8月8日8:13:37 PM org.openqa.selenium.remote.ProtocolHandshake createSession   INFO:检测到的方言:OSS   通过:method1   失败:method2   java.lang.NullPointerException       在Framework1.project.method2(project.java:24)       在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)       在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(未知源)       在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(未知源)       在java.base / java.lang.reflect.Method.invoke(未知来源)       在org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)       在org.testng.internal.Invoker.invokeMethod(Invoker.java:580)       在org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)       在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)       在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)       在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)       在org.testng.TestRunner.privateRun(TestRunner.java:648)       在org.testng.TestRunner.run(TestRunner.java:505)       在org.testng.SuiteRunner.runTest(SuiteRunner.java:455)       在org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)       在org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)       在org.testng.SuiteRunner.run(SuiteRunner.java:364)       在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)       在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)       在org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)       在org.testng.TestNG.runSuitesLocally(TestNG.java:1137)       在org.testng.TestNG.runSuites(TestNG.java:1049)       在org.testng.TestNG.run(TestNG.java:1017)       在org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)       在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)       在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

================================================     默认测试

运行测试:2,失败:1,跳过:0

================================================ 默认套件

运行的总测试次数:2,失败次数:1,跳过次数:0

2 个答案:

答案 0 :(得分:0)

您缺少driver对象,请添加到method2中:

  WebDriver driver = new ChromeDriver();

或将此语句从method1移到类级别。

答案 1 :(得分:0)

您已在多个位置定义了Webdriver,而必须在@Test方法中将对象值初始化为driver。

public class project 
{
static WebDriver driver;

@Test()
public void method1()
{
  System.setProperty("webdriver.chrome.driver","C:\\Users\\52013597\\Desktop\\Selenium\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("url");
}

@Test()
public void method2()
    {
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        driver.findElement(By.xpath("//*[@id=\"login-panel\"]/div[2]/form/div[3]/input")).clear();
        driver.findElement(By.xpath("//*[@id=\"login-panel\"]/div[2]/form/div[3]/input")).sendKeys("TOYVA30U5VC");
    }
}

或者,如果您想在另一个类中重用它,则可以在没有@Test注释的类级别或单独方法中定义它。