class TestApi(unittest.TestCase): '''文章,背景音的接口''' def setUp(self): self.logger = MyLog() self.logger.info('开始测试') print('开始测试') @data(*test_data) def test_api(self, data_item): # data_item=Application_profiles(data_item) print('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) self.logger.info('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) print('测试数据为{0}'.format(data_item['param'])) self.logger.info('测试数据为{0}'.format(data_item['param'])) res = HttpRequtser().http_request(data_item['url'], eval(data_item['param']), data_item['HttpMethod'], verify=False, headers=GetInfoData().get_hander()) print('测试结果是{0}'.format(res.json())) self.logger.info('测试结果是{0}'.format(res.json())) # if res.cookies: # 任何非空数据的布尔值都为True cookies是一个类字典的格式 # COOKIES = res.cookies # 如果cookies不为空 就替换全局变量的COOKIES 修改全局变量 try: self.assertEqual(str(data_item['ExpectedResult']), str(res.json()['error_code'])) self.assertEqual(res.status_code, 200) print('用例通过') self.logger.info('用例通过') TestResult = 'PASS' Comment = None except AssertionError as e: print('用例失败错误是{}'.format(e)) self.logger.error('用例失败错误是{}'.format(e)) TestResult = 'Failed' Comment = res.json()['error_message'] raise e finally: DoExce(project_path.test_case_path).write_back( 'mindfulness', data_item['id'] + 1, res.json()['error_code'], TestResult, Comment) def tearDown(self): print('用例结束') self.logger.info('用例结束')
class TestCommunity(unittest.TestCase): '''社区''' def setUp(self): self.logger=MyLog() self.logger.info('开始测试') @data(*test_data) def test_community(self, data_item): self.logger.info('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) #查找tid,cid,rid并替换 data_item=replace_data(data_item) # if data_item['param'].find('${tid}')!=-1: # data_item['param']=data_item['param'].replace('${tid}', str(getattr(GetInfoData,'RID'))) params = eval(data_item['param']) #添加token params['access_token'] = GetInfoData().get_token() self.logger.info('测试数据为{0}'.format(params)) #发送请求 res = HttpRequtser().http_request(data_item['url'],params , data_item['HttpMethod'], verify=False, headers=GetInfoData().get_hander()) self.logger.info('测试结果是{0}'.format(res.json())) try: #断言 self.assertEqual(str(data_item['ExpectedResult']),str(res.json()['error_code'])) self.assertEqual(res.status_code,200) self.logger.info('用例通过') TestResult='PASS' Comment=None if data_item['description'] == '发布动态': tid = res.json()['data']['tid'] # 替换info里的tid global RID,TID,CID setattr(GetInfoData,'TID',tid) except AssertionError as e: self.logger.error('用例失败错误是{}'.format(e)) TestResult = 'Failed' Comment=res.json()['error_message'] raise e finally: DoExce(project_path.test_case_path).write_back('community',data_item['id']+1,res.json()['error_code'],TestResult,Comment) def tearDown(self): self.logger.info('用例结束')
class TestAccount(unittest.TestCase): def setUp(self): self.logger=MyLog() self.logger.info('开始测试') def tearDown(self): print('用例结束') self.logger.info('用例结束') @data(*test_data) def test_account(self,data_item): print('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) self.logger.info('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) params = eval(data_item['param']) params['access_token']=GetInfoData().get_token() print('测试数据为{0}'.format(params)) self.logger.info('测试数据为{0}'.format(params)) res = HttpRequtser().http_request(data_item['url'], params, data_item['HttpMethod'], verify=False, headers=GetInfoData().get_hander()) print('测试结果是{0}'.format(res.json())) self.logger.info('测试结果是{0}'.format(res.json())) try: self.assertEqual(str(data_item['ExpectedResult']),str(res.json()['error_code'])) self.assertEqual(res.status_code,200) print('用例通过') self.logger.info('用例通过') TestResult='PASS' Comment=None except AssertionError as e: print('用例失败错误是{}'.format(e)) self.logger.error('用例失败错误是{}'.format(e)) TestResult = 'Failed' Comment=res.json()['error_message'] raise e finally: DoExce(project_path.test_case_path).write_back('login',data_item['id']+1,res.json()['error_code'],TestResult,Comment)
class TestLogin(unittest.TestCase): def setUp(self): self.logger = MyLog() self.logger.info('开始测试') print('开始测试') @data(*test_data) def test_login(self, data_item): global TOKEN print('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) self.logger.info('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) params = eval(data_item['param']) if data_item['module'] == '登录': if params['mobilecaptcha'] == 'code': #去数据库查询验证码替换 for i in [2]: sql = 'SELECT idstring FROM `pre_captcha_mobile` WHERE mobile={0} ORDER BY captchaid DESC LIMIT 1'.format( params['phone']) code = DoMysql().do_mysql(sql) params['mobilecaptcha'] = code[0][0] else: params['access_token'] = TOKEN print('测试数据为{0}'.format(params)) self.logger.info('测试数据为{0}'.format(params)) res = HttpRequtser().http_request(data_item['url'], params, data_item['HttpMethod'], verify=False, headers=header) print('测试结果是{0}'.format(res.json())) self.logger.info('测试结果是{0}'.format(res.json())) if data_item['module'] == '登录': if res.json( )['data']['access_token']: # 任何非空数据的布尔值都为True cookies是一个类字典的格式 TOKEN = res.json()['data'][ 'access_token'] # 如果cookies不为空 就替换全局变量的COOKIES 修改全局变量 try: self.assertEqual(str(data_item['ExpectedResult']), str(res.json()['error_code'])) self.assertEqual(res.status_code, 200) print('用例通过') self.logger.info('用例通过') TestResult = 'PASS' Comment = None except AssertionError as e: print('用例失败错误是{}'.format(e)) self.logger.error('用例失败错误是{}'.format(e)) TestResult = 'Failed' Comment = res.json()['error_message'] raise e finally: DoExce(project_path.test_case_path).write_back( 'login', data_item['id'] + 1, res.json()['error_code'], TestResult, Comment) def tearDown(self): print('用例结束') self.logger.info('用例结束')
class Page: def __init__(self, driver: Chrome): self.driver = driver self.driver.maximize_window() self.logging = MyLog() # 导入日志类 def find_element_wait(self, locator, timeout=20, poll_frequency=0.5) -> WebElement: # 页面元素查找等待 try: wait = WebDriverWait(self.driver, timeout, poll_frequency) element = wait.until(ec.presence_of_element_located(locator)) except Exception as e: # 写入日志 self.logging.error('元素定位失败{}'.format(e)) self.get_screen_shot('等待元素不可见') raise e return element def click_element_wait(self, locator, timeout=20, poll_frequency=0.5) -> WebElement: try: # 等待页面元素可以点击 wait = WebDriverWait(self.driver, timeout, poll_frequency) element = wait.until(ec.element_to_be_clickable(locator)) except Exception as e: self.logging.error('元素定位失败{}'.format(e)) self.get_screen_shot('等待元素不可点击') raise e return element def current_handles(self): # 获得当前窗口数 try: handles = self.driver.window_handles except Exception as e: self.logging.error('获取窗口数失败{}'.format(e)) raise e return handles def switch_windows_wait(self, handles, timeout=20, poll_frequency=0.5): # 切换新窗口 try: wait = WebDriverWait(self.driver, timeout, poll_frequency) wait.until(ec.new_window_is_opened(handles)) self.driver.switch_to.window( self.driver.window_handles[-1]) # 切换窗口 except Exception as e: self.logging.error('切换窗口失败{}'.format(e)) raise e def switch_frame_wait(self, locator, timeout=20, poll_frequency=0.5) -> WebElement: # 切换到frame try: wait = WebDriverWait(self.driver, timeout, poll_frequency) element = wait.until( ec.frame_to_be_available_and_switch_to_it(locator)) self.driver.switch_to.default_content() except Exception as e: self.logging.error('frame切换失败{}'.format(e)) raise e return element def switch_main_page(self): # 切换回主文档,跳出所有frame try: self.driver.switch_to.default_content() except Exception as e: self.logging.error('回到主界面失败{}'.format(e)) raise e def get_screen_shot(self, name): """截图""" mkdir(img_path) self.driver.save_screenshot(img_path + '//' + name + '.png') @staticmethod def send_file(file): # 上传文件 time.sleep(2) dialog = win32gui.FindWindow("#32770", "打开") # 一级窗口 # 找到窗口 ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, "ComboBoxEx32", None) # 二级 comboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, "ComboBox", None) # 三级 edit = win32gui.FindWindowEx(comboBox, 0, 'Edit', None) # 四级 button = win32gui.FindWindowEx(dialog, 0, 'Button', None) # 四级 # 操作 time.sleep(2) win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file) # 发送文件路径 time.sleep(2) win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) # 点击打开按钮