微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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#[email protected] 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#[email protected] 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#[email protected] 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()

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐