def test_02_password_error(self): self.login.login(username=self.exl.get_cell(2, 2), password=self.exl.get_cell(2, 3)) self.assertEqual(self.exl.get_cell(2, 4), self.login.login_failed_msg()) AutoLog.set_messge("case02 complete", "info")
def test_01_success(self): self.login.login(username=self.exl.get_cell(1, 2), password=self.exl.get_cell(1, 3)) self.assertEqual(self.exl.get_cell(1, 4), self.login.login_success_msg()) AutoLog.set_messge("case01 complete", "info")
def alert_info(self): try: text = self.driver.switch_to.alert.text AutoLog.set_messge("Alert:{}".format(text), "info") return text except Exception as e: AutoLog.set_messge(e, "error")
def quit(cls): try: time.sleep(4) UseBrowser.driver.quit() AutoLog.set_messge("quit!", "info") except Exception as e: AutoLog.set_messge(e, "error")
def send_keys_id_js(self, name, value): try: js = "document.getElementById('{}').value='{}'".format(name, value) self.driver.execute_script(js) AutoLog.set_messge("send_js:{} 添加:{} 成功".format(name, value), "info") except Exception as e: AutoLog.set_messge(e, "error")
def swich_frame_default(self): try: time.sleep(1) self.driver.switch_to.default_content() AutoLog.set_messge("frame切换到default成功!", "info") time.sleep(1) except Exception as e: AutoLog.set_messge(e, "error")
def swich_frame(self, name): try: time.sleep(1) self.driver.switch_to.frame(name) AutoLog.set_messge("frame:{} 切换成功!".format(name), "info") time.sleep(1) except Exception as e: AutoLog.set_messge(e, "error")
def get_title_xpath(self): try: time.sleep(1) title = self.driver.title AutoLog.set_messge("title:{}".format(title), "info") return title except Exception as e: AutoLog.set_messge(e, "error")
def select_xpath_visible_text(self, xpath, text): try: Select(self.driver.find_element_by_xpath( xpath)).select_by_visible_text(text) AutoLog.set_messge( "select_xpth:{} text:{} 选择成功".format(xpath, text), "info") except Exception as e: AutoLog.set_messge(e, "error")
def get_conn(self): try: conn = pymysql.Connection(host=self.host, user=self.user, password=self.password, database=self.database, port=self.port, charset=self.charset) AutoLog.set_messge("database:{} 连接".format(self.database), "info") return conn except Exception as e: AutoLog.set_messge(e, "error")
class YamlOperation(): def __init__(self, locator_file): self.log = AutoLog() with open(locator_file, encoding="UTF-8") as yaml_file: self.data = yaml.load(yaml_file, yaml.FullLoader) def get_locatoer(self, page, local): try: local = self.data[page][local] self.log.set_messge("yaml:{} 读取成功".format(local), "info") return local except Exception as e: self.log.set_messge(e, "error")
def findall(self, sql): conn = self.get_conn() try: cur = conn.cursor(cursor=pymysql.cursors.DictCursor) cur.execute(sql) result = cur.fetchall() AutoLog.set_messge("sql:{} 执行".format(sql), "info") except Exception as e: AutoLog.set_messge(e, "error") conn.rollback() finally: cur.close() conn.close() return result
def update(self, sql): conn = self.get_conn() try: # sql的执行 cur = conn.cursor(cursor=pymysql.cursors.DictCursor) cur.execute(sql) conn.commit() AutoLog.set_messge("sql:{} 执行".format(sql), "info") except Exception as e: conn.rollback() AutoLog.set_messge(e, "error") finally: cur.close() conn.close()
def click_xpath(self, xpath): try: self.driver.find_element_by_xpath(xpath).click() AutoLog.set_messge("xpth:{} 点击成功".format(xpath), "info") except Exception as e: AutoLog.set_messge(e, "error")
def send_keys_xpath(self, xpath, value): try: self.driver.find_element_by_xpath(xpath).send_keys(value) AutoLog.set_messge("xpth:{} 添加:{} 成功".format(xpath, value), "info") except Exception as e: AutoLog.set_messge(e, "error")
def __init__(self, dr): self.dr = dr self.log = AutoLog()
def tearDown(self) -> None: self.login.ub.quit() AutoLog.set_messge("crm quite", "info")
def __init__(self, locator_file): self.log = AutoLog() with open(locator_file, encoding="UTF-8") as yaml_file: self.data = yaml.load(yaml_file, yaml.FullLoader)
def open_url(self, url): try: self.driver.get(url) AutoLog.set_messge("url:{} 打开!".format(url), "info") except Exception as e: AutoLog.set_messge(e, "error")
class AutoDef(): def __init__(self, dr): self.dr = dr self.log = AutoLog() def swich_frame(self, name): try: time.sleep(1) self.dr.switch_to.frame(name) self.log.set_messge("frame:{} 切换成功!".format(name), "info") time.sleep(1) except Exception as e: self.log.set_messge(e, "error") def get_title_xpath(self): try: time.sleep(1) title = self.dr.title self.log.set_messge("title:{}".format(title), "info") return title except Exception as e: self.log.set_messge(e, "error") def swich_frame_default(self): try: time.sleep(1) self.dr.switch_to.default_content() self.log.set_messge("frame切换到default成功!", "info") time.sleep(1) except Exception as e: self.log.set_messge(e, "error") def send_keys_xpath(self, xpath, value): try: self.dr.find_element_by_xpath(xpath).send_keys(value) self.log.set_messge("xpth:{} 添加:{} 成功".format(xpath, value), "info") except Exception as e: self.log.set_messge(e, "error") def send_keys_id_js(self, name, value): try: js = "document.getElementById('{}').value='{}'".format(name, value) self.dr.execute_script(js) self.log.set_messge("send_js:{} 添加:{} 成功".format(name, value), "info") except Exception as e: self.log.set_messge(e, "error") def click_xpath(self, xpath): try: self.dr.find_element_by_xpath(xpath).click() self.log.set_messge("xpth:{} 点击成功".format(xpath), "info") except Exception as e: self.log.set_messge(e, "error") def select_xpath_visible_text(self, xpath, text): try: Select(self.dr.find_element_by_xpath( xpath)).select_by_visible_text(text) self.log.set_messge( "select_xpth:{} text:{} 选择成功".format(xpath, text), "info") except Exception as e: self.log.set_messge(e, "error") def alert_info(self): try: text = self.dr.switch_to.alert.text self.log.set_messge("Alert:{}".format(text), "info") return text except Exception as e: self.log.set_messge(e, "error")
def test_05_username_password_null(self): self.login.login(username=self.exl.get_cell(5, 2), password=self.exl.get_cell(5, 3)) self.assertEqual(self.exl.get_cell(5, 4), self.login.login_failed_msg()) AutoLog.set_messge("case05 complete", "info")