def handle_element(self, element, optype, content): if optype == "click": element.click() if optype == "fill": element.clear() element.send_keys(content) if optype == "select": Select(element).select_by_visible_text(content) logger("INFO", "find element successfully in page")
def get_tab_element(self, tab_name): element = None a = self.driver.find_elements_by_tag_name("a") tds = self.driver.find_elements_by_tag_name("td") elemlist = a + tds for i in elemlist: if re.search(re.compile(tab_name), i.text): element = i logger("DEBUG", "find element successfully ") break if element: return element.is_displayed() return element
def handle_switch(self, element): parameter = element[0] wait_time = element[2] if parameter.find('default') != -1: self.driver.switch_to.default_content() self.current_frame = "default" logger("INFO", "Now switch to default frame") else: for frame in parameter.split('/'): self.driver.switch_to.frame(frame) self.current_frame = frame logger("INFO", "Now switch to frame " + frame) time.sleep(wait_time)
def login(driver, username, password): # 登录操作 logger('INFO', "start to login browser") driver.get(url) time.sleep(3) element_username = driver.find_element_by_id("uid") element_password = driver.find_element_by_id("password") element_username.clear() element_username.send_keys(username) element_password.clear() element_password.send_keys(password) driver.find_element_by_xpath("//*[@type='button']").click() time.sleep(3) logger('INFO', "login browser success")
def check_value(self, element): parameter = element[0] element_name_id = element[1] wait_time = element[2] test_data = element[3] if parameter.find('参数') != -1: parameter = parameter.replace('参数', test_data.split("检查:")[1].split("&")[0]) if parameter.find(u"不存在") != -1: val = parameter.split(u"不存在:") for v in val[1].split(","): result = self.get_tab_element(v) time.sleep(wait_time) get_img(self.driver, self.nowtime, self.CaseName) if result: logger("INFO", "check failed") self.assertTrue(False, msg="%s is exist" % v) else: logger("INFO", "check successfully") self.assertTrue(True) elif parameter.find(u"存在") != -1: val = parameter.split(u"存在:") for v in val[1].split(","): result = self.get_tab_element(v) time.sleep(wait_time) get_img(self.driver, self.nowtime, self.CaseName) if result: logger("INFO", "check successfully") self.assertTrue(True) else: logger("INFO", "check failed") self.assertTrue(False, msg="%s is not exist" % v)
def ui_engine(self, case_id, case_name, data_id): test_data = '' if data_id == '0': result = self.getCaseStepIDs(case_id) else: result = self.getDataDrivenCaseStepIDs(case_id) test_data = self.getTestData(data_id) lenghth = len(result) result1 = result for i in range(lenghth): step_id = result1[i][0] result = self.getCaseStep(step_id) action = result[0] element = result[1], result[2], result[3], test_data logger("INFO", "CaseStep is %s" % result[4]) # 点击操作 if action == "点击": self.handle_click(element) # 输入操作 elif action == "填写": self.handle_fill(element) # 检查操作 elif action == "检查": self.check_value(element) # 选择操作 elif action == "选择": self.handle_choose(element) # 上传操作 elif action == "上传": self.handle_fill(element) # 切换操作 elif action == "切换": self.handle_switch(element) else: logger("ERROR", "Sync Error, %s action is not correct!" % action) assert False, "Sync Error, %s action is not correct!" % action
def tearDownClass(self): self.driver.quit() logger('INFO', "login out browser")
def get_allelement(self, element_name_id, optype=None, content=None): logger("INFO", "Star to find element %s from database" % element_name_id) result = self.get_element(element_name_id) element_name = result[0] access_method = result[1] access_path = result[2] frame_name = result[3] logger("INFO", "element path is " + access_method + ':' + access_path) if frame_name.find('default') != -1: self.driver.switch_to.default_content() self.current_frame = "default" logger("INFO", "Now switch to default frame") else: for frame in frame_name.split('/'): if self.current_frame == frame: pass else: try: self.driver.switch_to.frame(frame) self.current_frame = frame logger("INFO", "Now switch to frame " + frame) except: self.driver.switch_to.default_content() self.driver.switch_to.frame(frame) self.current_frame = frame logger("INFO", "Now switch to frame " + frame) for i in range(wait_time): logger("DEBUG", "consume %d seconds already" % i) time.sleep(1) try: element = self.driver.find_element_by_link_text(element_name) self.handle_element(element, optype, content) return element except: if access_method == "id": element = self.driver.find_element_by_id(access_path) self.handle_element(element, optype, content) return element if access_method == "xpath": element = self.driver.find_element_by_xpath(access_path) self.handle_element(element, optype, content) return element logger("WARN", "failed to find element in page") return None
def click_button(self, element_name_id): result = self.get_allelement(element_name_id, optype="click") if result is None: get_img(self.driver, self.nowtime, self.CaseName) logger("ERROR", "Element %s is not found!" % element_name_id) assert False, "Element %s is not found!" % element_name_id
def select_element(self, label, content): result = self.get_allelement(label, "select", content) if result is None: get_img(self.driver, self.nowtime, self.CaseName) logger("ERROR", "Element %s is not found!" % label) assert False, "Element %s is not found!" % label