简体   繁体   English

@beforetest testng 由于某种原因被忽略

[英]@beforetest testng its being ignored for some reason

I am running my Cucumber suite tests with TestNG (Selenium + Java) and getting java.lang.NullPointerException.我正在使用 TestNG(Selenium + Java)运行我的 Cucumber 套件测试并获得 java.lang.NullPointerException。

I realized the problem is that my @BeforeTest() is being ignored for some reason causing the NullPointer problem.我意识到问题是我的 @BeforeTest() 由于某种原因被忽略,导致 NullPointer 问题。

I am using the TestNG 7.0.0 (but tried to use latest Beta also).我正在使用 TestNG 7.0.0(但也尝试使用最新的 Beta)。

@BeforeTest() 
public void setUp() {
    driver = Web.createChrome(); // it call a method that has the Chromedriver
}

Web.java Web.java

public class Web {

public static WebDriver createChrome() {

    System.setProperty("webdriver.chrome.driver", webdriver_path);

    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get("http://the-internet.herokuapp.com/");

    return driver;
}
}

Output Output

java.lang.NullPointerException
at br.dsanders.steps.steps.accessing_the_Tnternet_herokuapp_com_website(steps.java:62)
at ?.Given accessing the Tnternet.herokuapp.com website(testing.feature:9)

Try like below:尝试如下:

public class steps {


    WebDriver driver = null;

    public steps() {

        this.driver=Web.createChrome();

    }

@BeforeMethod() 
public void setUp() {
    driver.get("http://the-internet.herokuapp.com/");
  }
}

Note ClassName here is steps if you have other class name then change the class name and constructor name.注意如果您有其他 class 名称,则此处的 ClassName 是steps ,然后更改 class 名称和构造函数名称。

Change the @BeforeTest to @BeforeMethod@BeforeTest更改为@BeforeMethod

Source:资源:

What is the difference between BeforeTest and BeforeMethod in TestNG TestNG中的BeforeTest和BeforeMethod有什么区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM