python3+selenium框架设计04-封装测试基类
在完成了日志类封装之后,那我们就要对测试基类进行实现,在其中对一些请求再次封装,在项目下新建一个framework文件夹,在文件夹下新建Base_Page.py文件,这是用来写测试基类的文件。在项目下新建screenshots文件件,用来存放截图。项目结构如下。
具体怎么封装还是要看被测试需要什么,只能提供一个大致思路,我项目能用的封装,你项目并不一定能用
测试基类的思路就是把所有能公用的方法全部封装,并加上日志输出。报错截图等。
看下Base_Page.py下具体代码
from Logs.log import log1
from selenium.common.exceptions import NoSuchElementException
import getcwd
import os
import time
class BasePage:
"""测试基类"""
def __init__(self, driver):
self.driver = driver
@staticmethod
def isdisplayed(element):
"""元素是否存在"""
value = element.is_displayed()
return value
@staticmethod
def my_sleep(secondes):
"""强制等待"""
time.sleep(secondes)
log1.info('暂停%d秒' % secondes)
def get_img(self):
"""截图"""
path = os.path.join(getcwd.get_cwd(), 'screenshots/') # 拼接截图保存路径
rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) # 按格式获取当前时间
screen_name = path + rq + '.png' # 拼接截图文件名
# noinspection PyBroadException
try:
self.driver.get_screenshot_as_file(screen_name)
log1.info("截图保存成功")
except BaseException:
log1.error("截图失败", exc_info=1)
def find_element(self, selector):
"""定位元素"""
by = selector[0]
value = selector[1]
element = None
if by in ['id', 'name', 'class', 'tag', 'link', 'plink', 'css', 'xpath']:
# noinspection PyBroadException
try:
if by == 'id':
element = self.driver.find_element_by_id(value)
elif by == 'name':
element = self.driver.find_element_by_name(value)
elif by == 'class':
element = self.driver.find_element_by_class_name(value)
elif by == 'tag':
element = self.driver.find_element_by_tag_name(value)
elif by == 'link':
element = self.driver.find_element_by_link_text(value)
elif by == 'plink':
element = self.driver.find_element_by_partial_link_text(value)
elif by == 'css':
element = self.driver.find_element_by_css_selector(value)
elif by == 'xpath':
element = self.driver.find_element_by_xpath(value)
else:
log1.error('没有找到元素')
log1.info('元素定位成功。定位方式:%s,使用的值%s:' % (by, value))
return element
except NoSuchElementException:
log1.error("报错信息:", exc_info=1)
self.get_img() # 调用截图
else:
log1.error('输入的元素定位方式错误')
def type(self, selector, value):
"""输入内容"""
element = self.find_element(selector)
element.clear()
log1.info('清空输入内容')
# noinspection PyBroadException
try:
element.send_keys(value)
log1.info('输入的内容:%s' % value)
except BaseException:
log1.error('内容输入报错', exc_info=1)
self.get_img()
def click(self, selector):
"""点击元素"""
element = self.find_element(selector)
# noinspection PyBroadException
try:
element.click()
log1.info('点击元素成功')
except BaseException:
display = self.isdisplayed(element)
if display is True:
self.my_sleep(3)
element.click()
log1.info('点击元素成功')
else:
self.get_img()
log1.error('点击元素报错', exc_info=1)
def use_js(self, js):
"""调用js"""
# noinspection PyBroadException
try:
self.driver.execute_script(js)
log1.info('js执行成功,js内容为:%s' % js)
except BaseException:
log1.error('js执行报错', exc_info=1)
def switch_menue(self, parentelement, secelement, targetelement):
"""三级菜单切换"""
self.my_sleep(3)
# noinspection PyBroadException
try:
self.driver.switch_to_default_content()
self.click(parentelement)
log1.info('成功点击一级菜单:%s' % parentelement)
self.click(secelement)
log1.info('成功点击二级菜单:%s' % secelement)
self.click(targetelement)
log1.info('成功点击三级菜单:%s' % targetelement)
except BaseException:
log1.error('切换菜单报错', exc_info=1)
def switch_ifarme(self, selector):
"""切换farm"""
element = self.find_element(selector)
# noinspection PyBroadException
try:
self.driver.switch_to.frame(element)
log1.info('切换frame成功')
except BaseException:
log1.error('切换frame报错', exc_info=1)
def get_title(self):
"""获取title"""
title = self.driver.title
log1.info('当前窗口的title是:%s' % title)
return title
def my_quit(self):
"""关闭浏览器"""
self.driver.quit()
log1.info('关闭浏览器')
接下来测试一下封装的代码
在test_base.py写如下代码并运行