def refresh(self): logging.info("刷新浏览器") self.driver.refresh()
def input(self): selector, selector_location = self.selector_path(self.actions[1]) logging.info("定位元素:" + selector_location + "--正在输入" + self.actions[2]) self.action_element(selector, selector_location).send_keys(self.actions[2])
def quit(self): logging.info("退出浏览器") self.driver.quit()
def click(self): selector, selector_location = self.selector_path(self.actions[1]) logging.info("正在点击" + selector_location) self.action_element(selector, selector_location).click()
def open(self): logging.info("正在打开:" + self.actions[1]) self.driver.get(self.actions[1])
class testapi(RunMethod): def __init__(self): self.base_url = 'http://www.httpbin.org' self.headers = json.loads(test_case[0]["headers"]) self.variate_dict = {"wo": "test is 大帅哥", "channel": "111111111"} # 定义变量获取 def variate(self, name, response=None): if name: try: site, key, value = re.split(r"[::]=", name) # 请求头部添加变量信息 if site == "header": self.headers[key] = value # 提取response中的变量 if site == "response-data": pattern = re.findall( value, json.dumps(response.json(), ensure_ascii=False)) if pattern: self.variate_dict.update(key=pattern) except: logging.error(name + " 变量定义失败 ") # 变量替换 def judge(self, name): pattern = re.findall("{{(.*?)}}", name) if pattern: for s in pattern: name = re.sub(r"{{" + s + "}}", self.variate_dict[s], name) logging.info(s + " 变量替换为" + self.variate_dict[s]) return name.encode() else: logging.info("暂无变量替换") return name # 断言判断 def api_assert(self, r, eqs): RESULT = [] eqs == int(eqs) if type(eqs) is float else eqs eq = str(eqs).split(",") for e in eq: # 判断response返回码 if e.isdigit() and len(str(e)) == 3: if r.status_code == e: RESULT.append(True) else: RESULT.append(False) # 完全等于 elif ":=" in e: e = e.strip(":=") if r.text == e: RESULT.append(True) else: RESULT.append(False) # 包含 else: if e in r.text: RESULT.append(True) else: RESULT.append(False) return RESULT def one_test(self, case, test_case): # 头部变量信息获取 self.variate(case["variate"]) # 判断data中是否需要将变量替换 case["data"] = self.judge(case["data"]) logging.info(case["data"]) r = self.run_request(case["method"], self.base_url + case["url"], data=case["data"], header=self.headers) logging.info(r.json()) # 采集变量信息 self.variate(case["variate"], r) # 断言 case_index = test_case.index(case) case["eq_result"] = self.api_assert(r, case["eq"]) # 断言后excel更新断言状态 logging.info("当前更新第" + str(case_index + 1) + "条用例 eq_result") api_excel.api_update_data(case_index + 2, 13, str(case["eq_result"])) # 将断言状态信息返回 return case["eq_result"]
def wait_text(self, xpath, name): logging.info("正在等待元素" + xpath + ":" + name) WebDriverWait(self.driver, 3).until( EC.text_to_be_present_in_element((By.XPATH, xpath), name))
def setUpClass(cls): cls.driver = browser() logging.info("打开浏览器")
def tearDownClass(cls): # if "end" not in cls.name: logging.info("用例正在结束:" + cls.name) logging.info("关闭浏览器") cls.driver.quit()
def tearDown(self): self._testMethodName = self._testname self._testMethodDoc = self._testid logging.info("本轮用例执行完毕,开始下一条用例")
def setUp(self): logging.info("-----------------------------------------------") logging.info("本轮用例开始执行")
def setUpClass(cls): logging.info("setUpClass")
def tearDownClass(cls): logging.info("tearDownClass")
def setUp(self): logging.info("setUp")