重写unittest.TestCase 不仅继承了unittest的方法,还丰富不同断言方法,用起来更方便、简单。
import unittest
from common.BSBase import BasePage
from common.log import log
from common.browserDriver import getdriver
from common.publicFunc import *
class BSTestCase(unittest.TestCase, BasePage):
@classmethod
def setUpClass(cls):
"""get driver"""
log.info('[BS]_002:setUpClass')
cls.driver = ]getdriver()
if cls.driver is None:
cls.driver = getdriver()
def setUp(self):
log.info('[BS]_002:setUp')
@classmethod
def tearDownClass(cls):
log.info('[BS]_002:tearDownClass')
PublicDriver().setDriver(cls.driver)
def __call__(self, *args, **kwargs):
log.info('[BS]_001:To be run[%s]' % str(self._testMethodName))
super().__call__(*args, **kwargs)
def assertTrue(self, expr, msg=None):
try:
super().assertTrue(expr, msg)
log.warning('[BS]_200:pass')
except AssertionError:
log.warning('[BS]_555:false is not true %s' % msg)
raise self.failureException(msg)
def assertEqual(self, first, second, msg=None):
try:
super().assertEqual(first, second, msg)
log.warning('[BS]_200:pass')
except AttributeError:
log.warning('[BS]_555:%s is not equal %s' % (first, second))
raise self.failureException(msg)
def assertNotEqual(self, first, second, msg=None):
try:
super().assertNotEqual(first, second, msg)
log.warning('[BS]_200:pass')
except AttributeError:
log.warning('[BS]_555:%s is equal %s' % (first, second))
raise self.failureException(msg)
def assertIn(self, member, container, msg=None, timeout=5):
try:
super().assertIn(member, container, msg)
log.warning('[BS]_200:pass')
except AssertionError:
log.warning('[BS]_555: %s not in %s' % (member, str(container)))
raise self.failureException(msg)
def assertNotIn(self, member, container, msg=None, timeout=5):
try:
super().assertNotIn(member, container, msg)
log.warning('[BS]_200:pass')
except AssertionError:
log.warning('[BS]_555: %s in %s' % (member, str(container)))
raise self.failureException(msg)
def assertIsNotNone(self, obj, msg=None):
try:
super().assertIsNotNone(object, msg)
log.warning('[BS]_200:pass')
except AssertionError:
log.warning('[BS]_555: obj{obj} is none'.format(obj=obj))
raise self.failureException(msg)
def assertSelected(self, locator):
result = self.is_element_located_selected(locator)
self.assertTrue(result)
def assertElementVisible(self, locator, msg=None):
"""failed if element is not visible"""
element = self.find_element_visible(locator)
self.assertIsNotNone(element, msg)
def assertElementNotVisible(self, locator, msg=None):
"""failed if element is not visible"""
result = self.find_element_not_visible(locator, 10, 0.1)
self.assertTrue(result, msg)
def assert_Element_Text_Equal(self, locator, expect_text, timeout=8):
"""failed if 元素中的文字 != expect_text"""
# 有问题!只要locator中包含了expect_text,就会判断通过,
_, current_text = self.wait_until_element_located_contains_text(locator, expect_text, timeout)
self.assertEqual(expect_text, current_text)
def assert_Element_Text_Contained(self, locator, expect_text, msg=None, timeout=6, interval=0.5):
"""
获取元素的text,比较预期值是否在实际值里
"""
actual_text = self.get_element_text(locator, timeout=timeout, interval=interval)
actual_text = '' if actual_text is None else actual_text
print("actual_text: " + actual_text)
self.assertIn(expect_text, actual_text, msg=msg)
def assert_Element_Attribute_Value_Contained(self, locator, attribute, expect_text, msg=None, timeout=8,
interval=0.5):
'''
获取元素的Attribute的value,比较实际值和预期值是否相等
:param locator: 元素
:param attribute: 元素要获取的属性名
:param expect_text: 预期值
:param timeout:
:param msg:
'''
actual_text = self.get_element_Attribute_value(locator, attribute, timeout=timeout, interval=interval)
self.assertIn(expect_text, actual_text, msg=msg)
def assert_Element_Text_Not_Contained(self, locator, expect_text, timeout=8):
"""failed if 元素中的文字 != expect_text"""
_, current_text = self.wait_until_element_located_not_contains_text(locator, expect_text, timeout)
self.assertNotEqual(expect_text, current_text)
def assert_Text_In_Element(self, locator, expect_text, timeout=8):
"""failed if 元素中的文字 != expect_text"""
_, current_text = self.wait_until_element_located_contains_text(locator, expect_text, timeout)
self.assertIn(expect_text, current_text)
def assert_Text_Not_In_Element(self, locator, text, timeout=8):
"""failed if 元素中的文字 != expect_text"""
_, current_text = self.wait_until_element_located_not_contains_text(locator, text, timeout)
self.assertNotIn(text, current_text)
def assert_Selected_Option_Text_Equal(self, locator, text):
"""failed if Selected元素中已选中的文字 != expect_text"""
selected_text = self.get_selected_item_text(locator)
self.assertEqual(text, selected_text)
def assert_If_Checkbox_Selected(self, locator, msg=None):
"""checkbox 是否被选中"""
isSelected = self.checkbox_is_selected(locator)
self.assertTrue(isSelected, msg)
def assert_Text_Not_In_Selected(self, locator, text):
"""failed if text in selected"""
all_options_text = self.get_all_select_item(locator)
self.assertNotIn(text, all_options_text)
def assert_Text_In_Selected(self, locator, text):
"""failed if text not in selected"""
all_options_text = self.get_all_select_item(locator)
self.assertIn(text, all_options_text)
def assert_page_contains(self, text):
page_source = self.get_page_source()
self.assertIn(text, page_source)
def assert_page_not_contains(self, text):
page_source = self.get_page_source()
self.assertNotIn(text, page_source)
def assert_list_value_contained_in_basic_list(self, resultlist, basiclist):
""" resultlist 中的所有值都在 basiclist 中"""
if type(resultlist) != type(basiclist):
log.warning('[BS]_554:type error!,type should be the same,currentType:{type1}-{type2}'.format(
type1=type(resultlist), type2=type(basiclist)))
return False
if isinstance(resultlist, list):
"""比较对象是字符串"""
result = set(resultlist).issubset(basiclist)
self.assertTrue(result, msg='not all value in {list1} contained in {list2}'.format(list1=resultlist,
list2=basiclist))
def assert_value_contained_in_lists(self, text, lists):
"""
:param text:text = a
:param lists: resultlist = [(a,b),(a,c,d),(,a)]
:return:
"""
for i in range(len(lists)):
for j in range(len(lists[0])):
if text.lower() in lists[i][j].lower():
break
elif j+1 == len(lists[0]):
self.fail("{text} is not the lists: {list}".format(text=text, list=lists[i]))
self.assertTrue(True)
def assert_value_contained_in_list(self, text, resultlist):
"""
:param text: text = a
:param resultlist: resultlist = [test1,(c,d),testa]
:return:
"""
for i in range(len(resultlist)):
if text in resultlist[i]:
self.assertTrue(True)
break
elif i+1 == len(resultlist):
self.fail("{text} is not the list: {list}".format(text=text, list=resultlist))
def assert_value_not_contained_in_list(self, text, resultlist):
if text not in resultlist:
self.assertTrue(True)
else:
self.fail("{text} is the list: {list}".format(text=text, list=resultlist))
if __name__ == "__main__":
'''self test'''
ccc = BSTestCase('test')
a = 'test aa'
b=['test aaa', 'test aa']
ccc.assertIn(a, b,'pass')