def keyword_Execute(self, keyword, pageObjectDict, parameter): llk = LowLevelKeyword() if keyword == llk.launchBrowser.__name__: return llk._deco(llk.launchBrowser, self.paras["Url"]) elif keyword == llk.setText.__name__: return llk._deco(llk.setText, pageObjectDict, self.paras["Value"]) elif keyword == llk.select.__name__: return llk._deco(llk.select, pageObjectDict, self.paras["Value"]) elif keyword == llk.click.__name__: return llk._deco(llk.click, pageObjectDict) elif keyword == llk.clearText.__name__: return llk._deco(llk.clearText, pageObjectDict) elif keyword == llk.doubleClick.__name__: return llk._deco(llk.clearText, pageObjectDict) ######To be added other keywords#### else: tool = Tool() testStepResult: TestStateResult = TestStepResult() testStepResult.endTime = Tool.CurrentTime() testStepResult.stepLog = str( testStepResult.endTime ) + ' No such kind of keyword: ' + keyword testStepResult.stepResult = StepResult.Error self.settings.isRunable = False fileName = tool.RanString Tool.saveScreenShot( self.settings.wedriver, self.settings.CurrentScriptResultFolder + '\\' + fileName + '.png') testStepResult.Actual = fileName + '.png' return testStepResult
def WriteCaseStateInfo(self): CaseStateInfo :str = '' for CaseResult in self.settings.testPlanResult.TestCaseResultCollection: self.writeCaseSummaryInfo(CaseResult) CSIT = Tool.readFile(self.CaseStateInfoTemplate) CSIT = CSIT.replace("TestCaseNumber", CaseResult.TestCaseNumber) CSIT = CSIT.replace("TestCaseName", CaseResult.TestCaseName) StateInfo :str = '' for StateResult in CaseResult.TestStateResultCollection: SIT = Tool.readFile(self.StateInfoTemplate) SIT = SIT.replace("TestStateNumber", StateResult.TestStateNumber) SIT = SIT.replace("TestStateDescription", StateResult.TestStateDescription) SIT = SIT.replace("TestStateFile", StateResult.TestStateFile) SIT = SIT.replace("TestStateReportPath", StateResult.RelativeTestStateReportPath) if StateResult.StateResult == 'NotRun': SIT = SIT.replace("ResultCategory", "notruncentertext") elif StateResult.StateResult == 'Error' or StateResult.StateResult == 'Failed': SIT = SIT.replace("ResultCategory", "failedcentertext") else: SIT = SIT.replace("ResultCategory", "passedcentertext") SIT = SIT.replace("StateResult", StateResult.StateResult) StateInfo = StateInfo + SIT + "\r\n" detailsInfoReport = DetailsInfoReport(StateResult) detailsInfoReport.CreateDetailsInfoReport() detailsInfoReport.writeDetailsInfotoHtml() CSIT = CSIT.replace("StateInfo", StateInfo) CaseStateInfo = CaseStateInfo + CSIT + "\r\n" Tool.appendFile(self.CaseStateInfoReport, CaseStateInfo)
def writeDetailsInfotoHtml(self): DIT = Tool.readFile(self.DetailsInfoTemplate) DIT = DIT.replace("TestStateNumber", self.testStateResult.TestStateNumber) DIT = DIT.replace("TestStateFile", self.testStateResult.TestStateFile) stepInfo = '' for sResult in self.testStateResult.TestStepResultCollection: CIT = Tool.readFile(self.CaseInfoTemplate) CIT = CIT.replace("StepNumber", sResult.StepNumber) CIT = CIT.replace("StepDescription", sResult.StepDescription) CIT = CIT.replace("StepKeyword", sResult.StepKeyword) CIT = CIT.replace("StepParameter", sResult.StepParameter) CIT = CIT.replace("StepObjectID", sResult.StepObject) CIT = CIT.replace("Expect", sResult.Expect) CIT = CIT.replace("Actual", sResult.Actual) if sResult.stepResult == StepResult.NotRun: CIT = CIT.replace("ResultCategory", "notruncentertext") elif sResult.stepResult == StepResult.Error or sResult.stepResult == StepResult.Failed: CIT = CIT.replace("ResultCategory", "failedcentertext") else: CIT = CIT.replace("ResultCategory", "passedcentertext") CIT = CIT.replace("StepResult", sResult.stepResult.name) CIT = CIT.replace("StepLog", sResult.stepLog) stepInfo = stepInfo + CIT + "\r\n" DIT = DIT.replace("StepInfo", stepInfo) Tool.writeFile(self.DetailsInfoHtml, DIT)
def setNon_ExecutedResult(self, methodName): testStepResult: TestStateResult = TestStepResult() testStepResult.startTime = Tool.CurrentTime() testStepResult.stepResult = StepResult.NotRun testStepResult.endTime = Tool.CurrentTime() testStepResult.stepLog = methodName + \ " execution was " + testStepResult.stepResult.name return testStepResult
def writeCaseSummaryInfo(self, testCaseResult:TestCaseResult): CSIT = Tool.readFile(self.caseSummaryInfoTemplate) CSIT = CSIT.replace("TestCaseNumber", testCaseResult.TestCaseNumber) CSIT = CSIT.replace("TestCaseFile", testCaseResult.TestCaseFile) CSIT = CSIT.replace("StartTime", testCaseResult.StartTime) if testCaseResult.CaseResult == 'NotRun': CSIT = CSIT.replace("ResultCategory", "notruncentertext") elif testCaseResult.CaseResult == 'Error' or testCaseResult.CaseResult == 'Failed': CSIT = CSIT.replace("ResultCategory", "failedcentertext") else: CSIT = CSIT.replace("ResultCategory", "passedcentertext") CSIT = CSIT.replace("CaseResult", testCaseResult.CaseResult) Tool.appendFile(self.caseSummaryInfoReportPath, CSIT)
def __init__ (self, tsFile): self.TestStepResultCollection:list = [] self.TestStateFile = tsFile self.settings = Settings.getSettings() self.TestStateStartTime = Tool.CurrentTime() self.TestStateReportPath:str = self.settings.CurrentScriptResultFolder + '\\' + self.TestStateFile.replace('.xlsx', '.html') self.RelativeTestStateReportPath:str = self.settings.CurrentScript + '\\' + self.TestStateFile.replace('.xlsx', '.html')
def updateScriptFileAndCaseResult(self, workB: Workbook, testCaseResult: TestCaseResult, caseResult): workB.save(self.CurrentScriptPath) logging.info('save scenario file: "{0}" successfully'.format( self.CurrentScriptPath)) testCaseResult.CaseResult = caseResult testCaseResult.EndTime = Tool.CurrentTime() self.testPlanResult.TestCaseResultCollection.append(testCaseResult)
def setLog(self): self.TestStateEndTime:str = Tool.CurrentTime() if self.StateResult == 'Passed' : self.StateLog = self.TestStateEndTime + ' Set state passed' elif self.StateResult == 'Failed' : self.StateLog = self.TestStateEndTime + ' Set state failed' elif self.StateResult == 'Error' : self.StateLog = self.TestStateEndTime + ' Set state, error occurred' else: self.StateLog = self.TestStateEndTime + ' Set state, not run' return self.StateLog
def _deco(self, func, *args): tool = Tool() self.testStepResult.startTime = Tool.CurrentTime() try: func(*args) except Exception as e: self.settings.isRunable: bool = False self.testStepResult.endTime = Tool.CurrentTime() self.testStepResult.stepResult = StepResult.Error self.testStepResult.stepLog = str(self.testStepResult.endTime) + ' ' + func.__name__ + \ ' execution was ' + StepResult.Error.name + \ '. Actual exception : ' + str(e) else: self.testStepResult.stepResult = StepResult.Passed self.testStepResult.endTime = Tool.CurrentTime() self.testStepResult.stepLog = str(self.testStepResult.endTime) + ' ' + \ func.__name__ + ' execution was ' + self.testStepResult.stepResult.name finally: fileName = tool.RanString Tool.saveScreenShot( self.settings.wedriver, self.settings.CurrentScriptResultFolder + '\\' + fileName + '.png') self.testStepResult.Actual = fileName + '.png' return self.testStepResult
def writeSystemInfo_toHtml(self): PIT = Tool.readFile(self.planInfoTemplate) PIT = PIT.replace("TestPlanStartTime", self.settings.testPlanResult.TestPlanStartTime) PIT = PIT.replace("TestPlanEndTime", self.settings.testPlanResult.TestPlanEndTime) PIT = PIT.replace("TotalCount", str(self.settings.testPlanResult.TotalCount)) PIT = PIT.replace( "allScenarioPassedCount", str(self.settings.testPlanResult.allScenarioPassedCount)) PIT = PIT.replace( "allScenarioFailedCount", str(self.settings.testPlanResult.allScenarioFailedCount)) PIT = PIT.replace( "allScenarioPassPercentage", self.settings.testPlanResult.allScenarioPassPercentage) PIT = PIT.replace( "allScenarioFailPercentage", self.settings.testPlanResult.allScenarioFailPercentage) Tool.writeFile(self.planInfoHtml, PIT)
def test_execute(self): '''execute each test step by step''' try: wb_plan = self.settings.loadPanFileAndSetCaseNumber() wk_plan_sheet = wb_plan['Sheet1'] for test_case_number in range(2, self.settings.TestCaseNumbers + 2): self.test_case_result = self.settings.cleanUpAndSetUpPlanFile( wk_plan_sheet, test_case_number) self.settings.createWebDriver() try: wb_script = self.settings.loadScriptFile() wk_script_sheet = wb_script['Sheet1'] test_state_row: int = self.settings.getStateNumber( wb_script) for test_state_number in range(2, test_state_row + 2): self.settings.setStateFile(wk_script_sheet, test_state_number) self.test_state_result = TestStateResult( self.settings.StateFile) e_f = ExecuteFunction() self.test_state_result = e_f.SetState( self.settings.StateFile) self.test_state_result = self.settings.updateStateResult( wk_script_sheet, self.test_state_result, test_state_number) self.test_case_result.TestStateResultCollection.append( self.test_state_result) except Exception as exception: logging.error( f'there is error: "{str(exception)}" "{traceback.format_exc()}"' ) finally: test_case_result = ExecuteFunction.TestCase_Result( self.test_case_result) self.settings.updateScriptFileAndCaseResult( wb_script, self.test_case_result, test_case_result) MainFramework.settings.wedriver.quit() logging.info('webdriver quit successfully') for test_case_number in range(2, self.settings.TestCaseNumbers + 2): self.settings.updatePlanFile(wk_plan_sheet, test_case_number) finally: self.settings.testPlanResult.TestPlanEndTime = Tool.CurrentTime() wb_plan.save(self.settings.planFilePath) logging.info( f'save plan file: {self.settings.planFilePath} successfully')
def constructSettings(): Settings._settings = Settings() Settings.planFile = 'Plan1' currentWD = os.getcwd() Settings.planFilePath = currentWD + '\\TestPlan\\Plan1.xlsx' tool = Tool() folderName: str = tool.RanString Settings.CurrentTestResultFolder = currentWD + \ '\\TestResult\\' + folderName + '\\' + Settings.planFile os.makedirs(Settings.CurrentTestResultFolder) Settings.testPlanResult = TestPlanResult("TestPlan") LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" logging.basicConfig(filename=os.path.join( currentWD, 'TestResult', folderName, 'runtime_{0}.log'.format(folderName)), level=logging.INFO, format=LOG_FORMAT)
def setErrorResult(self, e: Exception): tool = Tool() self.settings._isRunable = False testStepResult: TestStepResult = TestStepResult() testStepResult.startTime = tool.CurrentTime() testStepResult.stepResult = StepResult.Error testStepResult.endTime = tool.CurrentTime() testStepResult.stepLog = testStepResult.endTime + " " + str(e) fileName = tool.RanString Tool.saveScreenShot( self.settings.wedriver, self.settings.CurrentScriptResultFolder + '\\' + fileName + '.png') testStepResult.Actual = fileName + '.png' return testStepResult
def writeModuleInfo(self): moduleInfo: str = '' for key in self.settings.testPlanResult.moduleInfoCollection: self.testModuleInfo = TestModuleInfo() self.testModuleInfo.setModuleName(key) self.testModuleInfo.setModuleInfo() MIR = Tool.readFile(self.ModuleInfoTemplatePath) MIR = MIR.replace('ModuleName', self.testModuleInfo.currentModuleName) MIR = MIR.replace('ModuleTotalScriptCount', str(self.testModuleInfo.moduleScriptCount)) MIR = MIR.replace('ModuleScriptPassedCount', str(self.testModuleInfo.moduleScriptPassedCount)) MIR = MIR.replace('ModuleScriptFailedCount', str(self.testModuleInfo.moduleScriptFailedCount)) MIR = MIR.replace("ModuleScriptPassPercentage", self.testModuleInfo.modulePassPercentage) MIR = MIR.replace("ModuleScriptFailPercentage", self.testModuleInfo.moduleFailPercentage) moduleInfo = moduleInfo + MIR + "\r\n" return moduleInfo
def testExecute(self): try: wb_plan = self.settings.loadPanFileAndSetCaseNumber() wk_plan_sheet = wb_plan['Sheet1'] for i in range(2, self.settings.TestCaseNumbers + 2): self.testCaseResult = self.settings.cleanUpAndSetUpPlanFile( wk_plan_sheet, i) try: wb_script = self.settings.loadScriptFile() wk_script_sheet = wb_script['Sheet1'] testStateRow: int = self.settings.getStateNumber(wb_script) for i in range(2, testStateRow + 2): self.settings.setStateFile(wk_script_sheet, i) self.testStateResult = TestStateResult( self.settings.StateFile) ef = ExecuteFunction() self.testStateResult = ef.SetState( self.settings.StateFile) self.testStateResult = self.settings.updateStateResult( wk_script_sheet, self.testStateResult, i) self.testCaseResult.TestStateResultCollection.append( self.testStateResult) except Exception as e: logging.error('there is error: ' + str(e) + '.' + traceback.format_exc()) finally: self.settings.updateScriptFileAndCaseResult( wb_script, self.testCaseResult, ExecuteFunction.TestCase_Result(self.testCaseResult)) MainFramework.settings.wedriver.quit() logging.info('webdriver quit successfully') for i in range(2, self.settings.TestCaseNumbers + 2): self.settings.updatePlanFile(wk_plan_sheet, i) finally: self.settings.testPlanResult.TestPlanEndTime = Tool.CurrentTime() wb_plan.save(self.settings.planFilePath) logging.info('save plan file: "{0}" successfully'.format( self.settings.planFilePath))
def WriteCaseStateInfo_toHtml(self): CSIR = Tool.readFile(self.CaseStateInfoReport) planInfo = Tool.readFile(self.planInfoHtml) planInfo = planInfo.replace("CaseStateInfo", CSIR) Tool.writeFile(self.planInfoHtml, planInfo)
def __init__(self, testCaseFile): self.TestCaseFile = testCaseFile self.TestCaseName = self.TestCaseFile.replace(".xlsx", "") self.TestStateResultCollection = [] self.StartTime = Tool.CurrentTime()
def writeModuleInfo_toHtml(self): MIR = self.writeModuleInfo() planInfo = Tool.readFile(self.planInfoHtml) planInfo = planInfo.replace("ModuleInfo", MIR) Tool.writeFile(self.planInfoHtml, planInfo)
def writeCaseSummaryInfo_toHtml(self): CSIT = Tool.readFile(self.caseSummaryInfoReportPath) planInfo = Tool.readFile(self.planInfoHtml) planInfo = planInfo.replace("CaseSummaryInfo", CSIT) Tool.writeFile(self.planInfoHtml, planInfo)
def CreatePlanInfoReport(self): if not os.path.exists(self.planInfoHtml): Tool.createFile(self.planInfoHtml)
def CreateCaseSummaryInfoReport(self): if not os.path.exists(self.caseSummaryInfoReportPath): Tool.createFile(self.caseSummaryInfoReportPath)
def CreateCaseStateInfoReport(self): if not os.path.exists(self.CaseStateInfoReport): Tool.createFile(self.CaseStateInfoReport)
def __init__(self, TestPlanFile): self.TestCaseResultCollection = [] self.TestPlanStartTime = Tool.CurrentTime() self.TestPlan = TestPlanFile
def CreateDetailsInfoReport(self): if not os.path.exists(self.DetailsInfoHtml): Tool.createFile(self.DetailsInfoHtml)