美文网首页
Python + Selenium PO 设计模式实践

Python + Selenium PO 设计模式实践

作者: 猫与测试 | 来源:发表于2019-11-21 00:55 被阅读0次

    前面讲了 PO 模式的思想,接下来我们讲一下常见的 PO 模式的实践。

    在 Python 领域(Java 中 Selenium 提供了 page factory)中,由于没有官方或者说非常标准的实践模式,再加之 Python 又是一种支持函数式编程的语言,所以对 PO 的实践有很多实现方式。

    目前我所知的有三种模式:

    • 以模块(页面文件)的方式收集页面元素定位和操作;
    • 以类为单位,在类中以类属性的形式只收集页面定位而不包含操作;
    • 以类为单位,包含页面元素定位和操作。

    以上三种方式都有实践,第一种方式没有使用面向对象并非真正意义上的 PO;第二种方式只提取了定位,并没有封装操作;第三种模式更接近真正意义上的 PO 模式。

    我们以前面讲过的登录为例,为了方便理解,我这里简化了步骤和代码。

    第一种方式:

    将所有的定位方式和操作都写在页面文件中,定位方式以常量(为阅读方便常量用了小写,这不太符合约定)的形式编写,操作以函数的形式编写:

    页面文件 login_page.py

    from selenium.webdriver.common.by import By
    
    # 定位方式
    username_input = By.NAME, 'username'
    password_input = By.NAME, 'password'
    login_button = By.NAME, 'submit'
    logout_link = By.LINK_TEXT, '退出'
    
    # 页面操作
    def login(driver, username, password):
        '''登录操作'''
        driver.find_element(*username_input).send_keys(username)
        driver.find_element(*password_input).send_keys(password)
        driver.find_element(*login_button).click()
    
    def logout(driver):
        '''登出操作'''
        driver.find_element(*logout_link )
    
    

    用例文件 test_login.py

    import unittest
    from selenium import webdriver
    from login_page import login
    
    class TestLogin(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
        
        def test_login(self):
            login(self.driver, 'rightuser','123456')
            self.assertEqual(login_text, '登录成功')
    
        def tearDown(self):
            self.driver.quit()
    

    这种方式,你不得不在函数参数中加入 driver 参数。如果多个页面涉及到类似的操作,你依然得每个页面都重复编写。

    其抽象程度不高,也就是面向过程级别的。相对于面向对象的难于理解,这种方式显得很直观,所以初学者选择这种方式也可以提高一定的可维护性。

    第二种方式:

    使用面向对象的方式,只是抽象的页面类中只包含元素定位。

    页面文件 login_page.py

    from selenium.webdriver.common.by import By
    
    # 定位方式
    class Login_Page:
        username_input = By.NAME, 'username'
        password_input = By.NAME, 'password'
        login_button = By.NAME, 'submit'
        logout_link = By.LINK_TEXT, '退出'
    

    用例文件 test_login.py

    import unittest
    from selenium import webdriver
    from login_page import Login_Page as lp
    
    class TestLogin(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
        
        def test_login(self):
            driver = self.driver
            driver.find_element(*lp.username_input).send_keys('rightuser')
            driver.find_element(*lp.password_input).send_keys('123456')
            driver.find_element(*lp.logout_link).click()
            self.assertEqual(login_text, '登录成功')
    
        def tearDown(self):
            self.driver.quit()
    

    这种方式虽然用了面向对象,其实只是用了类将定位方式集中到一起。如果页面的操作方式发生变化,也意味着测试用例的更改。

    同第二种方式一样,抽象程度不高,容易理解。问题依然是抽象程度不够,页面变化依然会引起测试代码的变化。

    第三种方式:

    第三种方式,相当于是前两种的集合,也是更符合 PO 模式的实践方式。通过将定位方式和操作全部封装,这样可以通过面向对象的特性很容易的进行更高层次的抽象和封装。

    页面文件 page.py

    可以在这里将每个页面都会涉及到的内容进行封装,比如等待、select 元素处理、重新定义 find_element 方法等。

    class Page:
        def __init__(self, driver):
            self.driver = driver
    
        def wait(self, locator, timeout=10):
            pass
        
        def select(self, locator, option):
            pass
            
        def find(self, locator):
            pass
    

    页面文件 login_page.py

    继承 Page 类,就可以运用自己定义的方法,提高代码灵活性和开发速度:

    
    class Login_Page(Page):
        # 定位方式
        username_input = By.NAME, 'username'
        password_input = By.NAME, 'password'
        login_button = By.NAME, 'submit'
        logout_link = By.LINK_TEXT, '退出'
        
        # 页面操作
        def login(self, username, password):
            '''登录操作'''
            self.wait(self.username_input)
            driver.find(self.username_input).send_keys(username)
            driver.find(self.password_input).send_keys(password)
            driver.find(self.login_button).click()
    
        def logout(self):
            '''登出操作'''
            driver.find(self.logout_link )
    

    用例文件 test_login.py

    import unittest
    from selenium import webdriver
    from login_page import Login_Page
    
    class TestLogin(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
        
        def test_login(self):
            lp = Login_Page(self.driver)
            lp.login('righteruser', '123456')
            self.assertEqual(login_text, '登录成功')
            lp.logout()
    
        def tearDown(self):
            self.driver.quit()
    

    网上讲的最多应该就是第三种,因为前面两种算不上真正意义上的 PO。当然,这也不见得是最好的,最终还要取决于你的项目情况和框架。只要能领会到这种设计模式的意义和目的,至于具体代码各有各的变化。

    相关文章

      网友评论

          本文标题:Python + Selenium PO 设计模式实践

          本文链接:https://www.haomeiwen.com/subject/slxxictx.html