分享几个自己封装的一些断言!
觉得有帮助的同学可以点个赞!分享给更多人!
- 前置条件
- 断言预期的元素是否可见
- 断言实际值是否包含预期值
- 断言实际值是否包含多个预期的文本中的一个(模糊断言)
- 断言实际值是否等于预期值
- 断言为真
- 断言为假
- 断言预期文件是否存在(导出/下载后的等)
- 调试代码
pip install selenium
pip 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 True
except Exception as e:
print(f"{mes}不可见.{e}")
return False
def 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 actual
except 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 = val
if 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 == actual
except 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 False
def 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() + timeout
while 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 and self.get_current_output_dir_file_account() > contrast_num and FileUtils.file_if_exist(
expect_file_path) is False:
break
sleep_wait(poll)
# 超时则停止然后抛出异常
if time.time() > end_time:
break
raise 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:
@classmethod
def file_if_exist(cls, file_path):
"""文件是否存在"""
if os.path.exists(file_path) and os.path.isfile(file_path):
return True
return False
@classmethod
def 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 os
import time
from unittest import TestCase as TeCa
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
def sleep_wait(poll):
time.sleep(poll)
class AssertUtils:
def __init__(self):
# 浏览器驱动
self.driver = None
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 = val
if 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 actual
except 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 == actual
except 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 False
def 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 True
except Exception as e:
print(f"{mes}不可见.{e}")
return False
def 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() + timeout
while 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 and self.get_current_output_dir_file_account() > contrast_num and FileUtils.file_if_exist(
expect_file_path) is False:
break
sleep_wait(poll)
# 超时则停止然后抛出异常
if time.time() > end_time:
break
raise 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:
@classmethod
def file_if_exist(cls, file_path):
"""文件是否存在"""
if os.path.exists(file_path) and os.path.isfile(file_path):
return True
return False
@classmethod
def 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))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)