def generate_report_history(): folders = common.get_sub_folder_names(os.path.join(env.RESULT_PATH, "result")) reports = [] for folder in folders: if len(folder) == 36: reports.append(folder) with open(os.path.join(env.RESULT_PATH, "result", "history.html"), "w") as f: f.write(html_source_header(title="Knitter Web Automation Test History")) f.write(html_source_body(title="Knitter Web Automation Test History")) f.write(html_source_table_history_header()) i = 0 for report in sorted(reports, reverse=True): Duration = common.get_value_from_conf(os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "Duration") TotalCases = common.get_value_from_conf(os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "TotalCases") PassedCases = common.get_value_from_conf(os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "PassedCases") FailedCases = common.get_value_from_conf(os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "FailedCases") f.write(html_source_table_history(report, report, Duration, TotalCases, PassedCases, FailedCases)) i = i + 1 if i > 100: break f.write(html_source_end_table()) f.write(html_source_foot())
def generate_report_history(): folders = common.get_sub_folder_names( os.path.join(env.RESULT_PATH, "result")) reports = [] for folder in folders: if len(folder) == 36: reports.append(folder) with open(os.path.join(env.RESULT_PATH, "result", "history.html"), "w") as f: f.write( html_source_header(title="Knitter Web Automation Test History")) f.write(html_source_body(title="Knitter Web Automation Test History")) f.write(html_source_table_history_header()) i = 0 for report in sorted(reports, reverse=True): Duration = common.get_value_from_conf( os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "Duration") TotalCases = common.get_value_from_conf( os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "TotalCases") PassedCases = common.get_value_from_conf( os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "PassedCases") FailedCases = common.get_value_from_conf( os.path.join(env.RESULT_PATH, "result", report, "status.ini"), "FailedCases") f.write( html_source_table_history(report, report, Duration, TotalCases, PassedCases, FailedCases)) i = i + 1 if i > 100: break f.write(html_source_end_table()) f.write(html_source_foot())
def run_module(module_name): if sys.getdefaultencoding() != 'utf-8': reload(sys) sys.setdefaultencoding('utf-8') testmodule = importlib.import_module("testcase.%s" % module_name) env.MODULE_NAME = module_name.split('.')[-1] testcases = [testmodule.__dict__.get(a).__name__ for a in dir(testmodule) if isinstance(testmodule.__dict__.get(a), types.FunctionType)] env.PROJECT_PATH = os.path.dirname(os.path.abspath(inspect.stack()[1][1])) sys.path.append(env.PROJECT_PATH) env.TESTING_BROWSERS = common.get_value_from_conf("TESTING_BROWSERS") for testcase in testcases: if testcase == "before_each_testcase" or testcase == "after_each_testcase" or testcase == "before_launch_browser": continue for browser in env.TESTING_BROWSERS.split('|'): env.RUNNING_BROWSER = browser ##### Launch Browser if "before_launch_browser" in testcases: getattr(testmodule, "before_launch_browser")() if launch_browser() == False: continue ##### Run Test Case. try: log.start_test(testcase) if "before_each_testcase" in testcases: getattr(testmodule, "before_each_testcase")() getattr(testmodule, testcase)() except: log.handle_error() finally: if "after_each_testcase" in testcases: getattr(testmodule, "after_each_testcase")() log.stop_test() ##### Clear Environment. Quite Browser, Kill Driver Processes. testcase_windingup()
def sendmail(file_new): mail_from = common.get_value_from_conf("mail_from") mailto = common.get_value_from_conf("mail_to") smtp_server = common.get_value_from_conf("smtp_server") smtp_login_name = common.get_value_from_conf("smtp_login_name") smtp_login_password = common.get_value_from_conf("smtp_login_password") Running_Browser_Devices = common.get_value_from_conf("TESTING_BROWSERS_OR_DEVICES") if Running_Browser_Devices.split("|")[0] in ("Chrome", "Firefox", "IE", "Safari"): source = "PC-Web" elif Running_Browser_Devices == "APP-Android": source = "APP-Android" elif Running_Browser_Devices == "APP-IOS": source = "APP-IOS" elif Running_Browser_Devices == "H5-Android": source = "H5-Android" elif Running_Browser_Devices == "H5-IOS": source = "H5-IOS" for mail_to in mailto.split('|'): f = open(file_new, 'rb') mail_body = f.read() f.close() msgRoot = MIMEMultipart('related') msgText = MIMEText(mail_body, _subtype='html', _charset='utf-8') msgRoot.attach(msgText) # 构造附件 att1 = MIMEText(open('.\\Result\\result.html', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1["Content-Disposition"] = 'attachment; filename="YOHO_autotest_result.html"' msgRoot.attach(att1) att2 = MIMEText(open('.\\Result\\summary.log', 'rb').read(), 'base64', 'utf-8') att2["Content-Type"] = 'application/octet-stream' att2["Content-Disposition"] = 'attachment; filename="YOHO_autotest_summary.log"' msgRoot.attach(att2) att3 = MIMEText(open('.\\Result\\result.xls', 'rb').read(), 'base64', 'utf-8') att3["Content-Type"] = 'application/octet-stream' att3["Content-Disposition"] = 'attachment; filename="YOHO_autotest_result.xls"' msgRoot.attach(att3) failnum = env.CaseFail if failnum >= 1: msgRoot['Subject'] = u"[%s Fail] YOHO %s Automated Test Report ( %s )" % (failnum, source, env.ExecuterDate) else: msgRoot['Subject'] = u"YOHO %s Automated Test Report ( %s )" % (source, env.ExecuterDate) msgRoot['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') smtp = smtplib.SMTP() smtp.connect(smtp_server) smtp.login(smtp_login_name, smtp_login_password) smtp.sendmail(mail_from, mail_to, msgRoot.as_string()) smtp.quit() print('=> Email has send out !')
def run_case(module_name, case_name): testmodule = importlib.import_module("testcase.%s" % module_name) env.MODULE_NAME = module_name.split('.')[-1] testcases = [testmodule.__dict__.get(a).__name__ for a in dir(testmodule) if isinstance(testmodule.__dict__.get(a), types.FunctionType)] env.PROJECT_PATH = inspect.stack()[1][1].rsplit("\\", 1)[0] env.TESTING_BROWSERS = common.get_value_from_conf("TESTING_BROWSERS") if not case_name in testcases: return for browser in env.TESTING_BROWSERS.split('|'): env.RUNNING_BROWSER = browser ##### Launch Browser if "before_launch_browser" in testcases: getattr(testmodule, "before_launch_browser")() if launch_browser() == False: continue ##### Run Test Case. try: log.start_test(case_name) if "before_each_testcase" in testcases: getattr(testmodule, "before_each_testcase")() getattr(testmodule, case_name)() except: log.handle_error() finally: if "after_each_testcase" in testcases: getattr(testmodule, "after_each_testcase")() log.stop_test() ##### Clear Environment. Quite Browser, Kill Driver Processes. testcase_windingup()
def run_module(module_name): testmodule = importlib.import_module("testcase.%s" % module_name) env.MODULE_NAME = module_name.split('.')[-1] testcases = [ testmodule.__dict__.get(a).__name__ for a in dir(testmodule) if isinstance(testmodule.__dict__.get(a), types.FunctionType) ] env.PROJECT_PATH = inspect.stack()[1][1].rsplit("\\", 1)[0] env.TESTING_BROWSERS = common.get_value_from_conf("TESTING_BROWSERS") for testcase in testcases: if testcase == "before_each_testcase" or testcase == "after_each_testcase" or testcase == "before_launch_browser": continue for browser in env.TESTING_BROWSERS.split('|'): env.RUNNING_BROWSER = browser ##### Launch Browser if "before_launch_browser" in testcases: getattr(testmodule, "before_launch_browser")() if launch_browser() == False: continue ##### Run Test Case. try: log.start_test(testcase) if "before_each_testcase" in testcases: getattr(testmodule, "before_each_testcase")() getattr(testmodule, testcase)() except: log.handle_error() finally: if "after_each_testcase" in testcases: getattr(testmodule, "after_each_testcase")() log.stop_test() ##### Clear Environment. Quite Browser, Kill Driver Processes. testcase_windingup()
def sendmail(file_new): mail_from = common.get_value_from_conf("mail_from") mailto = common.get_value_from_conf("mail_to") smtp_server = common.get_value_from_conf("smtp_server") smtp_login_name = common.get_value_from_conf("smtp_login_name") smtp_login_password = common.get_value_from_conf("smtp_login_password") Running_Browser_Devices = common.get_value_from_conf( "TESTING_BROWSERS_OR_DEVICES") if Running_Browser_Devices.split("|")[0] in ("Chrome", "Firefox", "IE", "Safari"): source = "PC-Web" elif Running_Browser_Devices == "APP-Android": source = "APP-Android" elif Running_Browser_Devices == "APP-IOS": source = "APP-IOS" elif Running_Browser_Devices == "H5-Android": source = "H5-Android" elif Running_Browser_Devices == "H5-IOS": source = "H5-IOS" for mail_to in mailto.split('|'): f = open(file_new, 'rb') mail_body = f.read() f.close() msgRoot = MIMEMultipart('related') msgText = MIMEText(mail_body, _subtype='html', _charset='utf-8') msgRoot.attach(msgText) # 构造附件 att1 = MIMEText( open('.\\Result\\result.html', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' att1[ "Content-Disposition"] = 'attachment; filename="YOHO_autotest_result.html"' msgRoot.attach(att1) att2 = MIMEText( open('.\\Result\\summary.log', 'rb').read(), 'base64', 'utf-8') att2["Content-Type"] = 'application/octet-stream' att2[ "Content-Disposition"] = 'attachment; filename="YOHO_autotest_summary.log"' msgRoot.attach(att2) att3 = MIMEText( open('.\\Result\\result.xls', 'rb').read(), 'base64', 'utf-8') att3["Content-Type"] = 'application/octet-stream' att3[ "Content-Disposition"] = 'attachment; filename="YOHO_autotest_result.xls"' msgRoot.attach(att3) failnum = env.CaseFail if failnum >= 1: msgRoot[ 'Subject'] = u"[%s Fail] YOHO %s Automated Test Report ( %s )" % ( failnum, source, env.ExecuterDate) else: msgRoot['Subject'] = u"YOHO %s Automated Test Report ( %s )" % ( source, env.ExecuterDate) msgRoot['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') smtp = smtplib.SMTP() smtp.connect(smtp_server) smtp.login(smtp_login_name, smtp_login_password) smtp.sendmail(mail_from, mail_to, msgRoot.as_string()) smtp.quit() print('=> Email has send out !')
def launch_browser(): if env.RUNNING_BROWSER.upper() == "FIREFOX": #os.popen("TASKKILL /F /IM firefox.exe") fp = FirefoxProfile() fp.native_events_enabled = False binary_path = common.get_value_from_conf("FIREFOX_BINARY_PATH") if binary_path == "": env.BROWSER = webdriver.Firefox(firefox_profile=fp) else: fb = FirefoxBinary(firefox_path=binary_path) env.BROWSER = webdriver.Firefox(firefox_profile=fp, firefox_binary=fb) elif env.RUNNING_BROWSER.upper() == "CHROME": #os.popen("TASKKILL /F /IM chrome.exe") os.popen("TASKKILL /F /IM chromedriver.exe") binary_path = common.get_value_from_conf("CHROME_BINARY_PATH") chromedriver = common.get_value_from_conf("DRIVER_CHROME") if binary_path == "": os.environ["webdriver.chrome.driver"] = chromedriver env.BROWSER = webdriver.Chrome(executable_path=chromedriver) else: opts = Options() opts.binary_location = binary_path os.environ["webdriver.chrome.driver"] = chromedriver env.BROWSER = webdriver.Chrome(executable_path=chromedriver, chrome_options=opts) elif env.RUNNING_BROWSER.upper() == "IE": #os.popen("TASKKILL /F /IM iexplore.exe") os.popen("TASKKILL /F /IM IEDriverServer.exe") dc = DesiredCapabilities.INTERNETEXPLORER.copy() dc['acceptSslCerts'] = True dc['nativeEvents'] = True iedriver = common.get_value_from_conf("DRIVER_IE") os.environ["webdriver.ie.driver"] = iedriver env.BROWSER = webdriver.Ie(executable_path=iedriver, capabilities=dc) else: return False env.TEST_URL = common.get_value_from_conf("TESTING_URL") env.BROWSER.get(env.TEST_URL) env.BROWSER.maximize_window() time.sleep(3) return True
def generatehtml(): hostfromconf = common.get_value_from_conf("host") portfromconf = common.get_value_from_conf("port") userfromconf = common.get_value_from_conf("user") passwdfromconf = common.get_value_from_conf("passwd") dbfromconf = common.get_value_from_conf("db") charsetfromconf = common.get_value_from_conf("charset") dbconfig = {'host': hostfromconf, 'port': int(portfromconf), 'user': userfromconf, 'passwd': passwdfromconf, 'db': dbfromconf, 'charset': charsetfromconf} db = DBUtil.MySQL(dbconfig) Running_Browser_Devices = common.get_value_from_conf("TESTING_BROWSERS_OR_DEVICES") if Running_Browser_Devices.split("|")[0] in ("Chrome", "Firefox", "IE", "Safari"): source = "PC-Web" elif Running_Browser_Devices == "APP-Android": source = "APP-Android" elif Running_Browser_Devices == "APP-IOS": source = "APP-IOS" elif Running_Browser_Devices == "H5-Android": source = "H5-Android" elif Running_Browser_Devices == "H5-IOS": source = "H5-IOS" precent = round(float(env.CaseSuccess)/(env.CaseSuccess + env.CaseFail), 4)*100 template = """ <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <table border="1" style="width: 100%;text-align: center;" cellspacing="0" cellpadding="0"> <tr> <td colspan="10"><font face="微软雅黑" size="4" ><strong>YOHO """ + source + """ Automated Test Report</strong></font></td> </tr> <tr align="left"> <td colspan="10"><font color="blue">→Executer_Date: """ + env.ExecuterDate + """</font> <br> <font color="blue">→Executer_Time: """ + env.CalTime + """</font> <br> <font color="blue">→Running_Browser_Devices: """ + Running_Browser_Devices + """</font> <br> <font color="blue">→Executer_Case_Total: """ + str(env.CaseSuccess + env.CaseFail) + """</font> <br> <font color="blue">→Executer_Case_Success: """ + str(env.CaseSuccess) + """</font> <br> <font color="blue">→Executer_Case_Fail: """ + str(env.CaseFail) + """</font> <br> <font color="blue">→Executer_SuccessRate(%): """ + str(precent) + """% </font> </td> </tr> <tr> <td bgColor=#C0C0C0 colspan="2" >Test_Case_Name</td> <td bgColor=#C0C0C0 >IE</td> <td bgColor=#C0C0C0 >Firefox</td> <td bgColor=#C0C0C0 >Chrome</td> <td bgColor=#C0C0C0 >Safari</td> <td bgColor=#C0C0C0 >APP-Android</td> <td bgColor=#C0C0C0 >APP-IOS</td> <td bgColor=#C0C0C0 >H5-Android</td> <td bgColor=#C0C0C0 >H5-IOS</td> </tr> <result_trs/> </table> </html> """ result_trs = "" data = xlrd.open_workbook(".\\Result\\result.xls") for sheetname in data.sheet_names(): table = data.sheet_by_name(sheetname) nrows = table.nrows for i in range(1, nrows): excelvalue = table.row_values(i) testcasename = excelvalue[0] IE_status = excelvalue[1] Firefox_status = excelvalue[2] Chrome_status = excelvalue[3] Safari_status = excelvalue[4] Android_status = excelvalue[5] IOS_status = excelvalue[6] H5_Android_status = excelvalue[7] H5_IOS_status = excelvalue[8] """ xls = datadriver.ExcelSheet("TeseCaseDescription.xlsx", "TeseCaseDescription") for j in range(1, xls.nrows()): # log.step_section("Execute TeseCaseDescription Excel Date: Line [%s]" % j) tescaseName = xls.cell(j, "TeseCaseName") tesecasedescription = xls.cell(j, "TeseCaseDescription") if sheetname == tescaseName: break """ sql_list = "select testcasename, testcasedescription from yoho_testcasedescription " \ "where state='A' order by id asc" db.query(sql_list) sql_list_result = db.fetchAllRows() for result in sql_list_result: tescaseName = result[0] tesecasedescription = result[1] if sheetname == tescaseName: break sql = "insert into executer_result values('" + env.ExecuterDate + "','" + sheetname + "','" + testcasename + "','" + IE_status + "','" + Firefox_status + "','" + Chrome_status + "','" + Safari_status + "','" + Android_status + "','" + IOS_status + "','" + H5_Android_status + "','" + H5_IOS_status + "')" db.insert(sql) result_tr = "<tr><td style="'text-align:left'" >" + tesecasedescription + "</td><td style="'text-align:left'" >" + sheetname + "</td>" # IE_status if IE_status == "Pass": result_tr += "<td bgColor=#008000 >" + IE_status + "</td>" elif IE_status == "Fail": result_tr += "<td bgColor=#FF0000 >" + IE_status + "</td>" else: result_tr += "<td>" + IE_status + "</td>" # Firefox_status if Firefox_status == "Pass": result_tr += "<td bgColor=#008000 >" + Firefox_status + "</font></td>" elif Firefox_status == "Fail": result_tr += "<td bgColor=#FF0000 >" + Firefox_status + "</font></td>" else: result_tr += "<td>" + Firefox_status + "</td>" # Chrome_status if Chrome_status == "Pass": result_tr += "<td bgColor=#008000 >" + Chrome_status + "</font></td>" elif Chrome_status == "Fail": result_tr += "<td bgColor=#FF0000 >" + Chrome_status + "</font></td>" else: result_tr += "<td>" + Chrome_status + "</td>" # Safari_status if Safari_status == "Pass": result_tr += "<td bgColor=#008000 >" + Safari_status + "</font></td>" elif Safari_status == "Fail": result_tr += "<td bgColor=#FF0000 >" + Safari_status + "</font></td>" else: result_tr += "<td>" + Safari_status + "</td>" # Android_status if Android_status == "Pass": result_tr += "<td bgColor=#008000 >" + Android_status + "</font></td>" elif Android_status == "Fail": result_tr += "<td bgColor=#FF0000 >" + Android_status + "</font></td>" else: result_tr += "<td>" + Android_status + "</td>" # IOS_status if IOS_status == "Pass": result_tr += "<td bgColor=#008000 >" + IOS_status + "</font></td>" elif IOS_status == "Fail": result_tr += "<td bgColor=#FF0000 >" + IOS_status + "</font></td>" else: result_tr += "<td>" + IOS_status + "</td>" # H5_Android_status if H5_Android_status == "Pass": result_tr += "<td bgColor=#008000 >" + H5_Android_status + "</font></td>" elif H5_Android_status == "Fail": result_tr += "<td bgColor=#FF0000 >" + H5_Android_status + "</font></td>" else: result_tr += "<td>" + H5_Android_status + "</td>" # H5_IOS_status if H5_IOS_status == "Pass": result_tr += "<td bgColor=#008000 >" + H5_IOS_status + "</font></td>" elif H5_IOS_status == "Fail": result_tr += "<td bgColor=#FF0000 >" + H5_IOS_status + "</font></td>" else: result_tr += "<td>" + H5_IOS_status + "</td>" result_tr += "</tr>" result_trs += result_tr db.close() html = template.replace("<result_trs/>", result_trs) open(".\\Result\\result.html", "w").write(html)