【发布时间】:2017-01-10 15:46:40
【问题描述】:
在 Java 页面对象模型中创建步骤时实例化对象的最佳方法是什么?
有人知道 Cucumber 脚本是如何编译的吗?
我想如果一切都已构建并符合以下第 2 或第 3 选项可能是最好的方法。
如果只编译与正在执行的测试相关的步骤,那么我想它会是第一个。
我有以下例子:
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();
}
以上所有步骤(以及同一个 LoginSteps.java 类中的更多步骤)都以
开头new HomePage(driver).clickLoginButton();
这是最好的方法,还是创建单个实例更好?
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();
}
或者,在 LoginSteps 扩展的“baseSteps”类中实例化所有页面是否更有效?
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);
}
【问题讨论】:
标签: java automation cucumber pageobjects