Ejemplo n.º 1
0
 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")
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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")
Ejemplo n.º 4
0
 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")
Ejemplo n.º 5
0
 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")
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
    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 --------------------")
Ejemplo n.º 8
0
 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")
Ejemplo n.º 9
0
 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
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
0
 def maximizeWindow(self):
     self.driver.maximize_window()
     log1.info("Set Browser Max")
Ejemplo n.º 12
0
 def hideWait(self, times):
     self.driver.implicitly_wait(times)
     log1.info("Set Implicitly Wait: " + str(times))
Ejemplo n.º 13
0
 def getUrl(self, url):
     self.driver.get(url)
     log1.info("Set Url is: " + url)
Ejemplo n.º 14
0
 def isSelect(element):
     log1.info("Element is Select")
     return element.is_selected()
Ejemplo n.º 15
0
 def sleepWait(times):
     time.sleep(times)
     log1.info("Set Sleep Time is: " + str(times))
Ejemplo n.º 16
0
 def getCurrentUrl(self):
     url = self.driver.current_url()
     log1.info("Get Browser Url, The Url is: " + url)
     return url
Ejemplo n.º 17
0
 def acceptAlert(self):
     self.driver.switch_to.alert.accept()
     log1.info("Alert Accept")
Ejemplo n.º 18
0
 def textAlert(self):
     t = str(self.driver.switch_to.alert.text)
     log1.info("Get Alert Text: " + t)
     self.acceptAlert()
     return t
Ejemplo n.º 19
0
 def getTitle(self):
     log1.info("Get Title")
     return self.driver.title
Ejemplo n.º 20
0
 def getText(element):
     log1.info("Get Element Text")
     return element.text
Ejemplo n.º 21
0
 def getAttribute(element, attribute):
     log1.info("Get Element Attribute")
     return element.get_attribute(attribute)
Ejemplo n.º 22
0
 def getAllSelect(element):
     try:
         log1.info("Get All Select")
         return Select(element).all_selected_options
     except BaseException:
         log1.error("Not Found All Select")
Ejemplo n.º 23
0
 def clearCookies(self):
     self.driver.delete_all_cookies()
     log1.info("Clear All Cookies")
Ejemplo n.º 24
0
 def refreshBrowser(self):
     self.driver.refresh()
     log1.info("Browser Refresh")
Ejemplo n.º 25
0
 def dismissAlert(self):
     self.driver.switch_to.alert.dismiss()
     log1.info("Alert Dismiss")
Ejemplo n.º 26
0
 def isDisplayed(element):
     is_display = element.is_displayed()
     log1.info("Element displayed is: " + is_display)
     return is_display
Ejemplo n.º 27
0
 def quitBrowser(self):
     self.sleepWait(3)
     self.driver.quit()
     log1.info("Quit Browser")
     log1.info("-------------------- test end --------------------")
Ejemplo n.º 28
0
 def sendKeysAlert(self, text):
     self.driver.switch_to.alert.send_keys(text)
     log1.info("Input Alert Text: " + text)
     self.acceptAlert()
Ejemplo n.º 29
0
 def closeBrowser(self):
     self.sleepWait(3)
     self.driver.close()
     log1.info("Close Browser")
     log1.info("-------------------- test end --------------------")