def click(self, selector): '''点击元素''' element = self.find_element(selector) try: element.click() log1.info('点击元素成功') except BaseException: log1.error('点击元素报错', exc_info=1) self.get_img()
def test_timestamp_close_target(self): case_name = 'timestamp equal target' log1.info("Execution case:%s" % case_name) postapi = WebRequests() url = postapi.confige_get('test', 'url') payload = {'timestamp': '2017-10-9', "target": "2017-10-10"} status_code, response_json = postapi.get(url, params=payload) self.assertEqual(status_code, 200) self.assertEqual(response_json, {'before': True})
def get_proxy_ip(port): if not port: log1.info("无法获取到代理IP端口") return "", "" proxies = { 'http': "http://" + proxy_server + ':' + port, 'https': 'http://' + proxy_server + ':' + port } return proxies
def select(self, id): select1 = self.find_element(id) try: options_list = select1.find_elements_by_tag_name('option') del options_list[0] s1 = choice(options_list) Select(select1).select_by_visible_text(s1.text) log1.info("随机选的是:%s" % s1.text) except NameError as e: log1.error("Failed to click the element with %s" % e)
def inqut(self, selector, value): '''输入内容''' element = self.find_element(selector) element.clear() log1.info('清空输入内容') try: element.send_keys(value) log1.info('输入的内容:%s' % value) except BaseException: log1.error('内容输入报错', exc_info=1) self.get_img()
def get_img(self): '''截图''' path = os.path.join(getcwd.get_cwd(), 'screenshots/') # 拼接截图保存路径 rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) # 按格式获取当前时间 screen_name = path + rq + '.png' # 拼接截图文件名 try: self.driver.get_screenshot_as_file(screen_name) log1.info("截图保存成功") except BaseException: log1.error("截图失败", exc_info=1)
def get_img(self): jt_path = os.path.join(getcwd.get_cwd(), 'img/') #拼接截图保存路径 rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) #按格式获取当前时间 img_name = jt_path + rq + '.png' #拼接截图文件名 # noinspection PyBroadException try: self.driver.get_screenshot_as_file(img_name) log1.info('截图保存成功') except BaseException: log1.error('截图失败', exc_info=1)
def type(self, selector, value): """往输入框输入内容""" element = self.get_element(selector) element.clear() # noinspection PyBroadException try: element.send_keys(value) log1.info('输入的内容:%s' % value) except BaseException: log1.error('内容输入报错', exc_info=1) self.get_img()
def config_get(self,section,key,url=None): '''读取配置文件值,并返回''' config = configparser.ConfigParser() config.read(config_path,encoding="utf-8-sig") if key =='url': config_url = config.get(section,key) url = config_url + url log1.info('请求的url: %s' %url) return url else: config_get=config.get(section,key) return config_get
def test_login3(self): """密码不正确""" case_name = '密码不正确' log1.info("执行测试用例:%s" % case_name) login = LoginPage(self.driver) login.login('xuhaixiang1993', ' 12314') error_text = login.get_login_error() try: self.assertEqual(error_text, 'Incorrect username or password.') log1.info("测试用例执行成功:%s" % case_name + '\n') except AssertionError: log1.error("测试用例执行失败:%s" % case_name + '\n') raise
def test_login1(self): """用户名为空""" case_name = '用户为空' log1.info("执行测试用例:%s" % case_name) login = LoginPage(self.driver) login.login(' ', '12324') error_text = login.get_login_error() try: self.assertEqual(error_text, 'Incorrect username or password.') log1.info("测试用例执行成功:%s" % case_name + '\n') except AssertionError: log1.error("测试用例执行失败:%s" % case_name + '\n') raise
def test_login3(self): """密码不正确""" case_name = '密码不正确' log1.info("执行测试用例:%s" % case_name) login = LoginPage(self.driver) login.login('WQ68', '12314') error_text = login.get_login_error2() try: self.assertEqual(error_text, '用户名或密码错误,再输错4次该用户将被锁定15分钟。') log1.info("测试用例执行成功:%s" % case_name + '\n') except AssertionError: log1.error("测试用例执行失败:%s" % case_name + '\n') raise
def test_login2(self): """密码为空""" case_name = '密码为空' log1.info("执行测试用例:%s" % case_name) login = LoginPage(self.driver) login.login('LJDY1077', '') error_text = login.get_login_error() try: self.assertEqual(error_text, '密码不能为空。') log1.info("测试用例执行成功:%s" % case_name + '\n') except AssertionError: log1.error("测试用例执行失败:%s" % case_name + '\n') raise
def config_write(section, key=None, value=None): """往配置文件写入""" # 在section下写入key, value if key is not None and value is not None: config.set(section, key, value) log1.info('在section:%s下新增%s=%s' % (section, key, value)) with open(config_path, 'w', encoding='utf-8') as f: config.write(f) else: # 新增section config.add_section(section) log1.info("新增section:%s" % section) with open(config_path, 'w', encoding='utf-8') as f: config.write(f)
def config_delete(section, key=None): """从配置文件中删除""" # 删除section下对应key, value if key is not None: config.remove_option(section, key) log1.info('删除section:%s下%s和他的值' % (section, key)) with open(config_path, 'w', encoding='utf-8') as f: config.write(f) else: # 删除section config.remove_section(section) log1.info('删除section:%s' % section) with open(config_path, 'w', encoding='utf-8') as f: config.write(f)
def test_switch_login(self): """切换到login页面""" case_name = '切换至login页面' log1.info("执行测试用例:%s" % case_name) home = HomePage(self.driver) home.click_sign_in() home_title = home.get_title() try: # Sign in中间空格删了,验证失败后会被测试报告统计 self.assertEqual(home_title, '用户登录—华为云') log1.info("测试用例执行通过:%s" % case_name + '\n') except AssertionError: log1.error("测试用例执行失败:%s" % case_name + '\n') raise
def test_select_selenium(self): """测试搜索""" case_name = '测试搜索' log1.info("执行测试用例:%s" % case_name) home = HomePage(self.driver) home.search_for('安全组') home.my_sleep(2) title = home.get_title() try: self.assertEqual(title, '安全组 华为云_搜索结果页') log1.info("测试用例执行通过:%s" % case_name + '\n') except AssertionError: log1.error("测试用例执行失败:%s" % case_name + '\n') raise
def dict_value(self, dict1, obj, defaule=None): """查找嵌套字典中key对应的值""" try: for k, v in dict1.items(): if k == obj: log1.info('嵌套字典中%s对应的值为%s' % (obj, v)) return v else: if type(v) is dict: re = self.dict_value(v, obj, defaule) if re is not defaule: return re except BaseException: log1.error('可能有非字典嵌套', exc_info=1)
def test_login4(self): """登录成功""" # 想要执行成功,需使用GitHub账号和密码 case_name = '登录成功' log1.info("执行测试用例:%s" % case_name) login = LoginPage(self.driver) login.login('username', 'password') login_title = login.get_title() try: self.assertEqual(login_title, 'GitHub') log1.info("测试用例执行成功:%s" % case_name + '\n') except AssertionError: log1.error("测试用例执行失败:%s" % case_name + '\n') raise
def test_get_one(self): """调用github查询邮箱接口""" case_name = '查询邮箱' log1.info("执行测试用例:%s" % case_name) try: getone = Base() url = getone.config_read('test', 'url', url='/user/emails') # github用户名和密码 auth = ('username', 'password') status_code, response_json = getone.get(url, auth=auth) if status_code is 200: log1.info('测试用例执行成功:%s' % case_name) except BaseException: log1.error("测试用例执行出错:%s" % case_name, exc_info=1) raise
def examine(self,examine_url,examine_date): '''审核产品函数''' try: session = requests.Session() host = "http://admin.tjs.net" login_url = "http://admin.tjs.net/login.html" login_post_data = { "phone": "15268299107", "password": "******" } session.post(login_url, login_post_data) ress2 = session.post(examine_url, examine_date) print(ress2.content.decode('UTF-8')) except BaseException as e: log1.info('接口访问失败',exc_info=1)
def click(self, selector): """点击元素""" element = self.get_element(selector) # noinspection PyBroadException try: element.click() log1.info('点击元素成功') except BaseException: isdisplay = self.isdisplayed(element) if isdisplay is True: self.my_sleep(5) element.click() log1.info('点击元素成功') else: log1.error('点击元素报错', exc_info=1)
def config_read(self, section, key, url=None): """从配置文件中读取""" try: # key是url,拼接url返回 if key == 'url': config_url = config.get(section, key) url = config_url + url log1.info('请求的url:%s' % url) return url else: # 读取section下key的值 config_get = config.get(section, key) return config_get except BaseException: log1.error('读取配置报错', exc_info=1)
def test_post_two(self): """验证执行失败,测试报告是否会统计""" case_name = '测试失败' log1.info("执行测试用例:%s" % case_name) try: postone = Base() # url地址少了/ url = postone.config_read('test', 'url', url='user/emails') payloda = {'emails': ['*****@*****.**', '*****@*****.**']} auth = ('username', 'password') status_code, response_json = postone.post_json(url, data=payloda, auth=auth) if status_code is 201: log1.info('测试用例执行成功:%s' % case_name) except BaseException: log1.error("测试用例执行出错:%s" % case_name, exc_info=1) raise
def run(self): self.parse_url(self.login_url) sleep(w1) if not self.login(): log1.info("登录失败") self.dr.quit() return companys = self.get_companys() # companys = ["中山奥凯华泰电子有限公司"] for company in companys: self.m_apd = max_apd(company) if not self.enter_index(company): continue self.enter_detail(company) self.dr.refresh() sleep(w4) self.dr.quit()
def config_write(self, section, key=None, value=None): """往配置文件中写入""" try: # 在section下新增key = value if key is not None and value is not None: config.set(section, key, value) with open(config_path, 'w', encoding='utf-8') as f: config.write(f) log1.info('在section:%s下添加%s=%s' % (section, key, value)) else: # 新增section config.add_section(section) with open(config_path, 'w', encoding='utf-8') as f: config.write(f) log1.info('新增section:%s' % section) except BaseException: log1.error('往配置文件中写入报错', exc_info=1)
def test_post_one(self): """调用github添加邮箱接口""" case_name = '添加邮箱' log1.info("执行测试用例:%s" % case_name) try: postone = Base() url = postone.config_read('test', 'url', url='/user/emails') # 想要添加的邮箱,请求实体 payloda = {'emails': ['*****@*****.**', '*****@*****.**']} # github用户名和密码 auth = ('username', 'password') status_code, response_json = postone.post_json(url, data=payloda, auth=auth) if status_code is 201: log1.info('测试用例执行成功:%s' % case_name) except BaseException: log1.error("测试用例执行出错:%s" % case_name, exc_info=1) raise
def switch_webview(self): """ 功能:hybrid 混合型APP 在native和webview之间切换 切换当前driver为所填的contexts “webview_name” driver 驱动 context_name 需要切换的webview或native名称 """ # con2 = self.driver.contexts # print("所有contexts{}".format(con2)) try: self.driver.switch_to.context('WEBVIEW_com.shuniuyun.tjs') time.sleep(4) con = self.driver.current_context print("已切到:{}".format(con)) except BaseException as e: log1.info("切换WEBVIEW失败") raise
def switch_native(self): """ 功能:hybrid 混合型APP 在native和webview之间切换 切换当前driver为所填的contexts “webview_name” driver 驱动 context_name 需要切换的webview或native名称 """ # con = self.driver.current_context # print("当前contexts:{}".format(con)) try: self.driver.switch_to.context('NATIVE_APP') time.sleep(4) con2 = self.driver.current_context print("已切到:{}".format(con2)) except BaseException as e: log1.info("切换native失败") raise
def config_remove(self, section, key=None): """从配置文件中删除""" try: # 在section下删除key if key is not None: config.remove_option(section, key) with open(config_path, 'w', encoding='utf-8') as f: config.write(f) log1.info('在section:%s下删除键:%s' % (section, key)) else: # 删除section config.remove_section(section) with open(config_path, 'w', encoding='utf-8') as f: config.write(f) log1.info('删除section:%s' % section) except BaseException: log1.error('配置文件删除报错', exc_info=1)