Page Factory trong Selenium là một khái niệm mô hình đối tượng trang sẵn có cho Selenium WebDriver và nó rất tối ưu.
Với sự trợ giúp của lớp PageFactory, chúng ta sử dụng chú thích @FindBy để tìm WebElement.
@FindBy có thể chấp nhận tagName, partialLinkText, name, linkText, id, css, className, xpath dưới dạng thuộc tính.
Nội dung chính
Ví dụ Page Factory trong Selenium
Hãy xem ví dụ tương tự như Page Object Model (POM) trong Selenium bằng cách sử dụng Page Factory:
File: Utils.java
package vn.kienthuclaptrinh.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class Utils { public static boolean waitForElementDisplay(WebDriver driver, By by, int waitInSecond) { for (int i = 0; i < waitInSecond / 2 + 1; i++) { try { if (driver.findElement(by).isDisplayed()) { return true; } Thread.sleep(2 * 1000); } catch (Exception e) { System.out.println("waiting element for display..."); } } return false; } }
File: FacebookLogin.java
package vn.kienthuclaptrinh.selenium; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class FacebookLogin { private WebDriver driver; @FindBy(id="email") private WebElement weEmail; @FindBy(id="pass") private WebElement wsPassword; @FindBy(id="loginbutton") private WebElement wsTtnLogin; public FacebookLogin(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } public FacebookHomePage login(String email, String password) { // enter email weEmail.sendKeys(email); // enter password wsPassword.sendKeys(password); // click login button wsTtnLogin.click(); // return home page return new FacebookHomePage(driver); } }
File: FacebookHomePage.java
package vn.kienthuclaptrinh.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.PageFactory; public class FacebookHomePage { private WebDriver driver; private By byUserNavigation = By.id("userNavigationLabel"); public FacebookHomePage(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); } public boolean isDisplayed() { if (Utils.waitForElementDisplay(driver, byUserNavigation, 10)) { return true; } else { return false; } } }
File: TestLoginFacebook.java
package vn.kienthuclaptrinh.selenium; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class TestLoginFacebook { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "D:\\SeleniumWebdriver\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); // Open website driver.get("https://facebook.com"); // Maximize the browser driver.manage().window().maximize(); // Login facebook FacebookLogin facebookLogin = new FacebookLogin(driver); FacebookHomePage homePage = facebookLogin.login("kienthuclaptrinh@gmail.com", "helloPW123"); if (homePage.isDisplayed()) { System.out.println("Login Successfully!"); } else { System.out.println("Login Fail!"); } // Quit driver driver.quit(); } }
Kết quả:
waiting element for display... waiting element for display... waiting element for display... waiting element for display... waiting element for display... waiting element for display... Login Fail!