python ui自动化表单_Python实现UI自动化框架 -- 一键调用页面操作

前言:

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

知识储备前提:熟练python语言理论与实际运用,熟悉selenium库与自动化测试环境配置。上一篇文章《Python实现UI自动化框架 -- 基础操作封装》

工厂类(Factory)的文案尚未完成(忙于学习,五一过后应该写完了)。

工厂封装:

1、首先配置函数注册ini文件,中文名=基础类函数名,切记,切记,不能有错字。

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

[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、初始化工厂

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

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

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)

#如果是打开网页,是浏览器初始化,需要将返回值传递给另一个基础类

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执行完成的页面

执行结果打印:

网页[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]点击成功

等待成功

上传文件成功

结语:这是我的UI自动化框架第二篇章,很有趣的工厂吧。工厂的操作不仅仅如此,下一编是excel编写case操作的工厂化了。不需要代码去实现PO模型,只要了解这工厂,什么UI自动化用例都能用excel写出来。以后基本就是论新手如何驾驭自动化了

下期的标题是 : “Python实现UI自动化框架 -- excel用例处理PO模型”

让我们一起学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值