def run_cases(self, data: list) -> None: """ run list case :param data: :return: """ if isinstance(data, list) is False: raise TypeError("Use cases must be lists.") if len(data) == 0: log.error("There are no use cases to execute") return suit = unittest.TestSuite() for suits in self.TestSuits: for cases in suits: for case in cases: file_name = case.__module__ class_name = case.__class__.__name__ method_name = str(case).split(" ")[0] ret = self._diff_case(file_name, class_name, method_name, data) if ret is True: suit.addTest(case) self.run(suit) self.close_browser()
def sender(self, to=None, subject=None, contents=None, attachments=None): if to is None: raise ValueError("Please specify the email address to send") if isinstance(to, str): to = [to] if isinstance(to, list) is False: raise ValueError("Received mail type error") if subject is None: subject = 'Unit Test Report' if contents is None: contents = env.get_template('mail.html').render( mail_pass=str(RunResult.passed), mail_fail=str(RunResult.failed), mail_error=str(RunResult.errors), mail_skip=str(RunResult.skipped)) msg = MIMEMultipart() msg['Subject'] = Header(subject, 'utf-8') msg['From'] = self.user msg['To'] = ",".join(to) text = MIMEText(contents, 'html', 'utf-8') msg.attach(text) if attachments is None: attachments = BrowserConfig.REPORT_PATH att_name = "report.html" if "\\" in attachments: att_name = attachments.split("\\")[-1] if "/" in attachments: att_name = attachments.split("/")[-1] att = MIMEApplication(open(attachments, 'rb').read()) att['Content-Type'] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="{}"'.format( att_name) msg.attach(att) smtp = smtplib.SMTP_SSL(self.host, self.port) try: smtp.login(self.user, self.password) smtp.sendmail(self.user, to, msg.as_string()) log.info(" 📧 Email sent successfully!!") except BaseException as msg: log.error('❌ Email failed to send!!' + msg.__str__()) finally: smtp.quit()
def addFailure(self, test, err): self.failure_count += 1 self.status = 1 TestResult.addFailure(self, test, err) _, _exc_str = self.failures[-1] output = self.complete_output() self.result.append((1, test, output, _exc_str)) if Seldom.driver is not None: try: test.imgs.append(Seldom.driver.get_screenshot_as_base64()) except BaseException as msg: log.error(msg) if self.verbosity > 1: sys.stderr.write('F ') sys.stderr.write(str(test)) sys.stderr.write('\n') else: sys.stderr.write('F')
def find_element(elem): """ Find if the element exists. """ for i in range(Seldom.timeout): elems = Seldom.driver.find_elements(by=elem[0], value=elem[1]) if len(elems) == 1: log.info("Find element: {by}={value} ".format(by=elem[0], value=elem[1])) break elif len(elems) > 1: log.warn("Find {n} elements through:{by}={value}".format( n=len(elems), by=elem[0], value=elem[1])) break else: time.sleep(1) else: log.error("Find 0 elements through:{by}={value}".format(by=elem[0], value=elem[1]))
def assertTitle(self, title=None): """ Asserts whether the current title is in line with expectations. Usage: self.assertTitle("title") """ if title is None: raise AssertionError("The assertion title cannot be empty.") for _ in range(Seldom.timeout): try: self.assertEqual(title, self.get_title) log.info("assert title: {title}.".format(title=self.get_title)) break except AssertionError: sleep(1) else: log.error("assert fail: {title}.".format(title=title)) self.assertEqual(title, self.get_title)
def assertInUrl(self, url=None): """ Asserts whether the current URL is in line with expectations. Usage: self.assertInUrl("url") """ if url is None: raise AssertionError("The assertion URL cannot be empty.") for _ in range(Seldom.timeout): try: self.assertIn(url, self.get_url) log.info("assertIn url: {url}.".format(url=self.get_url)) break except AssertionError: sleep(1) else: log.error("assertIn fail: {url}.".format(url=url)) self.assertIn(url, self.get_url)
def osSystem(path=''): os_system = platform.system() if os_system == 'Windows': log.info('当前操作系统:Windows') pathWin = path + r'Browser_Driver/chromedriver84(win32).exe' return pathWin elif os_system == 'Darwin' or \ os_system == 'darwin' or \ os_system == 'Mac' or \ os_system == 'mac' or \ os_system == 'OS X': log.info('当前操作系统:Mac OS') pathMac = path + r'Browser_Driver/chromedriver84(Mac64)' return pathMac else: log.error("当前操作系统 " + os_system) raise SystemExit( log.error( ' ❌ The current operating system environment is not Windows or Mac, ' 'please confirm the current environment!!'))
def osSystem(path=''): os_system = platform.system() driver = automatic() if os_system == 'Windows': log.info('当前操作系统:Windows') driver.Contrast_win(path=path) # 检查对比Windows系统环境下的Chromedriver pathWin = path + r'Browser_Driver/chromedriver.exe' return pathWin elif os_system == 'Darwin' or \ os_system == 'darwin' or \ os_system == 'Mac' or \ os_system == 'mac' or \ os_system == 'OS X': log.info('当前操作系统:Mac OS') driver.Contrast_mac(path=path) # 检查对比Mac系统环境下的Chromedriver pathMac = path + r'Browser_Driver/chromedriver' return pathMac else: log.error("当前操作系统 " + os_system) raise SystemExit(log.error(' ❌ The current operating system environment is not Windows or Mac, ' 'please confirm the current environment!!'))
def find_element(elem): """ Find if the element exists. """ for i in range(Seldom.timeout): elems = Seldom.driver.find_elements(by=elem[0], value=elem[1]) if len(elems) == 1: log.info("✅ Find element: {by}={value} ".format( by=elem[0], value=elem[1])) break elif len(elems) > 1: log.info("❓ Find {n} elements through: {by}={value}".format( n=len(elems), by=elem[0], value=elem[1])) break else: time.sleep(1) else: error_msg = "❌ Find 0 elements through: {by}={value}".format( by=elem[0], value=elem[1]) log.error(error_msg) raise NotFindElementError(error_msg)
def sender(self, to=None, subject=None, contents=None, attachments=None): if to is None: raise ValueError("Please specify the email address to send") if subject is None: subject = 'Seldom Test Report' if contents is None: contents = """ <table width="50%" cellpadding="0" cellspacing="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;display:inline-block;font-size:14px;overflow:hidden;border-radius:7px;background-color:##f5f9fc;margin:0;border:1px solid #e9e9e9" bgcolor="#f5f9fc"> <tbody> <tr style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;margin:0"> <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:16px;vertical-align:top;color:#fff;font-weight:500;text-align:center;border-radius:3px 3px 0 0;background-color:#47bac1;margin:0;padding:20px" align="center" bgcolor="#354052" valign="top"> <span style="margin-top:20px;display:block"> Seldom Test Report </span> </td> </tr> <tr style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;margin:0"> <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;vertical-align:top;margin:0;padding:20px" valign="top"> <font color="#888888"> </font> <font color="#888888"> </font> <table width="100%" cellpadding="0" cellspacing="0" style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;margin:0"> <tbody> <tr style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;margin:0"> <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;vertical-align:top;margin:0;padding:0 0 20px" valign="top"> 通过用例: {mail_pass} </td> </tr> <tr style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;margin:0"> <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;vertical-align:top;margin:0;padding:0 0 20px" valign="top"> 失败用例: {mail_fail} </td> </tr> <tr style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;margin:0"> <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;vertical-align:top;margin:0;padding:0 0 20px" valign="top"> 错误用例: {mail_error} </td> </tr> <tr style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;margin:0"> <td style="font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;box-sizing:border-box;font-size:14px;vertical-align:top;margin:0;padding:0 0 20px" valign="top"> 跳过用例: {mail_skip} </td> </tr> </tbody> </table> <font color="#888888"> </font> </td> </tr> </tbody> </table> """.format(mail_pass=str(RunResult.passed), mail_fail=str(RunResult.failed), mail_error=str(RunResult.errors), mail_skip=str(RunResult.skiped)) msg = MIMEMultipart() msg['Subject'] = Header(subject, 'utf-8') msg['From'] = self.user msg['To'] = to text = MIMEText(contents, 'html', 'utf-8') msg.attach(text) if attachments is None: attachments = BrowserConfig.report_path att_name = "report.html" if "\\" in attachments: att_name = attachments.split("\\")[-1] if "/" in attachments: att_name = attachments.split("/")[-1] att = MIMEApplication(open(attachments, 'rb').read()) att['Content-Type'] = 'application/octet-stream' att["Content-Disposition"] = 'attachment; filename="{}"'.format( att_name) msg.attach(att) try: smtp = smtplib.SMTP() smtp.connect(self.host) smtp.login(self.user, self.password) smtp.sendmail(self.user, to, msg.as_string()) smtp.quit() log.info(" 📧 Email sent successfully!!") except BaseException as msg: log.error('❌ Email failed to send!!' + str(msg))