我正在尝试通过Selenium WebDriver自动化prestashop测试用例。我这样做是否正确,尤其是页面对象模型?
这是我的HomePagePOM课程:
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.Alert;
public class HomePagePOM {
private static WebElement element = null;
private static WebDriver driver = null;
public static String correctLogin = "aaaa";
public static void enterPage(WebDriver driver) {
driver.get("http://website.com");
}
public static WebElement homeLogo(WebDriver driver) {
element = driver.findElement(By.id("header_logo"));
return element;
}
public static WebElement searchBar(WebDriver driver) {
element = driver.findElement(By.id("search_query_top"));
return element;
}
public static WebElement searchBarButton(WebDriver driver) {
element = driver.findElement(By.name("submit_search"));
return element;
}
public static WebElement contactUs(WebDriver driver) {
element = driver.findElement(By.id("contact-link"));
return element;
}
public static WebElement signIn(WebDriver driver) {
element = driver.findElement(By.className("header_user_info"));
return element;
}
}
这是我的LoginPagePOM类:
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPagePOM {
private static WebElement element = null;
private static WebDriver driver = null;
public static String correctLogin = "client";
public static String incorrectLogin = "client_incorrect";
public static String correctPassword = "pass_correct";
public static String incorrectPassword = "pass_incorrect";
public static WebElement createAccountField(WebDriver driver) {
element = driver.findElement(By.id("email_create"));
return element;
}
public static WebElement registeredEmailField(WebDriver driver) {
element = driver.findElement(By.id("email"));
return element;
}
public static WebElement registeredPasswordField(WebDriver driver) {
element = driver.findElement(By.id("passwd"));
return element;
}
public static WebElement registeredSubmitButton(WebDriver driver) {
element = driver.findElement(By.id("SubmitLogin"));
return element;
}
public static WebElement createAccountButton(WebDriver driver) {
element = driver.findElement(By.id("SubmitCreate"));
return element;
}
}
这是我的LoginPageCorrectLogin
类,执行一个测试用例 - 正确登录prestashop网站:
package testCases;
import pageObjects.HomePagePOM;
import pageObjects.LoginPagePOM;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class LoginPageCorrectLogin {
public static WebDriver driver = null;
public static WebElement element = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
HomePagePOM.enterPage(driver); //Enter home site
HomePagePOM.signIn(driver).click(); //Find Sign In option and click
LoginPagePOM.registeredEmailField(driver).sendKeys(LoginPagePOM.correctLogin); // Input correct login to field
LoginPagePOM.registeredPasswordField(driver).sendKeys(LoginPagePOM.correctPassword); //Input correct password to field
LoginPagePOM.registeredSubmitButton(driver).click(); // Click on submit button
}
}
我是Selenium的佼佼者,但我想使用最佳实践。这是对的吗?对于此示例,我将创建其他类,如LoginPageCorrectPassword
,LoginPageIncorrectLogin
和LoginPageIncorrectPassword
有关页面模型对象的任何建议都会非常有用。
答案 0 :(得分:3)
由于这是一个开放式问题,这里有一些一般性的指示:
LoginPagePOM.login(username, password, isValidLogin)
通过这种方式,您无法在任何地方重复登录代码,更容易理解,只有当您的登录页面显示时,您才需要更改1个位置答案 1 :(得分:1)
编写对象时,尽可能多地封装所有元素,以便方法不依赖于访问原始元素。他们应该更多地采取行动。
这是我从上面的代码中看到的内容。
首先,我创建了一个浏览器对象来处理与浏览器相关的操作。浏览器特定的操作包括最大化,调整大小,清除cookie等。
public class Browser {
public static Browser newBrowser(WebDriver driver) {
return new Browser(driver);
}
private final WebDriver driver;
private Browser(WebDriver driver) {
this.driver = driver;
}
public HomePage gotoHomePage() {
driver.get("http://website.com");
return new HomePage(driver);
}
}
这是一个更简单的主页版本。您的方法应该描述一个动作。用这种方式阅读起来比较容易。
public class HomePage {
private final WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;
}
public LoginPage signIn() {
driver.findElement(By.className("header_user_info")).click();
return new LoginPage(driver);
}
}
接下来是登录页面。如果您注意到,我有两个版本的登录方法,用于成功和失败。也许它只是我,但当我做自动化时,我已经知道行动的结果和所有我正在做的是将其转化为自动化。因此,登录失败可能会转到另一个页面,成功登录可能会转到另一个页面。好吧,我们需要在两种情况下处理它们,以便我们的对象不会中断。要记住的最重要的事情是在测试用例中应该最小化条件(if-else)。
public class LoginPage {
public static String correctLogin = "client";
public static String incorrectLogin = "client_incorrect";
public static String correctPassword = "pass_correct";
public static String incorrectPassword = "pass_incorrect";
private final WebDriver driver;
public LoginPage(WebDriver driver) {
super();
this.driver = driver;
}
public FailurePage registerAndFail(String email, String password) {
driver.findElement(By.id("email")).sendKeys(email);
driver.findElement(By.id("passwd")).sendKeys(password);
driver.findElement(By.id("SubmitLogin")).submit();
return new FailurePage(driver);
}
public SuccessPage registerAndSucceed(String email, String password) {
driver.findElement(By.id("email")).sendKeys(email);
driver.findElement(By.id("passwd")).sendKeys(password);
driver.findElement(By.id("SubmitLogin")).submit();
return new SuccessPage(driver);
}
}
以下是你将如何编写测试。
public class LoginPageCorrectLogin {
public static void main(String[] args) {
Browser browser = Browser.newBrowser(new FirefoxDriver());
HomePage homepage = browser.gotoHomePage();
// assertions here
LoginPage loginPage = homepage.signIn();
//assertions here
SuccessPage sucessPage = loginPage.registerAndSucceed(LoginPage.correctLogin, LoginPage.correctPassword);
// assertions here
}
}