In testng class, I have a constructor with following methods super(driver) - this calls parent class and passes driver object setup() - this call goes to parent class,where a new driver object is created
Eg code
public class LoginTestNG extends ProjectTestBase {
WebDriver driver;
static By locator = By.name("airline");
public LoginTestNG(WebDriver driver) {
super(driver);
System.out.println("I am in constructor");
setup();
}
@Test(dataProvider = "logn")
public static void login_with_valid_user_param(String uname, String passwd){
LoginActions.submit_login(uname,passwd);
}
@DataProvider(name = "logn")
public static Object[][] loginData(){
return new Object[][]{{"test","test"}};
}
}
Parent class code:
public class ProjectTestBase{
public LoginActions loginActions;
static LoginPage loginPage;
public static WebDriver driver;
public ProjectTestBase(WebDriver driver){
this.driver=driver;
}
public static void setup(){
driver = startBrowser("firefox");
createActions(driver);
createPages(driver);
launchURL("http://newtours.demoaut.com/index.php");
driver.manage().window().maximize();
}
public static void createActions(WebDriver driver){
loginActions = new com.newtours.actions.LoginActions(driver);
}
public static void createPages(WebDriver driver){
loginPage = new LoginPage(driver);
}
}
my question is:
1. This code works fine if i run the test in a java class
2. If I add the test to a testNG class it does not work, I mean, I am getting
null pointer exception because driver object is null. I think the constructor is not getting invoked, I need help to understand if the constructor works differently in a testng class.
Thanks sk
TestNG doesn't call your constructor with parameter WebDriver driver so it's null.
If you want to pass some arguments to your test you can use:
@Parameters http://testng.org/doc/documentation-main.html#parameters @DataProvider http://testng.org/doc/documentation-main.html#parameters-dataprovidersOr you can init your WebDriver in @BeforeSuite / @BeforeClass etc. methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With