zoukankan      html  css  js  c++  java
  • selenium自动化测试框架之PO设计模式

    面向对象的特性:封装、继承、多态。在自动化中一样适用,Selenium自动化测试中有一个名字常常被提及PageObject(思想与面向对象的特性相同),通过PO模式可以大大提高测试用例的维护效率。

    传统测试脚本的弊端

    • 测试脚本分离,维护成本高
    • 可扩展性差
    • 复用性低等

    PageObject设计模式

    PO的核心要素:

    1. 在PO模式中抽象封装成一个BasePage类,该基类应该拥有一个只实现webdriver实例的属性。
    2. 每个一个page都继承BasePage,通过driver来管理本page中元素,将page中的操作封装成一个个的方法。
    3. TestCase继承unittest.Testcase类,并且依赖page类,从而实现相应的测试步骤。

    加入我们,群,642830685,领取最新软件测试资料大厂面试和Python自动化、接口、框架搭建学习资料!

    案例

    基础案例

    前面基础场景选取的是baidu搜索页面(baidu页面简单,不需要搭建测试环境)baidu.py

    from selenium import webdriver

    from time import sleep

    driver = webdriver.Firefox()

    driver.get("http://www.baidu.com")

    driver.find_element_by_xpath("//input[@id='kw']").send_keys("Bela")

    driver.find_element_by_xpath("//input[@id='su']").click() sleep(5)

    driver.quit()

    将上面的脚本放在baidu.py文件中。

    分析
    通过对baidu.py脚本的分析,可以提取到:

    1.不同的运行脚本环境,浏览器不同:驱动webdriver.Firefox()可以剥离,
    2.请求地址的变化(生产环境与测试环境):url== http://www.baidu.com可以剥离
    3.操作元素时,常常需要等待元素加载完毕后方可进行操作:是否可以把webdriver提供的find element 方法封装下,才操作元素前,先判断元素的是否可操作。
    4.实际测试场景中,可能有多个测试场景,如果每个测试场景都需要维护url、浏览器驱动、元素定位等,效率会非常低。

    因此基于对上面的分析,是否可以设计一个所有测试页面(selenium本身是对B/S系统开展测试)的基类,来维护一些公共的方法。此处先定义个名字哦BasePage.py,用于存放页面公共方法及webdriver原有方法二次封装等。
    BasePage.py内容如下:

     

    from selenium.webdriver.support.wait import WebDriverWait

    from selenium import webdriver

    from selenium.webdriver.support import expected_conditions as EC

    class BasePage(object):

    """ BasePage封装所有页面都公用的方法,例如driver, Find_Element等

    """

    # 实例化BasePage类时,最先执行的就是__init__方法,该方法的入参,其实就是BasePage类的入参。

    # __init__方法不能有返回值,只能返回None

    def __init__(self,selenium_driver,base_url):

    self.driver = selenium_driver

    self.base_url = base_url

    # self.pagetitle = pagetitle

    def on_page(self,pagetitle):

    return pagetitle in self.driver.title

    def _open(self,url):

    self.driver.get(url)

    self.driver.maximize_window()

    def open(self):

    self._open(self.base_url,self.pagetitle)

    def find_element(self,*loc): #*loc任意数量的位置参数(带单个星号参数)

    # return self.driver.find_element(*loc)

    try:

    WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))

    return self.driver.find_element(*loc)

    except: print("%s 页面未能找到 %s 元素"%(self,loc))

    def script(self,src):

    self.driver.excute_script(src)

    def send_keys(self, loc, vaule, clear_first=True, click_first=True):

    try:

    loc = getattr(self, "_%s" % loc) # getattr相当于实现self.loc

    if click_first:

    self.find_element(*loc).click()

    if clear_first:

    self.find_element(*loc).clear()

    self.find_element(*loc).send_keys(vaule)

    except AttributeError: print("%s 页面中未能找到 %s 元素" % (self, loc))

    测试脚本的优化
    BasePage.py提取完毕,其中设计了BasePage类,对一些webdriver的方法进行了二次封装。

    baidu.py基于BasePage.py进行优化(充分体现PO的设计思想,封装、继承)

    ```

    # 基本测试场景

    # from selenium import webdriver # from time import sleep #

    # driver = webdriver.Firefox()

    # driver.get("http://www.baidu.com") #

    # driver.find_element_by_xpath("//input[@id='kw']").send_keys("Bela")

    #输入框 # driver.find_element_by_xpath("//input[@id='su']").click() #百度一下按钮 #

    # sleep(3)

    # driver.quit()

    # 优化后的测试场景

    from selenium.webdriver.common.by import By

    from PODemo.BasePage import BasePage #假设baidu.py、BasePage.py均在PODemo.BasePage目录下

    from selenium import webdriver

    class SearchPage(BasePage):

    # 定位元素

    search_loc = (By.ID,"kw")

    btn_loc = (By.ID,"su")

    def open(self):

    self._open(self.base_url)

    def search_content(self,content):

    BaiduContent = self.find_element(*self.search_loc)

    BaiduContent.send_keys(content)

    def btn_click(self):

    BaiduBtn = self.find_element(*self.btn_loc)

    BaiduBtn.click()

    PageObject总结

    1.PO设计模式中的BasePage基类对应案例中的BasePage.py文件。
    2.PO模式中的page1或pageN对应案例中的Search.py
    3.PO设计模式中TestCase对应案例中的TestCase.py

    如果您觉得对您有帮助,请点赞,收藏,分享三连!您的支持是笔者最大的动力!
    加入我们,,642830685,领取最新软件测试资料大厂面试和Python自动化、接口、框架搭建学习资料!

  • 相关阅读:
    HBase HTablePool
    Hadoop MapReduce InputFormat/OutputFormat
    MapReduce执行过程源码分析(一)——Job任务的提交
    HBase MultiVersionConsistencyControl
    HBase Split
    HBase HFile
    Do not to test a private method.
    HTML5使用ApplicationCache
    Swift使用FMDB操作SQLite
    使用Swift操作NSDate类型基础
  • 原文地址:https://www.cnblogs.com/www642830685/p/13279592.html
Copyright © 2011-2022 走看看