TestNg 不是 运行 带有 'driver.find' 的 @test 注释,但是仅适用于 'syso-printing

TestNg is not running @test annotations with 'driver.find' however works with just 'syso-printing

我在使用 TestNg 启动新包时遇到问题。请注意,我已经简化了代码以试图找出我哪里出错了。在 @Test(priority=3) 我遇到了问题。它不允许我点击按钮。

我检查了编译器,运行 1.8 没问题。

我检查了我以前的项目,运行 很好,但看不出有什么不同。我还有我的依赖项,它们是 maven、selenium、testng,看起来不错。我导入了很好的库。

更重要的是 TestNg 在我过去的项目中表现出色,可能是一个月前在同一台计算机和所有东西上。

package com.Prod.dtx_project;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class DIF1v2 {

protected WebDriver driver;

@Test(priority=1)
public void initialization()
{
    // To set the path of the Chrome driver.
    System.setProperty("webdriver.chrome.driver", "C:\Program Files\Eclipes\ChromeDriver\chromedriver.exe");
}

@Test(priority=2)
public void OpenBrowserChrome ()
{
    // Create a new instance of the Chrome driver
    WebDriver driver = new ChromeDriver();
    //WebDriver driver = new InternetExplorerDriver();
    driver.get("https://testng.org/doc/index.html");
        // To maximize the browser
    driver.manage().window().maximize();
        // implicit wait
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        // Print a Log In message to the screen
    System.out.println("Successfully opened the website ");

    driver.findElement(By.xpath("//*[@id='topmenu']/table/tbody/tr[2]/td[1]/a")).click();

}

@Test(priority=3)    //IT FAILED HERE WHEN USING A NEW ANNOTATION// 
public void Issue ()
{
driver.findElement(By.xpath("/html/body/a[13]")).click();

}

@Test(priority=4)
public void LandingPage ()
{
    System.out.println("LandingPage-4");

}

@Test(priority=5)
private void publ() 
{
    System.out.println("publ-5");
}



}

这是我的 testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite">

  <test name="Regression Testing">
    <classes>
      <class name="com.Prod.dtx_project.DIF1v2"/>
    </classes>
  </test>   

</suite> 

此外,这是我在控制台中收到的错误消息 -

FAILED: Issue
java.lang.NullPointerException
at com.Prod.dtx_project.DIF1v2.Issue(DIF1v2.java:49)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
at org.testng.internal.TestMethodWorker.invokeTestMethods 
(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run     
(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
Default test
Tests run: 5, Failures: 1, Skips: 0
===============================================

我预计它会打开 url 并单击此页面上的 eclipse 按钮​​,但它没有。

请注意:当我在@Test(priority=2) 中移动 driver.get 和 driver.findElement 时,它就可以工作了。请看下面。但是,我如何使用这种布局 运行 我的 TestNG。为什么这行得通,但是当使用多个 @Test 注释时它会失败。

@Test(priority=2)
 public void OpenBrowserChrome ()
 {
        // Create a new instance of the Chrome driver
    WebDriver driver = new ChromeDriver();
        //WebDriver driver = new InternetExplorerDriver();
    driver.get("https://testng.org/doc/index.html");
        // To maximize the browser
    driver.manage().window().maximize();
        // implicit wait
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        // Print a Log In message to the screen
    System.out.println("Successfully opened the website ");


    driver.get("https://testng.org/doc/index.html");
    driver.findElement(By.xpath("//*[@id='topmenu']/table/tbody/tr[2]/td[1]/a")).click();



}

它在 driver.get 语句中给出空指针,因为您没有在 priority=3 测试中初始化 driver
每个测试的范围都不同,因此您需要在每个要使用它的测试中对其进行初始化。

在priority=2中,你已经使用driver = new ChromeDriver();初始化了它,所以同样你需要在priority=3测试中初始化它,然后你需要执行driver.get(url)就可以了。

你的测试 2 和测试 3 应该是这样的:

@Test(priority=2)
public void OpenBrowserChrome()
{
    WebDriver driver = new ChromeDriver();
    driver.get("https://testng.org/doc/index.html");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    System.out.println("Successfully opened the website ");
    driver.findElement(By.xpath("//[@id='topmenu']/table/tbody/tr[2]/td[1]/a")).click();
    driver.quit();

}

@Test(priority=3)
public void Issue() throws InterruptedException
{
    WebDriver driver = new ChromeDriver();
    driver.get("https://testng.org/doc/index.html");
    driver.findElement(By.xpath("//[@id='topmenu']/table/tbody/tr[2]/td[1]/a")).click();
    Thread.sleep(5000);
    driver.findElement(By.xpath("/html/body/a[13]")).click();
    driver.quit();

}

如果你想像 test3 一样连续点击两个元素,你需要创建一个不同于 test2 的驱动程序的单独实例,然后再次点击 url 然后你需要单击元素。在上面的场景中,你可以去掉test2,因为它目前并没有真正为你解决任何目的,你可以在test3中执行这两个操作。

为 systems.setProperty 添加 @BeforeClass 最终成功了。

在@Test(priority=2) 中,删除"WebDriver" 然后一切都会正常工作。我测试了。

在 "public class DIF1v2" 下,您有 "protected WebDriver driver"。这意味着 "driver" 是一个实例变量(在 class 中定义的变量)- 在 class 下声明的变量,在每个方法之外。在这种情况下,"driver" 的默认值为 null。

但是在@Test(priority=2)中,有"WebDriver driver = new ChromeDriver();"。您将 "driver" 声明为 "WebDriver"。这是另一个 "driver"。这个 "driver" 是局部变量。这个 "driver" 只属于@Test(priority=2)。它的值只影响内部@Test(priority=2).

@Test(priority=3)中的错误"java.lang.NullPointerException"是由driver = null引起的。您正在调用 null 的 findElement 方法。

对于方法之间的值保持不变,变量需要是实例变量。 让我们看一个测试。我添加了一个字符串变量 "test".


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class DIF1v2 {

protected WebDriver driver;
protected String test = "123"; //// THIS INSTANCE VARIABLE IS FOR TESTING.
@Test(priority=1)
public void initialization()
{
    // To set the path of the Chrome driver.
    System.setProperty("webdriver.chrome.driver", "C:\Program Files\Eclipes\ChromeDriver\chromedriver.exe");
}

@Test(priority=2)
public void OpenBrowserChrome ()
{
    String test = "456";       //// THIS IS ANOTHER "test". JUST THE SAME NAME.
    // Create a new instance of the Chrome driver
    driver = new ChromeDriver();
    //WebDriver driver = new InternetExplorerDriver();
    driver.get("https://testng.org/doc/index.html");
        // To maximize the browser
    driver.manage().window().maximize();
        // implicit wait
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        // Print a Log In message to the screen
    System.out.println("Successfully opened the website ");

    driver.findElement(By.xpath("//*[@id='topmenu']/table/tbody/tr[2]/td[1]/a")).click();

}

@Test(priority=3)    //IT FAILED HERE WHEN USING A NEW ANNOTATION// 
public void Issue ()
{
    System.out.println(test);  //// THIS WILL PRINT "123"
    driver.findElement(By.xpath("/html/body/a[13]")).click();

}

@Test(priority=4)
public void LandingPage ()
{
    System.out.println("LandingPage-4");

}

@Test(priority=5)
private void publ() 
{
    System.out.println("publ-5");
}



}

输出:

123
LandingPage-4
publ-5
PASSED: initialization
PASSED: OpenBrowserChrome
PASSED: Issue
PASSED: LandingPage
PASSED: publ

我初始化了实例变量:

protected String test = "123";

在@Test(priority=2)中,我初始化了另一个同名变量:

String test = "456";

然后,在@Test(priority=3)中,我有:

System.out.println(test);

打印结果为:

123