• Python Selenium自动化测试PO设计模式实战


      Page Object设计模式是自动化测试非常重要的一环,很多新入门自动化测试难以理解,先来看官网对PO说明:

    1、设计原则

    • The public methods represent the services that the page offers
    • Try not to expose the internals of the page
    • Generally don't make assertions
    • Methods return other PageObjects
    • Need not represent an entire page
    • Different results for the same action are modelled as different methods

      翻译如下:

      公共方法表示页面提供的服务

      不要暴露页面的细节

      Page设计中不要出现断言,应该写在测试用例类中

      方法应该返回其它的Page对象

      不要去代表整个page

      不同的结果返回不同的方法,不同的模式

    从字面理解还是比较困难,我们继续看官网例子,官网例子为java

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

    以上学习依然费劲,下面我们以登录为例,用PageObject模式开发脚本:

    login.py  登录类Page

    #_*_coding:utf-8_*_
    #大牛测试
    #qq:2574674466
    #2574674466@qq.com
    
    class loginPage(base):
      #对用户名元素封装
        def username(self):
            return self.by_id("nloginname")
        #对密码元素封装
        def passwd(self):
            return  self.by_id("npwd")
        #对登录按钮封装
        def btn(self):
            return  self.by_id("nsubmit")
        #对登录方法封装
        def LoginIn(self,username,passwd):
            self.username().send_keys(username)
            self.passwd().send_keys(passwd)
            self.btn().click()

    base.py  基类封装,

    #_*_coding:utf-8_*_
    #大牛测试
    #qq:2574674466
    #2574674466@qq.com
    
    class base():
        def __init__(self,driver):
            self.driver = driver
         #id定位
        def by_id(self,element):
            return  self.driver.find_element_by_id(element)
        #name定位
        def by_name(self,element):
            return self.driver.find_element_by_name(element)
        #xpath定位
        def by_xpath(self,element):
            return  self.driver.find_element_by_xpath(element)

    loginTest.py  登录测试类

    #_*_coding:utf-8_*_
    #大牛测试
    #qq:2574674466
    #2574674466@qq.com
    
    
    class logingTest(unittest.TestCase):
    
        def setUp(self):
          #初始化
            self.driver = webdriver.Chrome(path)
    
        def test_01(self):
            lo=loginPage(self.driver)
            l=lo.LoginIn("","")
    
        def tearDown(self):
          #执行完成后退出
           self.driver.quit()
    
    
    if __name__ == '__main__':
         unittest.main()
    欢迎加入交流群:Selenium学习群: 628908894
  • 相关阅读:
    Spark介绍与环境搭建
    Kafka基本操作
    Hadoop的HDFS概述
    hadoop环境搭建
    常用小工具
    mac机
    Eclipse使用
    微信公众号开发
    PM2
    JS 零散知识点
  • 原文地址:https://www.cnblogs.com/tim2016/p/14682741.html
Copyright © 2020-2023  润新知