//通过环境文件 和 数据文件 得到数据字典 并加入几个特殊的键 private Dictionary <String, String> getTestData(String envFileName, String excelFileName) { String envName = File.ReadAllText(envFileName, Encoding.UTF8).Trim().Replace("\r\n", ""); Dictionary <String, String> dic = new ExcelProcessor().getDicByEnvNameFromExcel(excelFileName, envName); return(dic); }
//excelFile 用例的电子表格文件 //functionDic 方法字典 //dataDic 数据字典 //生成中间的test里面的执行部分的python代码 private String getPyCodes(String excelFile, String testPagePath, String envFilePath, String dataFilePath) { List <Action> list = null;// new ExcelProcessor().readActionListFromExcelFile(excelFile); try { list = new ExcelProcessor().readActionListFromExcelFile(excelFile); } catch (Exception e) { throw (new Exception(e.Message + "\r\n" + "1读取用例文件出错,可能是因为用其他程序打开了用例文件!")); } Dictionary <String, String> functionDic = null; try { functionDic = new TextProcessor().createFunctionDic(testPagePath); } catch (Exception e) { throw(new Exception("自定义方法读取异常")); } Dictionary <String, String> dataDic = null; try { dataDic = this.getTestData(envFilePath, dataFilePath); //这里再加入几个特殊的键 String timeStr = DateTime.Now.ToString("yyyyMMddHHmmss"); dataDic.Add("TIME", timeStr); dataDic.Add("RANDOM2", getRandom(2)); dataDic.Add("RANDOM3", getRandom(3)); dataDic.Add("RANDOM4", getRandom(4)); dataDic.Add("RANDOM5", getRandom(5)); dataDic.Add("RANDOM6", getRandom(6)); } catch (Exception e) { throw (new Exception("读取配置文件出错,可能是因为用其他程序打开了配置文件!")); } return(getPyCodes(list, functionDic, dataDic, 0)); }
//action 动作 //functionDic 方法字典 //dataDic 数据字典 private String getPyCode(Action action, Dictionary <String, String> functionDic, Dictionary <String, String> dataDic, int indent) { String code = ""; int index = action.index; bool isRun = action.isRun; String note = this.getIndentSpace(TextProcessor.indent) + " #步骤" + index + "生成的代码\r\n"; //+this.getIndentSpace(TextProcessor.indent) + " print('步骤" + index + "')"; if (isRun) { //创建驱动 if (action.GetType().FullName.EndsWith("CreateDriverAction")) { String browserName = ((CreateDriverAction)action).browserName; String driverName = ((CreateDriverAction)action).driverName; code = this.getIndentSpace(TextProcessor.indent) + " if self.getPar('LASTSTEP'):"; code += "\r\n"; code += this.getIndentSpace(TextProcessor.indent) + " self.driver = DriverInit(self.logger).getWebDriver('" + browserName + "')"; code += "\r\n"; code += this.getIndentSpace(TextProcessor.indent) + " pP=PageOfPublic(self.pars,self.driver,self.logger)"; } /* * * //创建Appium驱动 * else if (action.GetType().FullName.EndsWith("CreateAppiumDriverAction")) * { * String deviceName = ((CreateAppiumDriverAction)action).deviceName; * String appPackageAndAppActivity = ((CreateAppiumDriverAction)action).appPackageAndAppActivity; * String[] tmp = appPackageAndAppActivity.Split( new String[] {","},StringSplitOptions.None ); * String appPackage = tmp[0]; * String appActivity = tmp[1]; * code = " this.appiumDriver = new Init().getAppiumDriver(\"" + deviceName + "\", \"" + appPackage + "\", \"" + appActivity + "\");" + "\r\n this.appPackage=\"" + appPackage + "\";"; * } * * //创建IOS驱动 * else if (action.GetType().FullName.EndsWith("CreateIOSDriverAction")) * { * String macIP = ((CreateIOSDriverAction)action).macIP; * String appPath = ((CreateIOSDriverAction)action).appPath; * code = " this.appiumDriver = new Init().getIOSDriver(\""+ macIP +"\",\""+ appPath +"\");"; * * } * */ // 打开网址 else if (action.GetType().FullName.EndsWith("GoAction")) { String driverName = ((GoAction)action).driverName; String url = ((GoAction)action).url; code = this.getIndentSpace(TextProcessor.indent) + " if self.getPar('LASTSTEP'):"; code += "\r\n"; code += this.getIndentSpace(TextProcessor.indent) + " self.openURL('" + url + "')"; } //设置参数 else if (action.GetType().FullName.EndsWith("SetParameterAction")) { String parName = ((SetParameterAction)action).parName; String parValue = ((SetParameterAction)action).parValue; code = this.getIndentSpace(TextProcessor.indent) + " self.setPar(\"" + parName + "\", \"" + parValue + "\")"; } //执行方法 else if (action.GetType().FullName.EndsWith("ExecuteFunctionAction")) { String driverName = ((ExecuteFunctionAction)action).driverName; String functionName = ((ExecuteFunctionAction)action).functionName; if (functionDic.Keys.Contains(functionName)) { String classAndFunction = functionDic[functionName]; code = this.getIndentSpace(TextProcessor.indent) + " " + classAndFunction.Replace(".", "(self.pars, self.driver, self.logger).") + "()"; } else { code = this.getIndentSpace(TextProcessor.indent) + " print('没有找到配置的方法')"; } } //读取外部配置 else if (action.GetType().FullName.EndsWith("ReadExternalConfAction")) { String excelPath = ((ReadExternalConfAction)action).excelPath; if (":" != excelPath.Substring(1, 1)) { String absPath = new TextProcessor().getAbsPath(); excelPath = absPath + "\\files\\" + excelPath; } ExcelProcessor excelProcessor = new ExcelProcessor(); List <List <String> > lists = excelProcessor.readfirststr(excelPath); Dictionary <String, String> dic = excelProcessor.strList2Dic(lists); //code = this.getIndentSpace(TextProcessor.indent) + " print('步骤" + index + "')"; foreach (KeyValuePair <String, String> kv in dic) { String k = kv.Key; String v = kv.Value; code += this.getIndentSpace(TextProcessor.indent) + " self.setPar(\"" + k + "\", \"" + v + "\")"; code += "\r\n"; } } //单步方法 else if (action.GetType().FullName.EndsWith("OneStepAction")) { String oneStepAction = ((OneStepAction)action).oneStepName; String oneStepPars = ((OneStepAction)action).oneStepPars; code = this.getIndentSpace(TextProcessor.indent) + " pP.oneStep('" + oneStepAction + "','''" + oneStepPars + "''')"; } //暂停 else if (action.GetType().FullName.EndsWith("SleepAction")) { int timeLong = ((SleepAction)action).timeLong; code = this.getIndentSpace(TextProcessor.indent) + " time.sleep(" + timeLong + ")"; } //调用其他用例 else if (action.GetType().FullName.EndsWith("ExeOtherCaseAction")) { String casePath = ((ExeOtherCaseAction)action).casePath; String caseFullPath = new TextProcessor().getAbsPath() + "\\" + casePath; //////Console.WriteLine(casePath); List <Action> list = new ExcelProcessor().readActionListFromExcelFile(caseFullPath); code = this.getIndentSpace(TextProcessor.indent) + " ################################################################"; code += "\r\n\r\n"; code += this.getIndentSpace(TextProcessor.indent) + " #执行用例[" + casePath + "]"; code += "\r\n\r\n"; code += this.getIndentSpace(TextProcessor.indent) + " ################################################################"; code += "\r\n"; code += this.getIndentSpace(TextProcessor.indent) + " self.logger.appendContent('\\r\\n################################################################\\r\\n执行用例[" + casePath + "]\\r\\n################################################################')"; code += "\r\n"; code += this.getPyCodes(list, functionDic, dataDic, TextProcessor.indent); } //退出 else if (action.GetType().FullName.EndsWith("QuitAction")) { String driverName = ((QuitAction)action).driverName; code = this.getIndentSpace(TextProcessor.indent) + " if None!=self.driver:"; code += "\r\n"; code += this.getIndentSpace(TextProcessor.indent) + " self.driver.quit()"; code += "\r\n"; code += this.getIndentSpace(TextProcessor.indent) + " self.logger.appendContent(\"退出\")"; } //注释 else if (action.GetType().FullName.EndsWith("QuitAction")) { String annotationText = ((AnnotationAction)action).annotationText; code = this.getIndentSpace(TextProcessor.indent) + " #" + annotationText; } //Python代码 else if (action.GetType().FullName.EndsWith("PythonCodeAction")) { String pcode = ((PythonCodeAction)action).pcode; code = this.getIndentSpace(TextProcessor.indent) + " " + pcode; } //{ else if (action.GetType().FullName.EndsWith("AddIndentAction")) { TextProcessor.indent++; code = this.getIndentSpace(TextProcessor.indent) + " #开始缩进"; } //{ else if (action.GetType().FullName.EndsWith("RecoverIndentAction")) { code += this.getIndentSpace(TextProcessor.indent) + " #恢复缩进"; TextProcessor.indent--; } return(note + "\r\n" + replaceVars(code, dataDic) + "\r\n"); } else { return(""); } /* * * String fun = executeObject.fun; * String code = ""; * * if ("设置参数" == fun) * { * String par1 = executeObject.par1; * String par2 = executeObject.par2; * code = " pars.put(\"" + par1 + "\", \"" + par2 + "\");"; * } * else if ("打开网址" == fun || "转到网址" == fun) * { * String par1 = executeObject.par1; * * code = " openURL(\"" + par1 + "\");"; * * } * else if ("暂停" == fun) * { * String par1 = executeObject.par1; * code = " sleep("+par1+");"; * * } * * else * { * if (functionDic.Keys.Contains(fun)) * { * String classAndFunction = functionDic[fun]; * code = " " + classAndFunction; * * } * else * { * code = ""; * } * * * } * return replaceVars(code, dataDic); */ }