1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Python版UI自动化测试的断言方法/Selenium Pytest方法封装断言

Python版UI自动化测试的断言方法/Selenium Pytest方法封装断言

时间:2020-08-02 11:55:10

相关推荐

Python版UI自动化测试的断言方法/Selenium Pytest方法封装断言

分享几个自己封装的一些断言!

觉得有帮助的小伙伴可以点个赞!分享给更多人!

目录标题

前置条件断言预期的元素是否可见断言实际值是否包含预期值断言实际值是否包含多个预期的文本中的一个(模糊断言)断言实际值是否等于预期值断言为真断言为假断言预期文件是否存在(导出/下载后的等)调试代码

前置条件

pip install seleniumpip install pytest

断言预期的元素是否可见

def wait_element_visibility(self, locator, timeout=15, poll=0.3, extra_wait=None):"""等待元素可见(浏览器窗口显示范围内):param extra_wait: 智能等待结束后的额外等待时间/秒:param locator: 元素定位信息:param timeout: 超时时间:param poll: 轮询间隔时间/秒:return:"""mes = 'xxx'try:WebDriverWait(self.driver, timeout, poll).until(ec.visibility_of_element_located(locator))if extra_wait:sleep_wait(extra_wait)print(f"{mes}可见")return Trueexcept Exception as e:print(f"{mes}不可见.{e}")return Falsedef assert_element_visible(self, assert_locator, timeout=15):"""断言元素是否可见"""assert self.wait_element_visibility(assert_locator, timeout)

断言实际值是否包含预期值

def assert_contains2(self, actual, expected):""":param actual::param expected::return:断言@actual是否包含@expected"""mes = f'实际值:[{actual}]包含预期值:[{expected}]'try:assert expected in actualexcept AssertionError:raise AssertionError(f'{mes};不包含')

断言实际值是否包含多个预期的文本中的一个(模糊断言)

def assert_contains(self, actual, expected):""":param actual::param expected::return:断言@actual是否包含@expected"""mes = f'实际值:[{actual}]包含预期值:[{expected}]'def __assert_c(act, exp_val):try:TeCa().assertIn(str(exp_val), str(act))except AssertionError:raise AssertionError(f'{mes};不包含')if isinstance(expected, tuple) or isinstance(expected, list):exp = ''for val in expected:exp = valif actual == val:break__assert_c(actual, exp)else:__assert_c(actual, expected)

断言实际值是否等于预期值

def assert_equals(self, actual, expected):""":param actual::param expected::return:断言@actual是否等于@expected"""mes = f'实际值:[{actual}]等于预期值:[{expected}]'try:assert expected == actualexcept AssertionError:raise AssertionError(f'{mes};不包含')def assert_equals2(self, actual, expected):""":param actual::param expected::return:断言@actual是否等于@expected"""mes = f'实际值:[{actual}]等于预期值:[{expected}]'try:TeCa().assertEqual(actual, expected)except AssertionError:raise AssertionError(f'{mes};不包含')

断言为真

def assert_ture(self, exp):""":param exp::return: 断言是否为真"""TeCa().assertTrue(exp)def assert_ture2(self, exp):""":param exp::return: 断言是否为真"""assert exp is True

断言为假

def assert_false(self, exp):""":param exp::return: 断言是否为假"""assert exp is Falsedef assert_false2(self, exp):""":param exp::return: 断言是否为假"""TeCa().assertFalse(exp)

断言预期文件是否存在(导出/下载后的等)

output_path = r'xxx\xxx\xxx'def assert_output_file_exist(self, expect_file_path: str, timeout=60, poll=10, contrast_num=None):""":param contrast_num: 导出文件前的目标文件夹的文件数量:param expect_file_path: 预期的下载的文件的路径:param timeout: 超时时间/s:param poll: 轮询间隔/s:return: 断言是否已经发现预期文件"""if self.output_path not in expect_file_path:expect_file_path = os.path.join(self.output_path, expect_file_path)end_time = time.time() + timeoutwhile True:bool_file = FileUtils.file_if_exist(expect_file_path)# 若文件存在if bool_file is True:print(f"发现预期文件:{expect_file_path},创建时间:{os.path.getctime(expect_file_path)}")return bool_file# 若文件数量增加了,但是还是没有找到预期文件则停止,处理文件已经导出但是文件名称不对的情况--避免傻等if contrast_num is not None and self.get_current_output_dir_file_account() > contrast_num and \FileUtils.file_if_exist(expect_file_path) is False:raise AssertionError(f"导出文件:{expect_file_path}不存在,发现其他文件{os.listdir(self.em_output_path)};"f"超时时间:{int(time.time() - start_time)}秒")sleep_wait(poll)# 超时则停止然后抛出异常if time.time() > end_time:breakraise AssertionError(f"导出文件:{expect_file_path}不存在;超时时间:{timeout}")def get_current_output_dir_file_account(self):return FileUtils.get_file_amount_by_dir(self.output_path)class FileUtils:@classmethoddef file_if_exist(cls, file_path):"""文件是否存在"""if os.path.exists(file_path) and os.path.isfile(file_path):return Truereturn False@classmethoddef get_file_amount_by_dir(cls, dir_path):""":param dir_path: 文件夹路径:return: 文件夹下有多少文件"""gf_list = os.listdir(dir_path)file_list = []for rf in gf_list:file_path = os.path.join(dir_path, rf)if os.path.isfile(file_path):file_list.append(file_path)return len(file_list)

调试代码

import osimport timefrom unittest import TestCase as TeCafrom selenium.webdriver.support import expected_conditions as ecfrom selenium.webdriver.support.wait import WebDriverWaitdef sleep_wait(poll):time.sleep(poll)class AssertUtils:def __init__(self):# 浏览器驱动self.driver = Nonedef assert_contains(self, actual, expected):""":param actual::param expected::return:断言@actual是否包含@expected"""mes = f'实际值:[{actual}]包含预期值:[{expected}]'def __assert_c(act, exp_val):try:TeCa().assertIn(str(exp_val), str(act))except AssertionError:raise AssertionError(f'{mes};不包含')if isinstance(expected, tuple) or isinstance(expected, list):exp = ''for val in expected:exp = valif actual == val:break__assert_c(actual, exp)else:__assert_c(actual, expected)def assert_contains2(self, actual, expected):""":param actual::param expected::return:断言@actual是否包含@expected"""mes = f'实际值:[{actual}]包含预期值:[{expected}]'try:assert expected in actualexcept AssertionError:raise AssertionError(f'{mes};不包含')def assert_equals(self, actual, expected):""":param actual::param expected::return:断言@actual是否等于@expected"""mes = f'实际值:[{actual}]等于预期值:[{expected}]'try:assert expected == actualexcept AssertionError:raise AssertionError(f'{mes};不包含')def assert_equals2(self, actual, expected):""":param actual::param expected::return:断言@actual是否等于@expected"""mes = f'实际值:[{actual}]等于预期值:[{expected}]'try:TeCa().assertEqual(actual, expected)except AssertionError:raise AssertionError(f'{mes};不包含')def assert_ture(self, exp):""":param exp::return: 断言是否为真"""TeCa().assertTrue(exp)def assert_ture2(self, exp):""":param exp::return: 断言是否为真"""assert exp is Truedef assert_false(self, exp):""":param exp::return: 断言是否为假"""assert exp is Falsedef assert_false2(self, exp):""":param exp::return: 断言是否为假"""TeCa().assertFalse(exp)def wait_element_visibility(self, locator, timeout=15, poll=0.3, extra_wait=None):"""等待元素可见(浏览器窗口显示范围内):param extra_wait: 智能等待结束后的额外等待时间/秒:param locator: 元素定位信息:param timeout: 超时时间:param poll: 轮询间隔时间/秒:return:"""mes = 'xxx'try:WebDriverWait(self.driver, timeout, poll).until(ec.visibility_of_element_located(locator))if extra_wait:sleep_wait(extra_wait)print(f"{mes}可见")return Trueexcept Exception as e:print(f"{mes}不可见.{e}")return Falsedef assert_element_visible(self, assert_locator, timeout=15):"""断言元素是否可见"""assert self.wait_element_visibility(assert_locator, timeout)output_path = r'xxx\xxx\xxx'def assert_output_file_exist(self, expect_file_path: str, timeout=60, poll=10, contrast_num=None):""":param contrast_num: 导出文件前的目标文件夹的文件数量:param expect_file_path: 预期的下载的文件的路径:param timeout: 超时时间/s:param poll: 轮询间隔/s:return: 断言是否已经发现预期文件"""if self.output_path not in expect_file_path:expect_file_path = os.path.join(self.output_path, expect_file_path)end_time = time.time() + timeoutwhile True:bool_file = FileUtils.file_if_exist(expect_file_path)# 若文件存在if bool_file is True:print(f"发现预期文件:{expect_file_path},创建时间:{os.path.getctime(expect_file_path)}")return bool_file# 若文件数量增加了,但是还是没有找到预期文件则停止,处理文件已经导出但是文件名称不对的情况--避免傻等if contrast_num is not None and self.get_current_output_dir_file_account() > contrast_num and \FileUtils.file_if_exist(expect_file_path) is False:raise AssertionError(f"导出文件:{expect_file_path}不存在,发现其他文件{os.listdir(self.em_output_path)};"f"超时时间:{int(time.time() - start_time)}秒")sleep_wait(poll)# 超时则停止然后抛出异常if time.time() > end_time:breakraise AssertionError(f"导出文件:{expect_file_path}不存在;超时时间:{timeout}")def get_current_output_dir_file_account(self):return FileUtils.get_file_amount_by_dir(self.output_path)class FileUtils:@classmethoddef file_if_exist(cls, file_path):"""文件是否存在"""if os.path.exists(file_path) and os.path.isfile(file_path):return Truereturn False@classmethoddef get_file_amount_by_dir(cls, dir_path):""":param dir_path: 文件夹路径:return: 文件夹下有多少文件"""gf_list = os.listdir(dir_path)file_list = []for rf in gf_list:file_path = os.path.join(dir_path, rf)if os.path.isfile(file_path):file_list.append(file_path)return len(file_list)if __name__ == '__main__':# AssertUtils().assert_ture(1 > 1)AssertUtils().assert_contains(1, (3, 2, 2))

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。