def findElements(self, by, value): by_map = { 'id': By.ID, 'name': By.NAME, 'class': By.CLASS_NAME, 'tag': By.TAG_NAME, 'link': By.LINK_TEXT, 'plink': By.PARTIAL_LINK_TEXT, 'css': By.CSS_SELECTOR, 'xpath': By.XPATH } if by in by_map.keys(): try: elements = WebDriverWait( self.driver, 10, ignored_exceptions=None).until( EC.presence_of_all_elements_located( (by_map[by], value))) log1.info(by + " Query Element: " + value) return elements except NoSuchElementException: log1.error("Not Found Element Or Timeout", exc_info=1) self.getImage("Not Found Element Or Timeout") else: log1.error(by + " Variable Error", exc_info=1) self.getImage(by + " Variable Error")
def getImage(self, imageName): img = ReadConfig().getValue(section='located', name='image') try: self.driver.get_screenshot_as_file(img + imageName + ".png") log1.info("Screenshot Image") except BaseException: log1.error("Screenshot Image Fail", exc_info=1)
def open_excel(): try: excelPath = ReadConfig().getValue(section='located', name='excel') log1.info("Open Excel") return xlrd.open_workbook(excelPath) except BaseException: log1.error("Open Excel Error", exc_info=1) raise IOError("Open Excel Error")
def sendKeys(self, element, text): element.clear() log1.info("Element Clear Text") try: element.send_keys(text) log1.info("Element Input Text: " + text) except BaseException: log1.error("Not Found Element Or Input Error", exc_info=1) self.getImage("Not Found Element Or Input Error")
def __init__(self): driver = self.getBrowsers() try: log1.info("-------------------- test start --------------------") self.driver = driver log1.info("Load Web Driver Success") except Exception: log1.error("Load Web Driver Fail", exc_info=1) self.getImage("Load Web Driver Fail")
def click(self, element): try: element.click() log1.info("Element Click") except BaseException: if self.isDisplayed(element) is True: self.sleepWait(3) element.click() log1.info("Element Click") else: log1.error('Not Found Element', exc_info=1)
def send_email(new_report): # 读取测试报告中的内容作为邮件的内容 with open(new_report, 'r', encoding='utf8') as f: mail_body = f.read() log1.info( "-------------------- Found Send Email Content --------------------" ) addrPath = ReadConfig().getValue(section='email', name='username') serverPath = ReadConfig().getValue(section='email', name='server') passwordPath = ReadConfig().getValue(section='email', name='password') send_addr = addrPath reciver_addr = addrPath mail_server = serverPath now = time.strftime("%Y-%m-%d %H_%M_%S") # 邮件标题 subject = 'web自动化测试报告测试报告' + now username = addrPath password = passwordPath # 邮箱的内容和标题 message = MIMEText(mail_body, 'html', 'utf8') message['Subject'] = Header(subject, charset='utf8') log1.info( "-------------------- Get Send Email Content --------------------") # 发送邮件,使用的使smtp协议 smtp = smtplib.SMTP() smtp.connect(mail_server) smtp.login(username, password) smtp.sendmail(send_addr, reciver_addr.split(','), message.as_string()) smtp.quit() log1.info("-------------------- Send Email End --------------------")
def deselect(type, element, value=""): try: if type == "index" and value != "": Select(element).deselect_by_index(value) log1.info("Deselect Element Index") elif type == "value" and value != "": Select(element).deselect_by_value(value) log1.info("Deselect Element Value") elif type == "text" and value != "": Select(element).deselect_by_visible_text(value) log1.info("Deselect Element Text") elif type == "all" and value == "": Select(element).deselect_all() log1.info("Deselect All Element") else: log1.error('please input type', exc_info=1) except BaseException: log1.error("Not Found Select")
def acquire_report_address(reports_address): log1.info("-------------------- Send Email Start --------------------") log1.info("-------------------- Found New Report --------------------") # 测试报告文件夹中的所有文件加入到列表 test_reports_list = os.listdir(reports_address) # 按照升序排序生成新的列表 new_test_reports_list = sorted(test_reports_list) # 获取最新的测试报告 the_last_report = new_test_reports_list[-1] # 最新的测试报告地址 the_last_report_address = os.path.join(reports_address, the_last_report) log1.info("-------------------- Get New Report --------------------") return the_last_report_address
def select(type, element, value): try: if type == "index": Select(element).select_by_index(value) log1.info("Select Element Index") elif type == "value": Select(element).select_by_value(value) log1.info("Select Element Value") elif type == "text": Select(element).select_by_visible_text(value) log1.info("Select Element text") else: log1.error('please input type', exc_info=1) except BaseException: log1.error('Not Found Element', exc_info=1)
def maximizeWindow(self): self.driver.maximize_window() log1.info("Set Browser Max")
def hideWait(self, times): self.driver.implicitly_wait(times) log1.info("Set Implicitly Wait: " + str(times))
def getUrl(self, url): self.driver.get(url) log1.info("Set Url is: " + url)
def isSelect(element): log1.info("Element is Select") return element.is_selected()
def sleepWait(times): time.sleep(times) log1.info("Set Sleep Time is: " + str(times))
def getCurrentUrl(self): url = self.driver.current_url() log1.info("Get Browser Url, The Url is: " + url) return url
def acceptAlert(self): self.driver.switch_to.alert.accept() log1.info("Alert Accept")
def textAlert(self): t = str(self.driver.switch_to.alert.text) log1.info("Get Alert Text: " + t) self.acceptAlert() return t
def getTitle(self): log1.info("Get Title") return self.driver.title
def getText(element): log1.info("Get Element Text") return element.text
def getAttribute(element, attribute): log1.info("Get Element Attribute") return element.get_attribute(attribute)
def getAllSelect(element): try: log1.info("Get All Select") return Select(element).all_selected_options except BaseException: log1.error("Not Found All Select")
def clearCookies(self): self.driver.delete_all_cookies() log1.info("Clear All Cookies")
def refreshBrowser(self): self.driver.refresh() log1.info("Browser Refresh")
def dismissAlert(self): self.driver.switch_to.alert.dismiss() log1.info("Alert Dismiss")
def isDisplayed(element): is_display = element.is_displayed() log1.info("Element displayed is: " + is_display) return is_display
def quitBrowser(self): self.sleepWait(3) self.driver.quit() log1.info("Quit Browser") log1.info("-------------------- test end --------------------")
def sendKeysAlert(self, text): self.driver.switch_to.alert.send_keys(text) log1.info("Input Alert Text: " + text) self.acceptAlert()
def closeBrowser(self): self.sleepWait(3) self.driver.close() log1.info("Close Browser") log1.info("-------------------- test end --------------------")