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();
        }
    }

  • 相关阅读:
    一.js高级(4)-函数调用-this指向-其他参数
    一.js高级(3)-原型及其继承
    一.js高级(2) -构造函数-原型对象
    curl ,post,get (原创)
    PDOHelper (原创)
    php 写日志函数(原创)
    一致性hash 算法 (转)
    md5 c# unicode 互换(原创)
    php auto_load mvc 接口框架(原创)
    php获取uniqid
  • 原文地址:https://www.cnblogs.com/wldan/p/10533501.html
Copyright © 2011-2022 走看看