When UI automated testing, we often choose PO setup mode, the PO design mode, we need to consider how elegant page elements and page functionality provided by the package today simply say both pages packaging method for reference purposes only.
- The first passed Page driver
Prior to this, UI test automation, has been using pass-way driver, but also a relatively simple method, the code is as follows:
1 /** 2 * @Author: zap 3 * @Date: 2020/3/27 23:58 4 * @Description: 5 */ 6 public class P0_LoginPage { 7 WebDriver driver; 8 public P0_LoginPage(WebDriver driver) { 9 this.driver = driver; 10 PageFactory.initElements(driver, this); 11 } 12 13 //username TextBox 14 @FindBy(xpath = "//*[@id='username']") 15 WebElement usernameTextBox; 16 17 /** 18 * @function Input username 19 * @param username 20 */ 21 public void setUsernameTextBox(String username) { 22 usernameTextBox.clear(); 23 usernameTextBox.sendKeys(username); 24 } 25 26 //password TextBox 27 @FindBy(xpath = "//*[@id='password']") 28 WebElement passwordTextBox; 29 30 /** 31 * @function Input password 32 * @param password 33 */ 34 public void setPasswordTextBox(String password) { 35 passwordTextBox.clear(); 36 passwordTextBox.sendKeys(password); 37 } 38 39 //login button 40 @FindBy(xpath = "//button[@class = 'ant-btn ant-btn-lg']") 41 WebElement loginButton; 42 43 /** 44 * @function click LoginButton 45 */ 46 public void clickLoginButton() { 47 loginButton.click(); 48 } 49 50 public HomePage toUserLogin(String user, String pwd){ 51 setUsernameTextBox(user); 52 setPasswordTextBox(pwd); 53 clickLoginButton(); 54 return new HomePage(); 55 } 56 }
- Page inherited second driver
BasePage create a class, as follows:
/** * @Author: zap * @Date: 2020/3/27 22:10 * @Description: */ public class BasePage { public static WebDriver driver; public void closeBrowser(){ driver.close(); } }
Then a new page to inherit BasePage, the parent class is defined by the driver in each page to use, inheritance follows:
/** * @Author: zap * @Date: 2020/3/27 22:12 * @Description: */ public class LoginPage extends BasePage{ /** * @Function: User Login * @Return : The return value needs to be considered successful landing and return of failed login different page * / public the HomePage toUserLogin (the User String, String pwd) { BaseURL String = "HTTP: // *********** / # /" ; // reference to the URL of address only System.setProperty("webdriver.chrome.driver", "E:\\Study\\Test\\SeleniumUIDependentFiles\\WebDriver\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.get (baseURL); // 1. Enter Username WebElement userTextBox = driver.findElement (By.xpath ( "* @ [@ ID = 'username']" )); userTextBox.clear(); userTextBox.sendKeys(user); // Thread.sleep(500); // 2. Enter the user's password WebElement pwdTextBox = driver.findElement (By.xpath ( "* @ [@ ID = 'password']" )); pwdTextBox.clear(); pwdTextBox.sendKeys(pwd); // 3. Click the Login button WebElement submitButton = driver.findElement (By.xpath ( "the Button // [@ class = 'Ant-btn btn-LG-Ant']" )); submitButton.click(); // The return results to determine which pages are not in-depth consideration here return new new the HomePage (); } }
When using the PO design patterns, we must understand clearly that only the page a good package, big head will not only implementation and maintenance of late; few understand presented here:
Method for the PO class:
1. The encapsulated UI function page can provide
[Understanding]: go to the Features for pages that can be provided, then package, such as landing page typically includes user login, password recovery operation, we can login, password recovery, respectively, as a method of packaging in LoginPage page, use the direct calls, the code is as follows (inherited driver mode):
/** * @Author: zap * @Date: 2020/3/27 22:11 * @Description: */ public class TestCase_UserLogin { LoginPage loginPage; @BeforeMethod(alwaysRun=true) public void beforeClass(){ loginPage = new LoginPage(); } @Test(alwaysRun=true) public void userLogin(){ loginPage.toUserLogin("test1", "test1"); } @AfterMethod public void afterClass(){ loginPage.closeBrowser(); } }
2. The same act different results, can be modeled in different ways
[Appreciated] In the same operation, due to inconsistencies in the input parameters, you may give different results and page jump; such code is as follows:
/** * @Author: zap * @Date: 2020/3/28 0:19 * @Description: */ public class P1_LoginPage { /** * @Function enter the login page * @return */ public LoginPage toLoginPageFail (){ /* Failed landing operation */ return new LoginPage(); } /** * @Function main page * @return */ public HomePage toLoginPageSuccess(){ /* Successful landing operation */ return new HomePage(); } }
3 .. The method should return for asserting or other data PageObject
[Understanding] where we try in a few cases after use, will experience to his advantage.