zoukankan      html  css  js  c++  java
  • PageObject样例

    摘要

    • 公共方法表示页面提供的服务
    • 尽量不要暴露页面的内部
    • 一般不做断言
    • 方法返回其他PageObjects
    • 无需代表整个页面
    • 针对相同动作的不同结果被建模为不同方法
    public class LoginPage {
        private final WebDriver driver;
    
        public LoginPage(WebDriver driver) {
            this.driver = driver;
    
            // Check that we're on the right page.
            if (!"Login".equals(driver.getTitle())) {
                // Alternatively, we could navigate to the login page, perhaps logging out first
                throw new IllegalStateException("This is not the login page");
            }
        }
    
        // The login page contains several HTML elements that will be represented as WebElements.
        // The locators for these elements should only be defined once.
            By usernameLocator = By.id("username");
            By passwordLocator = By.id("passwd");
            By loginButtonLocator = By.id("login");
    
        // The login page allows the user to type their username into the username field
        public LoginPage typeUsername(String username) {
            // This is the only place that "knows" how to enter a username
            driver.findElement(usernameLocator).sendKeys(username);
    
            // Return the current page object as this action doesn't navigate to a page represented by another PageObject
            return this;    
        }
    
        // The login page allows the user to type their password into the password field
        public LoginPage typePassword(String password) {
            // This is the only place that "knows" how to enter a password
            driver.findElement(passwordLocator).sendKeys(password);
    
            // Return the current page object as this action doesn't navigate to a page represented by another PageObject
            return this;    
        }
    
        // The login page allows the user to submit the login form
        public HomePage submitLogin() {
            // This is the only place that submits the login form and expects the destination to be the home page.
            // A seperate method should be created for the instance of clicking login whilst expecting a login failure. 
            driver.findElement(loginButtonLocator).submit();
    
            // Return a new page object representing the destination. Should the login page ever
            // go somewhere else (for example, a legal disclaimer) then changing the method signature
            // for this method will mean that all tests that rely on this behaviour won't compile.
            return new HomePage(driver);    
        }
    
        // The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
        public LoginPage submitLoginExpectingFailure() {
            // This is the only place that submits the login form and expects the destination to be the login page due to login failure.
            driver.findElement(loginButtonLocator).submit();
    
            // Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials 
            // expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
            return new LoginPage(driver);    
        }
    
        // Conceptually, the login page offers the user the service of being able to "log into"
        // the application using a user name and password. 
        public HomePage loginAs(String username, String password) {
            // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
            typeUsername(username);
            typePassword(password);
            return submitLogin();
        }
    }

  • 相关阅读:
    [HAOI2008]硬币购物
    [SCOI2005]骑士精神
    [ZJOI2007]最大半联通子图
    [HAOI2007]反素数
    [SCOI2005]繁忙的都市
    小凯的疑惑
    5月16日vj题解
    周六题目前四题详解
    Codeforces Round #629 (Div. 3)做题记录
    Codeforces Round #570 (Div. 3) B. Equalize Prices
  • 原文地址:https://www.cnblogs.com/wldan/p/10533501.html
Copyright © 2011-2022 走看看