base类
from selenium import webdriver
import unittest
import traceback
import env
import datetime
from vendor.log.log import Log
class BaseCase(unittest.TestCase):
@classmethod
def setUpClass(cls): # 必须使用@classmethod 装饰器,所有test运行前运行一次
cls.log = Log()
# options.add_argument('headless') # 无界面
options = webdriver.ChromeOptions()
prefs = {"download.default_directory": env.DOWNLOAD_FILE_PATH}
options.add_experimental_option("prefs", prefs)
cls.driver = webdriver.Chrome(options=options)
cls.driver.maximize_window()
cls.failureException = cls.failure_monitor()
def setUp(self): # 每个测试函数运行前运行
pass
def tearDown(self): # 每个测试函数运行完后执行
self.driver.refresh() # 主要要刷新页面
@classmethod # 必须使用@classmethod装饰器,所有test运行完后运行一次
def tearDownClass(cls):
cls.driver.quit()
def failure_monitor(self): # 知识点
test_case = self # 将self赋值给test_case,以便下方的AssertionErrorPlus内部类可调用外部类的方法
class AssertionErrorPlus(AssertionError):
def __init__(self, msg):
try:
cur_method = test_case._testMethodName # 当前test函数的名称
time_code = ''.join(datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))
file_name = '%s_%s.png' % (cur_method, time_code)
test_case.driver.get_screenshot_as_file(env.CASE_FAILURE_PATH + file_name) # 截图生成png文件
print('失败截图已保存到: %s' % file_name)
msg += '
失败截图文件: %s' % file_name
except BaseException:
print('截图失败: %s' % traceback.format_exc())
super(AssertionErrorPlus, self).__init__(msg)
return AssertionErrorPlus # 返回AssertionErrorPlus类
子类:
@ddt.ddt
class NewTicketCase(BaseCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
# 如果不能添加cookie,就让他登录一遍,再继续登录后的操作
cls.driver.get("http://test.autoxiaoer.com:8084/#/login")
ab = AdminLoginBusiness(cls.driver) # 先使用admin_login_yaml文件
ab.admin_login('100473', '123456')
cls.nb = NewTicketBusiness(cls.driver)
cls.nb.before_handle()
cls.nh = NewTicketHandle(cls.driver)
warnings.simplefilter("ignore", ResourceWarning)
def setUp(self):
super().setUp()
self.nb.new_service_ticket_button_click()
def tearDown(self):
super().tearDown()
if "test_new_ticket" in self._testMethodName:
time.sleep(9)
@classmethod # 必须使用@classmethod装饰器,所有test运行完后运行一次
def tearDownClass(cls):
super().tearDownClass()