I am struggling with declaring a parameter at class level with testNG. I have a browser
parameter that works fine when declared at the method level.
Because I am mapping the test to a cucumber step definition and will be declaring a url
parameter at the method level, I want to take the browser parameter away from the method to the class (global) level. So, in the xml file, I moved the browser parameter from test level to the suite level like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="5">
<!-- Parametter moved to suite level -->
<parameter name="Browser" value="CHROME"/>
<parameter name="Browser" value="FF"/>
<test name="Chrome Test">
<classes>
<class name="tests.web.WebTest"/>
</classes>
</test>
<test name="Firefox Test">
<classes>
<class name="tests.web.WebTest"/>
</classes>
</test>
</suite>
Then in the test class, I removed the browser
parameter from the method and to the class
and declared browser
as public static
. Sadly, the browser could not be found at runtime, resulting in a NullPointerException
:
@Parameters("Browser") //parameter declared at class level
public class WebTest {
WebDriver driver = null;
BasePageWeb basePage;
public static String browser; //class variable
@BeforeClass
public void navigateToUrl() {
switch (browser) { //NullpointerException thrown here
case "CHROME":
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
break;
case "FF":
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
break;
default:
driver = null;
break;
}
driver.get("www.google.com");
}
How do I declare the browser
parameter successfully at class level?