Misty rain
python ui自动化框架 6:初步优化框架
本章节主要为框架添加一些其他组件,比如优化testcase,添加自动截图组件,添加一个页签等操作
执行一次用例前、用例后
在运行用例的时候,我们发现如果我们有俩个用例,那他会启动俩次浏览器,我们想要启动一次浏览器然后执行用例,该怎么办呢?
将setUp方法更改为:setUpClass,将tearDown方法更改为tearDownClass。
这样再次运行为启动一次。
注意:要为setUpClass、tearDownClass方法添加注释:@classmethod
# 用例执行前
@classmethod
def setUpClass(cls):
print('用例执行前')
# 打开浏览器
cls.driver = be.open_browser(cls, '百度搜索')
# 调用页面
cls.search = SearchPage(cls.driver)
# 隐式等待30,设置一次即可
cls.search.wait(30)
报错自动截图
在用例运行期间,我们会碰到用例错误的情况,不可能人为去观察并手动截图,这时我们需要为我们的框架添加遇到错误自动截图的功能。
在项目uitest下新建‘errorimg’文件夹用来保存错误截图。
获取目录:
在‘getfileposition’文件中新建
# 获取错误截图目录
def geterrorimgpath():
path = os.path.dirname(os.path.dirname(__file__)) + '/errorimg'
return path
打开‘base_page’文件编写一个截图的函数:
#截图
def saveerrorimg(self,casename):
#自定义截图名称
nowTime = time.strftime("%Y_%m_%d_%H_%M_%S")
self.driver.get_screenshot_as_file(gf.geterrorimgpath()+'/{name}.jpg'.format(name=casename+'_'+nowTime))
在‘base_page’中的所有函数中的except中添加这个方法‘saveerrorimg('函数名')’,如下:
# 点击元素
def click(self, selector):
selector_desc = selector.split('=>')[2]
el = self.find_element(selector)
try:
el.click()
logger.info("点击了( {desc} )元素".format(desc=selector_desc))
self.sleep(2)
except NameError as e:
logger.error("点击元素出现出错:%s" % e)
self.saveerrorimg('click')
然后修改SearchPage把一个元素改成不存在的运行用例后,会有一张截图保存在errorimg下面。
basepage中的错误已经全部截图,下来我们需要将用例(testcase)中断言失败的地方进行截图。
打开‘test_search’文件修改test_searchnull用例:
# 输入空
def test_searchnull(self):
self.tsearch('')
# 捕捉AssertionError异常
try:
self.assertEqual('https://www.baidu.com/1', self.search.get_page_url())
# logger.critical('用例通过')
except AssertionError as e:
# 定义为用例失败,主动抛出异常并截图
self.search.saveerrorimg(sys._getframe().f_code.co_name)
# 主动抛出异常
logger.critical('用例失败:{fail}'.format(fail=e))
raise
else:
logger.critical('用例通过')
我将url后面加了个1,这样就会报错,报错之后就会自动截图调用self.search.saveerrorimg函数到指定的文件夹中。
同时也将test_searchtest改成以上的格式。
继续在basepage中添加常用的公共方法
这里大概新增了表单提交、鼠标悬停在一个元素上(hover)、按下鼠标左键在一个元素上、鼠标拖放、刷新页面、切换浏览器handle,直接贴完整的base_page:
"""
@Author:Misty rain(ZhangHao)
@E-mail:676817831@qq.com
@FileName:base_page.py
@Software:PyCharm
@Desc:页面元素继承类
"""
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from commont.getlog import logger
from selenium.webdriver.common.by import By
import commont.getfileposition as gf
class BasePage(object):
def __init__(self, driver):
self.driver = driver
def quit_browser(self):
self.driver.quit()
logger.info("退出浏览器")
def forward(self):
self.driver.forward()
logger.info("浏览器前进")
def back(self):
self.driver.back()
logger.info("浏览器后退")
def wait(self, seconds):
self.driver.implicitly_wait(seconds)
logger.info("隐式等待-%d-秒" % seconds)
# 定义静态方法,不需要传入self
@staticmethod
def sleep(seconds):
time.sleep(seconds)
# logger.info("模拟暂停-%s-秒" % str(seconds))
# 获取当前网页的标题
def get_page_title(self):
logger.info("当前的网页标题是:%s" % self.driver.title)
return self.driver.title
# 获取当前网页url
def get_page_url(self):
logger.info("当前的网页url是:%s" % self.driver.current_url)
return self.driver.current_url
# 最基本的查找元素方法
def find_element(self, selector):
"""
这个地方根据=>来切割字符串:
xpath=>***=>描述
"""
element = ''
if '=>' not in selector:
return self.driver.find_element_by_id(selector)
selector_by = selector.split('=>')[0]
selector_value = selector.split('=>')[1]
selector_desc = selector.split('=>')[2]
if selector_by == "i" or selector_by == 'id':
try:
element = self.driver.find_element_by_id(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
except NoSuchElementException as e:
logger.error("NoSuchElementException: %s" % e)
self.saveerrorimg('NoSuchElementException')
elif selector_by == "n" or selector_by == 'name':
element = self.driver.find_element_by_name(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
elif selector_by == "c" or selector_by == 'class_name':
element = self.driver.find_element_by_class_name(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
elif selector_by == "l" or selector_by == 'link_text':
element = self.driver.find_element_by_link_text(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
elif selector_by == "p" or selector_by == 'partial_link_text':
element = self.driver.find_element_by_partial_link_text(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
elif selector_by == "t" or selector_by == 'tag_name':
element = self.driver.find_element_by_tag_name(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
elif selector_by == "xs" or selector_by == 'xpaths':
element = self.driver.find_elements_by_xpath(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
elif selector_by == "css" or selector_by == 'css_selector':
element = self.driver.find_element_by_css_selector(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
elif selector_by == "id" or selector_by == 'id':
element = self.driver.find_element_by_id(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
elif selector_by == "x" or selector_by == 'xpath':
try:
element = self.driver.find_element(By.XPATH, selector_value)
# element = self.driver.find_element_by_xpath(selector_value)
logger.info('已找到-{value}-元素:{ys}'.format(value=selector_value, ys=selector_desc))
self.sleep(1)
except NoSuchElementException as e:
logger.error("NoSuchElementException: %s" % e)
self.saveerrorimg('NoSuchElementException')
elif selector_by == "s" or selector_by == 'selector_selector':
element = self.driver.find_element_by_css_selector(selector_value)
else:
self.saveerrorimg('元素类型有误')
raise NameError("请输入有效的元素类型")
return element
# 定义元素基本方法
def type(self, selector, text):
selector_desc = selector.split('=>')[2]
el = self.find_element(selector)
el.clear()
try:
el.send_keys(text)
logger.info('( {desc} )输入框输入了:{text}'.format(desc=selector_desc, text=text))
self.sleep(2)
except NameError as e:
logger.error("输入框输入错误:%s" % text)
self.saveerrorimg('type')
# 获取控件内容
def get_elementtext(self, selector):
selector_desc = selector.split('=>')[2]
el = ''
try:
el = self.find_element(selector)
logger.info('已获取( {desc} )的值:{text}'.format(desc=selector_desc, text=el))
self.sleep(2)
return el
except NameError as e:
logger.error("输入框获取值失败:%s" % e)
self.saveerrorimg('get_elementtext')
return el
# 清除文本框
def clear(self, selector):
el = self.find_element(selector)
try:
el.clear()
logger.info("清除当前文本框")
except NameError as e:
logger.error("清除了文本框错误:%s" % e)
self.saveerrorimg('clear')
# 点击元素
def click(self, selector):
selector_desc = selector.split('=>')[2]
el = self.find_element(selector)
try:
el.click()
logger.info("点击了( {desc} )元素".format(desc=selector_desc))
self.sleep(2)
except NameError as e:
logger.error("点击元素出现出错:%s" % e)
self.saveerrorimg('click')
# 截图
def saveerrorimg(self, casename):
# 自定义截图名称
nowTime = time.strftime("%Y_%m_%d_%H_%M_%S")
self.driver.get_screenshot_as_file(gf.geterrorimgpath() + '/{name}.png'.format(name=casename + '_' + nowTime))
logger.error(
'错误截图已保存到:{png}'.format(png=gf.geterrorimgpath() + '/{name}.png'.format(name=casename + '_' + nowTime)))
# 表单提交
def formsubmission(self, selector):
selector_desc = selector.split('=>')[2]
el = self.find_element(selector)
try:
el.submit()
logger.info("提交表单:( {desc} )".format(desc=selector_desc))
self.sleep(3)
except NameError as e:
logger.error("提交表单出错:%s" % e)
self.saveerrorimg('formsubmission')
# 鼠标悬停在一个元素上(hover)
def mouseover(self, selector):
selector_desc = selector.split('=>')[2]
el = self.find_element(selector)
try:
ActionChains(self.driver).move_to_element(el).perform()
logger.info("鼠标悬停在元素:( {desc} )".format(desc=selector_desc))
self.sleep(3)
except NameError as e:
logger.error("鼠标悬停在元素出错:%s" % e)
self.saveerrorimg('mouseover')
# 按下鼠标左键在一个元素上
def leftmouse(self, selector):
selector_desc = selector.split('=>')[2]
el = self.find_element(selector)
try:
ActionChains(self.driver).click_and_hold(el).perform()
logger.info("鼠标左键点击元素:( {desc} )".format(desc=selector_desc))
self.sleep(3)
except NameError as e:
logger.error("鼠标左键点击元素出错:%s" % e)
self.saveerrorimg('leftmouse')
# 鼠标拖放
def draganddropmouse(self, selector1, selector2):
selector_desc1 = selector1.split('=>')[2]
selector_desc2 = selector2.split('=>')[2]
el1 = self.find_element(selector1)
el2 = self.find_element(selector1)
try:
ActionChains(self.driver).drag_and_drop(el1, el2).perform()
logger.info("鼠标按下:( {desc} )".format(desc=selector_desc1))
logger.info("鼠标释放:( {desc} )".format(desc=selector_desc2))
self.sleep(3)
except NameError as e:
logger.error("鼠标拖放出错:%s" % e)
self.saveerrorimg('draganddropmouse')
# 刷新页面
def refresh(self):
self.driver.refresh()
logger.info('刷新当前页面')
# 切换浏览器handle
def switchwindow(self):
# 获取当前窗口
handle = self.driver.current_window_handle
# 获取浏览器所有窗口
handles = self.driver.window_handles
for newhandle in handles:
if newhandle != handle:
# 切换窗口b
self.driver.switch_to_window(newhandle)
logger.info('切换到窗口:{window}'.format(window=newhandle))
# 返回窗口driver
return self.driver
1
1
1
1
1
1
1
1
-1; waitfor delay '0:0:15' --
1
1
1
1
1
response.write(9890739*9110082)
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
555
555
555
555
555
555
1BB0vBCxLgO
555
1
1
555
555
555
555
555
555
555'"()&%
HSDn(9190)Only now, with year round availability of carbohydrate energy and 24 7 light exposure, have our hearts had to deal with endless summer best place to buy generic cialis online UNE NOUVELLE NORMALITÉ
看的我热血沸腾啊
独家破解!私服传奇脱机脚本代码,助你畅游无忧!:https://501h.com/fugu/2024-08-05/25855.html
壹原始传奇年卡购买攻略:https://501h.com/danzhiye/211.html
《冠军亚瑟》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/2779.html