Python+Selenium WebUI自动化框架 — 统一入口自动化

导读:本篇文章讲解 Python+Selenium WebUI自动化框架 — 统一入口自动化,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

前言:

      执行工厂封装,实现一个入口调用所有Selenium基本操作,让所有页面操作一键调用,让UI自动化框架脱离高成本、低效率时代,将用例的重用性贯彻到极致,让烦人的PO模型变得无所谓,让一个测试小白都能编写并实现自动化

知识储备前提:熟练python语言理论与实际运用,熟悉selenium库与自动化测试环境配置。上一篇文章

《Python+Selenium WebUI自动化框架 — 基础操作封装》:https://blog.csdn.net/weixin_40331132/article/details/105915042

 

工厂封装:

目录

Python+Selenium WebUI自动化框架 -- 统一入口自动化

1、首先配置函数注册ini文件,

         中文名 = 基础类函数名

           打开网页 = open_url

open_url 对应browseroperator.py里的def open_url(self, **kwargs)  函数。

配置文件是通过中文查到函数名,然后通过getattr获得BrowserOperator的方法,然后执行。

Python+Selenium WebUI自动化框架 -- 统一入口自动化

      切记,切记,不能有错字。

    下面是我的部分方法注册配置,供参考,基础类函数名与上一篇文章 《基础操作封装》的函数同名

[Function]
打开网页 = open_url
关闭浏览器 = close_browser
对话框上传文件 = upload_file
点击 = element_click
输入 = element_input
截图 = get_screenshot_as_file
寻找元素 = find_element
隐式等待 = web_implicitly_wait
等待元素可见 = web_element_wait
等待 = gotosleep

2、初始化工厂 def __init__(self)

        初始化配置文件方法注册对象,self.con_fun

        初始化浏览器对象:self.browser_opr,网页操作对象:self.webdriver_opr,因为self.webdriver_opr是要打开网页后才能获取,暂赋值None,也可以先不定义,看个人习惯。

3、执行入口函数,def execute_keyword(self, **kwargs);   所有的用例都通过这个方法无差别执行

      先通过kwargs[‘keyword’]取的参数里的中文名;

      然后通过self.con_fun对象匹配 基础类函数名;

      再就通过 getattr在两个基础类对象里获取可执行方法;

      最后执行方法。

具体请细瞧代码:

from common.getconf import Config
from basefactory.browseroperator import BrowserOperator
from basefactory.webdriveroperator import WebdriverOperator


class Factory(object):

    def __init__(self):
        self.con = Config()
        self.con_fun = dict(self.con.items('Function'))


        """
        浏览器操作对象
        """
        self.browser_opr = BrowserOperator()
        """
        网页操作对象
        """
        self.webdriver_opr = None


    def init_webdriver_opr(self, driver):
        self.webdriver_opr = WebdriverOperator(driver)


    def get_base_function(self, function_name):
        try:
            function = getattr(self.browser_opr, function_name)
        except Exception:
            try:
                function = getattr(self.webdriver_opr, function_name)
            except Exception:
                return False, '未找到注册方法[' + function_name + ']所对应的执行函数,请检查配置文件'
        return True, function

    def execute_keyword(self, **kwargs):
        """
        工厂函数,用例执行方法的入口
        :param kwargs:
        :return:
        """
        try:
            keyword = kwargs['keyword']
            if keyword is None:
                return False, '没有keyword,请检查用例'
        except KeyError:
            return False, '没有keyword,请检查用例'

        _isbrowser = False

        try:
            function_name = self.con_fun[keyword]
        except KeyError:
            return False, '方法Key['+ keyword +']未注册,请检查用例'

        #获取基础类方法
        isOK, result = self.get_base_function(function_name)
        if isOK:
            function = result
        else:
            return isOK, result

        #执行基础方法,如打网点页、点击、定位、隐式等待 等
        isOK, result = function(**kwargs)

        #如果是打开网页,唯一一次初始化浏览器,需要将webdriver传参给另一个基础类,方便他操控元素
        if '打开网页' == keyword and isOK:
            url = kwargs['locator']
            self.init_webdriver_opr(result)
            return isOK, '网页[' + url + ']打开成功'

        return isOK, result

 

 

 

朋友们知道如何一键做自动化, 下面有调用代码,运行规则与结果,来看看吧。

规则:

        初始化工厂类对象,用其调用 execute_keyword() 传入自然语言与其他定位参数,一键执行执行自动化

from common.factory import Factory
fac = Factory()
_isOK, _strLog = fac.execute_keyword(keyword='打开网页', locator='https://xxx-static.xxxx.xx.xx/umc-cccon/x/xxxxx.html#/login')
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='等待元素可见', type='xpath',locator='/html/body/div[1]/div/div[2]/div/div/form/div[1]/div[2]/p[1]/input', time=15)
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='输入',type='xpath',locator='/html/body/div[1]/div/div[2]/div/div/form/div[1]/div[2]/p[1]/input', input='ABC')
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='输入',type='xpath',locator='/html/body/div[1]/div/div[2]/div/div/form/div[1]/div[2]/p[2]/input', input='FXXX454p')
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='点击',type='xpath',locator='/html/body/div[1]/div/div[2]/div/div/form/div[3]/a')
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='等待元素可见', type='xpath',locator='/html/body/div[1]/div/div[2]/ul/div[2]/li', time=15)  #wait mune
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='点击',type='xpath',locator='/html/body/div[1]/div/div[2]/ul/div[2]/li')
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='等待元素可见', type='xpath',locator='//*[@id="mainSection"]/div/div/section[1]/form/div[3]/div/div/div/div/i', time=15)  #wait time_elem
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='点击',type='xpath',locator='//*[@id="mainSection"]/div/div/section[1]/form/div[3]/div/div/div/div/i')     #clear_time
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='点击',type='xpath',locator='//*[@id="mainSection"]/div/div/section[1]/form/div[2]/div/div/div[1]/span[2]')    #select_click
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='点击',type='xpath',locator='//*[@id="mainSection"]/div/div/section[1]/form/div[2]/div/div/div[2]/ul[2]/li[2]')    #select_待上传
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='点击',type='xpath',locator='//*[@id="mainSection"]/div/div/section[1]/form/button[1]/i')    #click_search
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='等待元素可见', type='xpath',locator='//*[@id="mainSection"]/div/div/section[2]/div[1]/div/div[2]/table/tbody/tr[1]/td[12]/div/div/button', time=15)  #wait updata_button
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='点击',type='xpath',locator='//*[@id="mainSection"]/div/div/section[2]/div[1]/div/div[2]/table/tbody/tr[1]/td[12]/div/div/button')    #click_updata_button
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='等待元素可见', type='xpath',locator='//*[@id="mainSection"]/div/div/form/div[1]/div/section/div/div/div', time=15)  #wait updata_button
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='点击',type='xpath',locator='//*[@id="mainSection"]/div/div/form/div[1]/div/section/div/div/div')    #click_updataFile_button
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='等待')
print(_strLog)
_isOK, _strLog = fac.execute_keyword(keyword='对话框上传文件', type='#32770', locator=r'C:\Users\kerici\Pictures\Saved Pictures\无标题.png', index='打开(&O)')  #文件路径不能错,错了,就永远卡住用例
print(_strLog)

 

执行结果图

    最后上传文件的那条case执行完成的页面

Python+Selenium WebUI自动化框架 -- 统一入口自动化

 

执行结果打印:

网页[https://xxx-static.xxxxx.xxx.xx/umc-xxxon/xxxxxx.html#/login]打开成功
元素[/html/body/div[1]/div/div[2]/div/div/form/div[1]/div[2]/p[1]/input]等待出现成功
元素[/html/body/div[1]/div/div[2]/div/div/form/div[1]/div[2]/p[1]/input]输入[OKL]成功
元素[/html/body/div[1]/div/div[2]/div/div/form/div[1]/div[2]/p[2]/input]输入[FxKf454p]成功
元素[/html/body/div[1]/div/div[2]/div/div/form/div[3]/a]点击成功
元素[/html/body/div[1]/div/div[2]/ul/div[2]/li]等待出现成功
元素[/html/body/div[1]/div/div[2]/ul/div[2]/li]点击成功
元素[//*[@id=”mainSection”]/div/div/section[1]/form/div[3]/div/div/div/div/i]等待出现成功
元素[//*[@id=”mainSection”]/div/div/section[1]/form/div[3]/div/div/div/div/i]点击成功
元素[//*[@id=”mainSection”]/div/div/section[1]/form/div[2]/div/div/div[1]/span[2]]点击成功
元素[//*[@id=”mainSection”]/div/div/section[1]/form/div[2]/div/div/div[2]/ul[2]/li[2]]点击成功
元素[//*[@id=”mainSection”]/div/div/section[1]/form/button[1]/i]点击成功
元素[//*[@id=”mainSection”]/div/div/section[2]/div[1]/div/div[2]/table/tbody/tr[1]/td[12]/div/div/button]等待出现成功
元素[//*[@id=”mainSection”]/div/div/section[2]/div[1]/div/div[2]/table/tbody/tr[1]/td[12]/div/div/button]点击成功
元素[//*[@id=”mainSection”]/div/div/form/div[1]/div/section/div/div/div]等待出现成功
元素[//*[@id=”mainSection”]/div/div/form/div[1]/div/section/div/div/div]点击成功
等待成功
上传文件成功
 

结语:很有趣的工厂吧。工厂的操作不仅仅如此,下一编是excel编写case操作的工厂化了,实现特殊PO模型,实现后不用动代码便可编写自动化用例了。

让我们一起学习

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

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/5461.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!