I'm practicing some automation with Java + Cucumber + TestNG. I programmed a initialSetup class with all the of the WebDriverManager basic configuration I want to use. If I run a Cucumber Feature directly from the Feature File it works fine, but if I run the feature from the TestRunner class (TestNG) it fails, returning NullPointerException and saying that the driver equals null.
What could be wrong or what could I do?
initialSetup class where the WebDriverManager is set up:
package setup;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import io.github.bonigarcia.wdm.WebDriverManager;
public class initialSetup {
private static WebDriver driver;
@BeforeTest
public static void setupClass() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
setDriver(driver);
}
public static void setDriver(WebDriver driverValue) {
driver = driverValue;
}
public static WebDriver getDriver() {
return driver;
}
}
Here is my TestRunner class, in which the setup of TestNG i:
package Runner;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
//@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/features/",
glue= {"stepDefinition"},
plugin = { "pretty" },
monochrome = true
)
public class TestRunner extends AbstractTestNGCucumberTests {
}
And just in case is needed, here is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SeleniumPract</groupId>
<artifactId>SeleniumPract</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TestSelenium</name>
<dependencies>
<dependency>
<groupId> org.seleniumhq.selenium </groupId>
<artifactId> selenium-java </artifactId>
<version> 2.47.1 </version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.8.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>