问题
My automation framework is using selenium + TestNG + PageObject model.
Structure :
My Testng class / test case :
nullpointer error
How can i pass the driver instance into my page objects?
回答1:
I can see you are declaring a new instance of WebDriver inside the @BeforeTest
method. You need to use the WebDriver instance that you declared outside the @BeforeTest
i.e. you have already declared
static WebDriver driver;
Use the same driver
inside your @BeforeTest
. So inside the before method, instead of doing WebDriver driver = new FirefoxDriver();
write like driver = new FirefoxDriver();
Do same for other browser types (ie, safari, chrome).
And for you page object classes, you can do something as follows:
public class TaxPage {
public static WebDriver driver;
public TaxPage(WebDriver driver) {
this.driver = driver;
}
}
回答2:
Create a class like below and pass WebDriver in parametrize constructor and Call driver like Page.driver
whenever you need it
public class Page
{
public static WebDriver driver;
public Page(WebDriver driver)
{
Page.driver = driver;
}
}
Hope it will help you :)
来源:https://stackoverflow.com/questions/33100961/how-to-pass-the-same-driver-instance-in-page-object-model