简体   繁体   中英

@beforetest testng its being ignored for some reason

I am running my Cucumber suite tests with TestNG (Selenium + Java) and getting java.lang.NullPointerException.

I realized the problem is that my @BeforeTest() is being ignored for some reason causing the NullPointer problem.

I am using the TestNG 7.0.0 (but tried to use latest Beta also).

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

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

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.

Change the @BeforeTest to @BeforeMethod

Source:

What is the difference between BeforeTest and BeforeMethod in TestNG

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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