def scroll_text_click(self, text_value): self.driver.find_element_by_android_uiautomator( 'new UiScrollable(' 'new UiSelector().scrollable(true).instance(0))' '.scrollIntoView(' 'new UiSelector().text("' + text_value + '").instance(0));').click() MyLog.info("找到text:{}".format(text_value))
def get_element_text(self, locator): element = self.wait_element_visibility(locator) try: MyLog.info("获取:{}元素文本:{}".format(locator, element.text)) return element.text except Exception as e: MyLog.error("获取元素文本:{}失败".format(element.text)) self.save_image() raise e
def input_text(self, locator, value): element = self.wait_element_visibility(locator) try: MyLog.info("点击:{},输入:{}".format(locator, value)) element.send_keys(value) except Exception as e: MyLog.error("输入数据:{}失败".format(value)) self.save_image() raise e
def click_element(self, locator): element = self.wait_element_visibility(locator) try: MyLog.info("点击元素:{}".format(locator)) element.click() except Exception as e: MyLog.error("点击元素:{}失败".format(locator)) self.save_image() raise e
def wait_element_visibility(self, locator) -> WebElement: MyLog.info("等待元素:{}".format(locator)) try: element = WebDriverWait(self.driver, 20).until( expected_conditions.presence_of_element_located(locator)) return element except Exception as e: MyLog.error("等待元素:{}超时".format(locator)) self.save_image() raise e
def long_click(self, locator): action = TouchAction(self.driver) element = self.wait_element_visibility(locator) try: MyLog.info("长按:{}元素".format(locator)) action.long_press(element).wait(10000).perform() except Exception as e: MyLog.error("长按元素:{}失败".format(locator)) self.save_image() raise e
def get_toast_msg(self, locator): #return (By.XPATH, "//*[@class='android.widget.Toast']") #return (By.XPATH, "//*[@text='审核成功']") toast_msg = self.wait_element_visibility(locator) try: MyLog.info("获取toast元素:{},文本:{}".format(locator, toast_msg.text)) return toast_msg.text except Exception as e: MyLog.error("获取toast元素文本:{}失败".format(toast_msg.text)) self.save_image() raise e
def save_image(self): try: base_dir = os.path.dirname(os.path.dirname(__file__)) screenshot_dir = os.path.join(base_dir, "image") screenshot_img = os.path.join(screenshot_dir, str(int(time.time())) + ".png") self.driver.get_screenshot_as_file(screenshot_img) MyLog.info('保存截图至:{}'.format(screenshot_img)) except NameError as e: MyLog.info('保存截图失败:%s' % e) self.save_image() return screenshot_img
def ini_request(case_dict, relevance, _path, result): """ 用例前提条件执行,提取关联键 :param case_dict: 用例对象 :param relevance: 关联对象 :param _path: case路径 :param result: 全局结果 :return: """ if isinstance(case_dict["premise"], list): logging.info("执行测试用例前置接口") with allure.step("接口关联请求"): for i in case_dict["premise"]: relevance_list = relevance.copy() for j in range(0, 3): # 获取前置接口关联数据失败 code, data = send_request( i, case_dict["testinfo"].get("host"), case_dict["testinfo"].get("address"), relevance_list, _path, result) if not data: with allure.step("接口请求失败!等待三秒后重试!"): pass logging.info("接口请求失败!等待三秒后重试!") continue if i["relevance"]: if len(i["relevance"]): relevance = get_relevance(data, i["relevance"], relevance) if isinstance(relevance, bool): with allure.step("从结果中提取关联键的值失败!等待3秒后重试!"): pass logging.info("从结果中提取关联键的值失败!等待3秒后重试!") time.sleep(3) continue else: break else: break else: break if isinstance(relevance, bool): logging.info("从结果中提取关联键的值失败!重试三次失败") result["result"] = False raise failureException("获取前置接口关联数据失败") return relevance
def __init__(self): logging.info("初始化关联文件") self.data = ConfRelevance(CONF_PATH,"env").get_relevance_conf()
def send_request(data, host, address, port, relevance, _path, success): """ 再次封装请求 :param data: 测试用例 :param host: 测试地址 :param address: 接口地址 :param relevance: 关联对象 :param port: 端口地址 :param _path: case路径 :param success: 全局结果 :return: """ # logging = logdir.MyLog() config = conf.Config() logging.info("=" * 100) header = read_param.read_param(data["test_name"], data["headers"], relevance, _path, success) # 处理请求头 logging.debug("请求头处理结果: %s" % header) parameter = read_param.read_param(data["test_name"], data["parameter"], relevance, _path, success) # 处理请求参数 logging.debug("请求参数处理结果: %s" % header) try: # 如果用例中写了host和address,则使用用例中的host和address,若没有则使用全局的 host = data["host"] except KeyError: pass try: address = data["address"] except KeyError: pass host = config.host # host处理,读取配置文件中的host address = param_manage.manage(address, relevance) logging.debug("host处理结果: %s" % host) if not host: raise failureException("接口请求地址为空 %s" % data["headers"]) logging.info("请求接口:%s" % str(data["test_name"])) logging.info("请求地址:%s" % data["http_type"] + "://" + host + ":" + port + address) logging.info("请求头: %s" % str(header)) logging.info("请求参数: %s" % str(parameter)) if data["request_type"].lower() == 'post': if data["file"]: with allure.step("POST上传文件"): allure.attach("请求接口:", str(data["test_name"])) allure.attach( "请求地址", data["http_type"] + "://" + host + ":" + port + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = config_http.post( header=header, address=data["http_type"] + "://" + host + ":" + port + address, request_parameter_type=data["parameter_type"], files=parameter, timeout=data["timeout"]) else: with allure.step("POST请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach( "请求地址", data["http_type"] + "://" + host + ":" + port + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("POST请求接口") result = config_http.post( header=header, address=data["http_type"] + "://" + host + ":" + port + address, request_parameter_type=data["parameter_type"], data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == 'get': with allure.step("GET请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach( "请求地址", data["http_type"] + "://" + host + ":" + port + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("GET请求接口") result = config_http.get(header=header, address=data["http_type"] + "://" + host + ":" + port + address, data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == "put": if data["file"]: with allure.step("PUT上传文件"): allure.attach("请求接口:", str(data["test_name"])) allure.attach( "请求地址", data["http_type"] + "://" + host + ":" + port + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("PUT上传文件") result = config_http.put( header=header, address=data["http_type"] + "://" + host + ":" + port + address, request_parameter_type=data["parameter_type"], files=parameter, timeout=data["timeout"]) else: with allure.step("PUT请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach( "请求地址", data["http_type"] + "://" + host + ":" + port + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("PUT请求接口") result = config_http.put( header=header, address=data["http_type"] + "://" + host + ":" + port + address, request_parameter_type=data["parameter_type"], data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == "delete": with allure.step("DELETE请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach( "请求地址", data["http_type"] + "://" + host + ":" + port + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("DELETE请求接口") result = config_http.delete(header=header, address=data["http_type"] + "://" + host + ":" + port + address, data=parameter, timeout=data["timeout"]) else: result = {"code": False, "data": False} logging.info("接口请求结果:\n %s" % str(result)) return result
def send_request(data, host, address, _path, relevance=None): """ 封装请求 :param data: 测试用例 :param host: 测试host :param address: 接口地址 :param relevance: 关联对象 :param _path: case路径 :return: """ logging.info("=" * 100) header = readParameter.read_param(data["test_name"], data["headers"], _path, relevance) if data["cookies"] is True: header["Cookie"] = initializeCookie.ini_cookie() logging.debug("请求头处理结果:%s" % header) parameter = readParameter.read_param(data["test_name"], data["parameter"], _path, relevance) logging.debug("请求参数处理结果:%s" % parameter) try: host = data["host"] except KeyError: pass try: address = data["address"] except KeyError: pass host = confManage.host_manage(host) address = replaceRelevance.replace(address, relevance) logging.debug("host处理结果: %s" % host) if not host: raise Exception("接口请求地址为空 %s" % data["headers"]) logging.info("请求接口:%s" % str(data["test_name"])) logging.info("请求地址:%s" % data["http_type"] + "://" + host + address) logging.info("请求头: %s" % str(header)) logging.info("请求参数: %s" % str(parameter)) if data["test_name"] == 'password正确': with allure.step("保存cookie信息"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) api_method.save_cookie(header=header, address=data["http_type"] + "://" + host + address, data=parameter) if data["request_type"].lower() == 'post': logging.info("请求方法: POST") if data["file"]: with allure.step("POST上传文件"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = api_method.post( header=header, address=data["http_type"] + "://" + host + address, request_parameter_type=data["parameter_type"], files=parameter, timeout=data["timeout"]) else: with allure.step("POST请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = api_method.post( header=header, address=data["http_type"] + "://" + host + address, request_parameter_type=data["parameter_type"], data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == 'get': with allure.step("GET请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("请求方法: GET") result = api_method.get(header=header, address=data["http_type"] + "://" + host + address, data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == 'put': logging.info("请求方法: PUT") if data["file"]: with allure.step("PUT上传文件"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = api_method.post( header=header, address=data["http_type"] + "://" + host + address, request_parameter_type=data["parameter_type"], files=parameter, timeout=data["timeout"]) else: with allure.step("PUT请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) result = api_method.post( header=header, address=data["http_type"] + "://" + host + address, request_parameter_type=data["parameter_type"], data=parameter, timeout=data["timeout"]) elif data["request_type"].lower() == 'delete': with allure.step("DELETE请求接口"): allure.attach("请求接口:", str(data["test_name"])) allure.attach("请求地址", data["http_type"] + "://" + host + address) allure.attach("请求头", str(header)) allure.attach("请求参数", str(parameter)) logging.info("请求方法: DELETE") result = api_method.get(header=header, address=data["http_type"] + "://" + host + address, data=parameter, timeout=data["timeout"]) else: result = {"code": False, "data": False} logging.info("请求接口结果:\n %s" % str(result)) return result
def __init__(self, _path, title): logging.info("初始化关联文件") config = configparser.ConfigParser() config.read(_path, encoding="utf-8") self.host = config[title]