/// <summary> /// add pre-conditions to each screnario /// </summary> /// <param name="screen"></param> /// <param name="mapNameAndScreens"></param> /// <param name="myLog"></param> private void AddPreCondition(SpecScreen screen, Dictionary <string, SpecScreen> mapNameAndScreens, MyLog myLog) { foreach (IScenario scenario in screen.Scenarios) { if (scenario is SpecScenario specScenario) { if (specScenario.PreConditionsExpression != null && specScenario.PreConditionsExpression.Count > 0) { foreach (string preConditionExp in specScenario.PreConditionsExpression) { if (preConditionExp == null || preConditionExp.Equals("")) { continue; } int idx = preConditionExp.LastIndexOf("_"); if (idx < 0) { myLog.Warn("Can not find the pre-condiction: " + preConditionExp); } else { string preScreen = preConditionExp.Substring(0, idx); try { int preScenarioIdx = Int32.Parse(preConditionExp.Substring(idx + 1)); SpecScreen preConditionScreen = mapNameAndScreens[preScreen]; SpecScenario preConditonScenario = getScenarioByIndex( preScenarioIdx, preConditionScreen.Scenarios); if (preConditionScreen == null || preScenarioIdx > preConditionScreen.Scenarios.Count || preConditonScenario == null) { myLog.Warn("Can not find the pre-condiction: " + preConditionExp); } else { specScenario.PreConditions.Add(preConditonScenario); } } catch (FormatException e) { myLog.Warn("Invalid pre-condiction: " + preConditionExp); } } } } } else { myLog.Warn("Not implement this type"); } } }
/// <summary> /// analyze list of screens /// </summary> /// <param name="listScreens"></param> /// <param name="myLog"></param> public bool Expand(List <IScreen> listScreens, string pathToApp, MyLog myLog) { Dictionary <string, SpecScreen> map = new Dictionary <string, SpecScreen>(); List <ClassExpression> listClassesExpression = new List <ClassExpression>(); foreach (IScreen screen in listScreens) { if (screen == null) { listScreens.Remove(screen); } else if (screen is SpecScreen specScreen) { map.Add(screen.Name, specScreen); if (specScreen is TestSpecificationScreen testSpecScreen) { Utils.MergeClasses2(listClassesExpression, testSpecScreen.ClassExpressions); } } else { myLog.Warn("Not Implement for this type", logger); } } foreach (SpecScreen screen in map.Values) { DoExpand(screen, pathToApp, myLog, listClassesExpression); } foreach (SpecScreen screen in map.Values) { AddPreCondition(screen, map, myLog); } return(true); }
public static Tuple <IElement, string> parseNodeAndAttribute(string expression, ListUIElements elementsAndIndicator, Dictionary <string, string> mappingAlias, MyLog myLog) { if (mappingAlias != null && mappingAlias.ContainsKey(expression)) { expression = mappingAlias[expression]; } string[] subs = expression.Split('.'); string screenName = null; string nodeName = null; string attribute = null; IElement element = null; //case MainForm if (subs.Length == 1) { nodeName = subs[0]; element = searchElement(nodeName, screenName, elementsAndIndicator); } //case MainForm.Button else if (subs.Length == 2) { screenName = subs[0]; nodeName = subs[1]; element = searchElement(nodeName, screenName, elementsAndIndicator); //case MainForm.Text if (element == null) { attribute = nodeName; element = searchElement(screenName, null, elementsAndIndicator); } } //case MainForm.Button.Text and MainForm.DataGridView.Cell(1,1).Text else { screenName = subs[0]; nodeName = subs[1]; attribute = subs[2]; for (int i = 3; i < subs.Length; i++) { attribute += "." + subs[i]; } element = searchElement(nodeName, screenName, elementsAndIndicator); } if (element == null) { string alias = expression.Split('.')[0]; if (mappingAlias != null && mappingAlias.ContainsKey(alias)) { return(parseNodeAndAttribute(expression.Replace(alias, mappingAlias[alias]), elementsAndIndicator, mappingAlias, myLog)); } else { myLog.Warn("Can not find element " + expression, logger); return(null); } } return(new Tuple <IElement, string>(element, attribute)); }
private void setColumnIndex(Excel.Range firstRow, MyLog myLog, ref int columnCount) { //int columnCount = firstRow.Columns.Count; for (int fi = 1; fi <= columnCount; fi++) { string title = getValueCell(firstRow, fi); if (title == null) { columnCount = fi - 1; break; } switch (title) { case WORKSPACE: WORKSPACE_COLUMN_INDEX = fi; break; case CLASS: CLASS_COLUMN_INDEX = fi; break; case FUNCTION_NAME: FUNCTION_NAME_COLUMN_INDEX = fi; break; case ACCESSIBILITY: ACCESSIBILITY_COLUMN_INDEX = fi; break; case SUMMARY: SUMMARY_COLUMN_INDEX = fi; break; case PARAMETERS: PARAMETERS_COLUMN_INDEX = fi; break; case RETURNS: RETURNS_COLUMN_INDEX = fi; break; case IMPLEMENTATION: IMPLEMENTATION_COLUMN_INDEX = fi; break; case "": columnCount = fi - 1; break; default: myLog.Warn("Don't know column \"" + title + "\""); columnCount = -1; return; } } }
private List <ParameterExpression> parseParams(string paramsStr, MyLog myLog) { if (paramsStr == null || paramsStr.Equals("") || paramsStr.Equals(AbstractSpecUserAction.NA)) { return(null); } string[] paramStrList = paramsStr.Split('\n'); List <ParameterExpression> re = new List <ParameterExpression>(); foreach (string paramStr in paramStrList) { string[] paramAndDesc = paramStr.Split(':'); if (paramAndDesc.Length != 2) { myLog.Warn("Parameter \"" + paramsStr + "\" is incorrect format!", logger); } re.Add(new ParameterExpression(paramAndDesc[0].Trim(), paramAndDesc.Length > 1 ? paramAndDesc[1].Trim() : "")); } return(re); }
/// <summary> /// 'GetAttributeValue<object>("Text")' for Ranorex /// </summary> /// <param name="nodeExp"></param> /// <param name="listUIElements"></param> /// <param name="mappingAlias"></param> /// <param name="myLog"></param> /// <param name="instanceName"></param> /// <returns></returns> public static string Node2ParamExp(string nodeExp, ListUIElements listUIElements, Dictionary <string, string> mappingAlias, MyLog myLog, string instanceName, ScriptType scriptType) { Tuple <IElement, string> elementAndAttibute = NormalSheetParser.parseNodeAndAttribute(nodeExp, listUIElements, mappingAlias, myLog); if (elementAndAttibute == null || elementAndAttibute.Item1 == null) { myLog.Warn("cannot find node: " + nodeExp); return("null"); } if (scriptType == ScriptType.Normal) { return(GetScriptAccessElement(elementAndAttibute.Item1, instanceName)); } //Ranorex scripts string re = getScriptAccessRanorexElement( new SpecNode(elementAndAttibute.Item1, elementAndAttibute.Item2, null), instanceName); if (elementAndAttibute.Item2 != null && !elementAndAttibute.Item2.Equals("")) { re += ".GetAttributeValue<object>(\"" + elementAndAttibute.Item2 + "\")"; } return(re); }
public List <ClassExpression> parse(FunctionDescriptionSheet funcSheet, MyLog myLog) { // parseOneFile title row Excel.Range xlRange = funcSheet.getSheet().UsedRange; int rowCount = xlRange.Rows.Count; int columnCount = xlRange.Columns.Count; Excel.Range firstRow = funcSheet.getSheet().Rows[1]; setColumnIndex(firstRow, myLog, ref columnCount); if (columnCount < 0) { return(null); } List <ClassExpression> classExpressions = new List <ClassExpression>(); ClassExpression classExpression = new ClassExpression(); for (int fi = 2; fi <= rowCount; fi++) { Excel.Range row = funcSheet.getSheet().Rows[fi]; string funcName = getValueCell(row, FUNCTION_NAME_COLUMN_INDEX); // indicate end sheet if (funcName == null || funcName.Equals("")) { break; } string workspace = getValueCell(row, WORKSPACE_COLUMN_INDEX); string _class = getValueCell(row, CLASS_COLUMN_INDEX); string accessibility = getValueCell(row, ACCESSIBILITY_COLUMN_INDEX); string summary = getValueCell(row, SUMMARY_COLUMN_INDEX); string parameters = getValueCell(row, PARAMETERS_COLUMN_INDEX); string _returns = getValueCell(row, RETURNS_COLUMN_INDEX); string implementation = getValueCell(row, IMPLEMENTATION_COLUMN_INDEX); string last_workspace = workspace == null || workspace.Equals("") ? classExpression.getWorkspace() : workspace; string last_class = _class == null || _class.Equals("") ? classExpression.getName() : _class; if (last_workspace == null || last_workspace.Equals("")) { myLog.Warn("Not know workspace for row #" + (fi + 1) + " in \"" + funcSheet.getSheet().Name + "\" sheet", logger); } if (last_class == null || last_workspace.Equals("")) { myLog.Warn("Not know workspace for row #" + (fi + 1) + " in \"" + funcSheet.getSheet().Name + "\" sheet", logger); } if (workspace != null && !workspace.Equals("") || (_class != null && !_class.Equals(""))) { classExpression = new ClassExpression(last_workspace, last_class); classExpressions.Add(classExpression); } List <ParameterExpression> _params = parseParams(parameters, myLog); FunctionExpression funcEpx = new FunctionExpression( funcName, accessibility, summary, _params); funcEpx.setReturnDescription(_returns); funcEpx.setContent(implementation); if (classExpression.getListFunction() == null) { classExpression.setListFunction(new List <FunctionExpression>()); } classExpression.getListFunction().Add(funcEpx); } return(classExpressions); }
/// <summary> /// /// </summary> /// <param name="sheet"></param> /// <param name="elementsAndIndicator">indication to determine root elements or list of all elements</param> /// <param name="myLog"></param> /// <returns></returns> private SpecScreen parserOneSheet(Excel._Worksheet sheet, ListUIElements elementsAndIndicator, MyLog myLog) { Excel.Range xlRange = sheet.UsedRange; int rowCount = xlRange.Rows.Count; int columnCount = xlRange.Columns.Count; SpecScreen screen = new SpecScreen(); // get mapping alias Dictionary <string, string> mapAliasWithNode = getMappingAlias(sheet, rowCount, columnCount); screen.MappingAliasWithNode = mapAliasWithNode; /** * read first row to get all element Nodes */ //Row firstRow = sheet.getRow(ROW_TITLE_TESTCASE); string no = getCellValue(sheet, ROW_TITLE_TESTCASE, 1); //if (searchElement(no, null, allUIElements) == null && !no.Equals(NO, stringComparison.OrdinalIgnoreCase)) //{ // logWarnings.add("First column must numbered column!"); // logger.error("First column must numbered column!"); //} List <SpecNode> listSpecNodes = new List <SpecNode>(); // <start, end>-s List <Tuple <int, int> > listValidationUsercode = new List <Tuple <int, int> >(); // get each node element //int lastColumn = firstRow.getLastCellNum(); bool link = false; int startColumnIdx = 2; for (int fi = startColumnIdx; fi <= columnCount; fi++) { string value = getCellValue(sheet, ROW_TITLE_TESTCASE, fi); if (value == null || value.Trim().Equals("")) { if (checkLastColumn(sheet, fi, rowCount)) { columnCount = fi - 1; break; } else { if (listValidationUsercode.Count == 0) { listValidationUsercode.Add(new Tuple <int, int>(fi - 1 - startColumnIdx, fi - startColumnIdx)); } else { Tuple <int, int> lastPair = listValidationUsercode[listValidationUsercode.Count - 1]; if ((fi - lastPair.Item2 - startColumnIdx) == 1) { listValidationUsercode.RemoveAt(listValidationUsercode.Count - 1); listValidationUsercode.Add(new Tuple <int, int>(lastPair.Item1, fi - startColumnIdx)); } else { listValidationUsercode.Add(new Tuple <int, int>(fi - 1 - startColumnIdx, fi - startColumnIdx)); } } listSpecNodes.Add(listSpecNodes.Last()); } } else { if (Regex.IsMatch(value, AbstractSpecUserAction.LINKS)) { columnCount = fi - 1; link = true; break; } else { SpecNode specNode = parseNode(value, elementsAndIndicator, mapAliasWithNode, myLog); if (specNode == null) { return(null); } listSpecNodes.Add(specNode); } } } screen.Name = sheet.Name; screen.AllUIElements = elementsAndIndicator; screen.ListSpecNodes = listSpecNodes; screen.ListValidationUserCode = listValidationUsercode; /** * parseOneFile each scenario */ for (int fi = ROW_TITLE_TESTCASE + 1; fi <= rowCount; fi++) { //Row row = sheet.getRow(fi); bool isRowNotTest = true; if (getCellValue(sheet, fi, 1) == null || getCellValue(sheet, fi, 2) == null) { rowCount = fi - 1; break; } SpecScenario scenario = new SpecScenario(); for (int se = 2; se <= columnCount; se++) { Color color = Color.Empty; string value = getCellValue(sheet, fi, se); /** * check if result indicate 'not_test' */ if (listSpecNodes[se - 2].Expression.Equals(AbstractSpecUserAction.RESULT)) { if (value.Equals(AbstractSpecUserAction.NOT_TEST)) { isRowNotTest = true; break; } else if (value.Equals(AbstractSpecUserAction.TEST) || value.Equals("")) { ; } else { myLog.Warn("[WARNING] Result must be 'Test' or 'Not Test', not " + value + ", it will be treated as 'Test' by default", logger); } } color = getCellBgColor(sheet, fi, se); if (color.Equals(Color.Empty) || ( !color.Equals(AbstractSpecUserAction.NOT_TEST_COLOR1) && !color.Equals(AbstractSpecUserAction.NOT_TEST_COLOR2))) { isRowNotTest = false; } scenario.UserActionsInString.Add(value); scenario.Colors.Add(color); } if (isRowNotTest) { continue; } /** * pre-condition */ string linkExpress = ""; string lastCellValue = getCellValue(sheet, fi, columnCount + 1); if (link && lastCellValue != null && !lastCellValue.Equals("")) { linkExpress = lastCellValue; string[] preConsExp = linkExpress.Split(','); foreach (string preConExp in preConsExp) { scenario.PreConditionsExpression.Add(preConExp.Trim()); } } scenario.Id = fi - ROW_TITLE_TESTCASE; //scenario.ScreenName = screen.Name; if (screen.Scenarios == null) { screen.Scenarios = new List <IScenario>(); } screen.Scenarios.Add(scenario); } //release com objects to fully kill excel process from running in the background Marshal.ReleaseComObject(xlRange); Marshal.ReleaseComObject(sheet); return(screen); }
private void DoExpand(SpecScreen screen, string pathToApp, MyLog myLog, List <ClassExpression> allClassesExp) { for (int fi = 0; fi < screen.Scenarios.Count; fi++) { IScenario tempScenario = screen.Scenarios[fi]; if (tempScenario is SpecScenario) { SpecScenario scenario = tempScenario as SpecScenario; List <string> listActionExp = scenario.UserActionsInString; List <Color> colors = scenario.Colors; for (int se = 0; se < listActionExp.Count; se++) { string actionExp = listActionExp[se]; if (actionExp == null) { myLog.Error("NULL at (" + (fi + 1) + "," + (se + 2) + ")"); continue; } SpecNode specNode = screen.ListSpecNodes[se]; ScriptGenerationParams _params = null; AbstractSpecUserAction specUserAction = null; int lastInd = IsValidationUserCode(se, screen.ListValidationUserCode); if (lastInd > se) { specUserAction = new ValidationUserCodeSpecUserAction(); _params = new ValidationUCScriptGenerationParams(); List <string> listExp = new List <string>(); for (int th = se; th <= lastInd; th++) { String tempActionExp = listActionExp[th]; AbstractSpecUserAction temp = handleUserActionExpression( tempActionExp.Trim(), specNode, colors[th], myLog); if (!(temp is ValidationSpecUserAction)) { myLog.Warn("Expression: " + tempActionExp + " must be validation", logger); screen = null; return; } else { if (tempActionExp != null && !tempActionExp.Trim().Equals("")) { listExp.Add(tempActionExp.Trim()); } } } (specUserAction as ValidationUserCodeSpecUserAction).ListExps = listExp; ((ValidationUCScriptGenerationParams)_params).ListExps = listExp; if (screen is TestSpecificationScreen) { ((ValidationUCScriptGenerationParams)_params).ClassExpressions = allClassesExp; } ((ValidationUCScriptGenerationParams)_params).ClassName = AbstractSpecUserAction.GetFolderNameFromScreen(screen) + "_Validation"; ((ValidationUCScriptGenerationParams)_params).FunctionName = "Validate_" + Regex.Replace(specNode.Expression, "[^A-Za-z0-9]", "_"); SetAttributes(scenario, specUserAction, specNode, screen, pathToApp, colors, _params, se, myLog); se = lastInd; } else { string[] listActionsEpx = splitAction(actionExp, AbstractSpecUserAction.AND); foreach (string action in listActionsEpx) { specUserAction = handleUserActionExpression(action.Trim(), specNode, colors[se], myLog); if (specUserAction != null) { if (specUserAction is UserCodeSpecUserAction userCodeExpression) { _params = new UserCodeScriptGenerationParams(); ((UserCodeScriptGenerationParams)_params).MapAliasWithNode = screen.MappingAliasWithNode; if (screen is TestSpecificationScreen testSpecScreen) { ((UserCodeScriptGenerationParams)_params).ClassExpressions = allClassesExp; } } else if (specUserAction is WaitValidateSpecUserAction waitUserAction) { _params = new WaitValidateScriptGenerationParams(); } else { _params = new ScriptGenerationParams(); } SetAttributes(scenario, specUserAction, specNode, screen, pathToApp, colors, _params, se, myLog); } } } } } } }
/// <summary> /// type expression /// </summary> /// <param name="actionEpx"></param> /// <param name="specNode"></param> /// <param name="color"></param> /// <param name="myLog"></param> /// <returns></returns> private AbstractSpecUserAction handleUserActionExpression(String actionEpx, SpecNode specNode, Color color, MyLog myLog) { if (actionEpx == null || actionEpx.Equals("") || actionEpx.Equals(AbstractSpecUserAction.NA) || actionEpx.Equals(AbstractSpecUserAction.DO_NOTHING) || actionEpx.Equals(AbstractSpecUserAction.EMPTY) || color.Equals(AbstractSpecUserAction.NOT_TEST_COLOR1) || color.Equals(AbstractSpecUserAction.NOT_TEST_COLOR2)) // || // color.Equals(AbstractSpecUserAction.NOT_TEST_COLOR3)) { return(null); } /** * determine if delay, wait, keyboard press */ if (specNode.UIElement == null) { // delay if (Regex.IsMatch(specNode.Expression, AbstractSpecUserAction.DELAY_TITLE_EXPRESSION)) { DelaySpecUserAction delaySpecUserAction = new DelaySpecUserAction(); delaySpecUserAction.Expression = actionEpx; return(delaySpecUserAction); } // wait else if (Regex.IsMatch(specNode.Expression, AbstractSpecUserAction.PRE_WAIT + "(_\\d*)*")) { PreWaitValidateSpecUserAction waitValidateSpecUserAction = new PreWaitValidateSpecUserAction(); waitValidateSpecUserAction.Expression = actionEpx; return(waitValidateSpecUserAction); } else if (Regex.IsMatch(specNode.Expression, AbstractSpecUserAction.POST_WAIT + "(_\\d*)*")) { PostWaitValidateSpecUserAction waitValidateSpecUserAction = new PostWaitValidateSpecUserAction(); waitValidateSpecUserAction.Expression = actionEpx; return(waitValidateSpecUserAction); } //keyboard press else if (Regex.IsMatch(specNode.Expression, AbstractSpecUserAction.KEYBOARD_PRESS_REGEX)) { KeyboardSpecUserAction keyboardPressSpecUserAction = new KeyboardSpecUserAction(); keyboardPressSpecUserAction.Expression = actionEpx; return(keyboardPressSpecUserAction); } //test - not test else if (Regex.IsMatch(specNode.Expression, AbstractSpecUserAction.RESULT)) { return(null); } // usercode else if (Regex.IsMatch(specNode.Expression, AbstractSpecUserAction.USER_CODE) || Regex.IsMatch(specNode.Expression, AbstractSpecUserAction.USER_CODE_WITH_VARIABLE_DECLARE)) { UserCodeSpecUserAction userCodeExpression = new UserCodeSpecUserAction(); userCodeExpression.Expression = actionEpx; return(userCodeExpression); } } AbstractSpecUserAction specUserAction = null; string attribute = specNode.Attribute; if (actionEpx.Equals(AbstractSpecUserAction.OPEN)) { specUserAction = new OpenAppSpecUserAction(); } else if (actionEpx.Equals(AbstractSpecUserAction.CLOSE)) { specUserAction = new CloseAppSpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.CLICK)) { specUserAction = new ClickSpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.DOUBLE_CLICK)) { specUserAction = new DoubleClickSpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.RIGHT_CLICK)) { specUserAction = new RightClickSpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.WAIT_EXIST) || actionEpx.StartsWith(AbstractSpecUserAction.WAIT_NOT_EXIST)) { specUserAction = new WaitExistSpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.EXIST) || actionEpx.StartsWith(AbstractSpecUserAction.NOT_EXIST)) { specUserAction = new CheckExistSpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.DELAY)) { specUserAction = new DelaySpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.GET)) { specUserAction = new GetSpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.CAPTURE)) { specUserAction = new CaptureSpecUserAction(); } else if (actionEpx.StartsWith(AbstractSpecUserAction.DROP)) { specUserAction = new DropSpecUserAction(); } //else if (actionEpx.StartsWith(AbstractSpecUserAction.COMPARE_IMAGE)) //{ // specUserAction = new ImageComparativeSpecUserAction(); // specUserAction.Expression = actionEpx; //} else if (actionEpx.StartsWith(AbstractSpecUserAction.ATTRIBUTE_CONTAIN) || actionEpx.StartsWith(AbstractSpecUserAction.ATTRIBUTE_NOT_CONTAIN)) { specUserAction = new ContainSpecUserAction(); } else if (color.Equals(AbstractSpecUserAction.VALIDATION_COLOR) || color.Equals(AbstractSpecUserAction.ENVIRONMENT_COLOR)) { specUserAction = new ValidationSpecUserAction(); } else if (color.Equals(AbstractSpecUserAction.PROCEDURES_COLOR) || color.Equals(AbstractSpecUserAction.PRE_CONDITION_COLOR)) { if (attribute != null && !attribute.Equals("") && (Regex.IsMatch(attribute, "^\\w+$") || Regex.IsMatch(attribute, ".*\\..*"))) { specUserAction = new SetSpecUserAction(); } else if (attribute == null || attribute.Equals("") || Regex.IsMatch(attribute, "Cell\\s*\\((\\d+?),\\s*(\\d+?)\\)") || Regex.IsMatch(attribute, "Row\\s*\\((\\d+?)\\)")) { specUserAction = new PressSpecUserAction(); } } // add new case to handle here else { myLog.Warn("Incorrect expression: " + actionEpx); } if (specUserAction != null) { specUserAction.Expression = actionEpx; } return(specUserAction); }