What is the best approach to instantiating up objects on creating steps in a Java Page Object Model?
Does anyone know how Cucumber scripts are compiled?
I would imagine if everything is built and complied the 2nd or 3rd option below may be the best approach.
If only the steps related to the test under execution are compiled then I would imagine it would be the first.
I Have the following example:
private LoginPage loginPage = new LoginPage(driver);
@Given("^I have logged in as customer with two stored cards$")
public void iHaveLoggedInAsCustomerWithTwoStoredCards() throws Throwable {
new HomePage(driver).clickLoginButton();
loginPage.enterEmail("test@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
@Given("^I have logged in as customer with expired card$")
public void iHaveLoggedInAsCustomerWithExpiredCard() throws Throwable {
new HomePage(driver).clickLoginButton();
loginPage.enterEmail("test02@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
@Given("^the user logged in as customer with three stored cards$")
public void theUserLoggedInAsCustomerWithThreeStoredCards() throws Throwable {
new HomePage(driver).clickLoginButton();
loginPage.enterEmail("test03@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
All of the above steps (plus more in the same LoginSteps.java class) start with
new HomePage(driver).clickLoginButton();
Is this the best approach, or is it better to create a single instance?
private LoginPage loginPage = new LoginPage(driver);
private HomePage homePage = new HomePage(driver);
@Given("^I have logged in as customer with two stored cards$")
public void iHaveLoggedInAsCustomerWithTwoStoredCards() throws Throwable {
homePage.clickLoginButton();
loginPage.enterEmail("test@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
@Given("^I have logged in as customer with expired card$")
public void iHaveLoggedInAsCustomerWithExpiredCard() throws Throwable {
homePage.clickLoginButton();
loginPage.enterEmail("test02@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
@Given("^the user logged in as customer with three stored cards$")
public void theUserLoggedInAsCustomerWithThreeStoredCards() throws Throwable {
homePage.clickLoginButton();
loginPage.enterEmail("test03@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
OR is it even more efficient to have the instantiation all pages in a 'baseSteps' class which LoginSteps extends?
public class LoginSteps extends BaseSteps {
@Given("^I have logged in as customer with two stored cards$")
public void iHaveLoggedInAsCustomerWithTwoStoredCards() throws Throwable {
homePage.clickLoginButton();
loginPage.enterEmail("test@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
@Given("^I have logged in as customer with expired card$")
public void iHaveLoggedInAsCustomerWithExpiredCard() throws Throwable {
homePage.clickLoginButton();
loginPage.enterEmail("test02@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
@Given("^the user logged in as customer with three stored cards$")
public void theUserLoggedInAsCustomerWithThreeStoredCards() throws Throwable {
homePage.clickLoginButton();
loginPage.enterEmail("test03@test.com");
loginPage.enterPassword("Password1");
loginPage.clickLogin();
}
}
public baseSteps {
protected LoginPage loginPage = new LoginPage(driver);
protected HomePage homePage = new HomePage(driver);
}