ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-05-15 16:51:35  阅读:134  来源: 互联网

标签:username return Python driver self Selenium login 设计模式 page


  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.comclass 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.comclass 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.comclass 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()

 

标签:username,return,Python,driver,self,Selenium,login,设计模式,page
来源: https://blog.51cto.com/u_11959730/2778043

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有