public override IScenario Clone()
        {
            SpecScenario re = new SpecScenario();

            CopyAttributes(this, re);
            logger.Info("IScenario thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);

            if (this.colors != null)
            {
                re.Colors = new List <Color>();
                foreach (var color in this.colors)
                {
                    re.Colors.Add(Color.Green); //FromArgb(255, color));
                }
            }
            re.Id = id;
            if (this.preConditionsExpression != null)
            {
                re.PreConditionsExpression = new List <string>(this.preConditionsExpression);
            }
            if (this.userActionsInString != null)
            {
                re.UserActionsInString = new List <string>(this.UserActionsInString);
            }
            if (this.preConditions != null)
            {
                re.PreConditions = new List <IScenario>();
                foreach (var scenario in this.preConditions)
                {
                    re.PreConditions.Add(scenario.Clone());
                }
            }
            return(re);
        }
 private void SetAttributes(SpecScenario scenario, AbstractSpecUserAction specUserAction, SpecNode specNode,
                            IScreen screen, string pathToApp, List <Color> colors, ScriptGenerationParams _params, int se, MyLog myLog)
 {
     // importance: must create on the same thread with UI thread
     try
     {
         Application.Current.Dispatcher.Invoke((Action) delegate
         {
             specUserAction.BgColor = new System.Windows.Media.SolidColorBrush(
                 System.Windows.Media.Color.FromArgb(
                     255, colors[se].R, colors[se].G, colors[se].B));
         });
     }
     catch (Exception)
     {
         specUserAction.BgColor = new System.Windows.Media.SolidColorBrush(
             System.Windows.Media.Color.FromArgb(
                 255, colors[se].R, colors[se].G, colors[se].B));
     }
     _params.Color             = colors[se];
     _params.Id                = scenario.Id;
     _params.ListUIElements    = screen.AllUIElements;
     _params.MyLog             = myLog;
     _params.ScreenName        = screen.Name;
     _params.SpecNode          = specNode;
     _params.PathToApp         = pathToApp;
     specUserAction.Params     = _params;
     specUserAction.NodeAffect = specNode;
     if (scenario.UserActions == null)
     {
         scenario.UserActions = new List <IUserAction>();
     }
     scenario.UserActions.Add(specUserAction);
 }
 /// <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>
        /// handle Wait user action
        /// </summary>
        /// <param name="specScenario"></param>
        private void HandleWaitActions(SpecScenario specScenario)
        {
            int count = 0;

            for (int fi = 0; fi < specScenario.UserActions.Count; fi++)
            {
                var userAction = specScenario.UserActions[fi];
                if (userAction is PreWaitValidateSpecUserAction preWaitUserAction)
                {
                    var param = preWaitUserAction.Params as WaitValidateScriptGenerationParams;
                    param.ListUserActions = new List <AbstractSpecUserAction>();
                    int se = fi + 1;
                    while (se < specScenario.UserActions.Count &&
                           !(specScenario.UserActions[se] is WaitValidateSpecUserAction) &&
                           specScenario.UserActions[se] is AbstractSpecUserAction &&
                           (specScenario.UserActions[se] as AbstractSpecUserAction).Params.Color.Equals(
                               AbstractSpecUserAction.PRE_CONDITION_COLOR))
                    {
                        param.ListUserActions.Add(specScenario.UserActions[se] as AbstractSpecUserAction);
                        (specScenario.UserActions[se] as AbstractSpecUserAction).IgnoreGenerateScripts = true;
                        se++;
                    }
                    param.WaitIndex = count;
                    count++;
                }
                else if (userAction is PostWaitValidateSpecUserAction postWaitUserAction)
                {
                    var param = postWaitUserAction.Params as WaitValidateScriptGenerationParams;
                    param.ListUserActions = new List <AbstractSpecUserAction>();
                    int se = fi + 1;
                    while (se < specScenario.UserActions.Count &&
                           !(specScenario.UserActions[se] is WaitValidateSpecUserAction) &&
                           specScenario.UserActions[se] is AbstractSpecUserAction &&
                           (specScenario.UserActions[se] as AbstractSpecUserAction).Params.Color.Equals(
                               AbstractSpecUserAction.VALIDATION_COLOR))
                    {
                        param.ListUserActions.Add(specScenario.UserActions[se] as AbstractSpecUserAction);
                        (specScenario.UserActions[se] as AbstractSpecUserAction).IgnoreGenerateScripts = true;
                        se++;
                    }
                    param.WaitIndex = count;
                    count++;
                }
            }
        }
Ejemplo n.º 5
0
        /// <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);
        }
        /// <summary>
        /// return {function content, List[UserCodeScriptsExpression]}
        /// </summary>
        /// <param name="specScenario"></param>
        /// <param name="instanceName"></param>
        /// <param name="countLineBreak">determine new_line with blank line or only break new line</param>
        /// <param name="ignoreScenarioIfError">indicate whether ignore current scenario if an error occur</param>
        /// <returns></returns>
        protected Tuple <string, List <UserCodeScriptsExpression> > GenerateAScenario(SpecScenario specScenario,
                                                                                      MyLog myLog,
                                                                                      string instanceName        = INSTANCE_NAME,
                                                                                      int countLineBreak         = 1,
                                                                                      bool ignoreScenarioIfError = true)
        {
            string newLine     = string.Concat(Enumerable.Repeat(NEW_LINE, countLineBreak + 1));
            string funcContent = "";
            List <UserCodeScriptsExpression> listUcScriptExp = new List <UserCodeScriptsExpression>();
            List <IScenario> preConditions = specScenario.PreConditions;

            if (preConditions != null)
            {
                foreach (IScenario preCondition in preConditions)
                {
                    if (preCondition is SpecScenario preConditionSpec)
                    {
                        if (!funcContent.Equals(""))
                        {
                            funcContent += newLine;
                        }
                        Tuple <string, List <UserCodeScriptsExpression> > pair = GenerateAScenario(
                            preConditionSpec, myLog, instanceName, countLineBreak, ignoreScenarioIfError);
                        funcContent += pair.Item1;
                        var temp = pair.Item2;
                        if (temp != null && temp.Count > 0)
                        {
                            Utils.MergeUcScriptsEpx(listUcScriptExp, temp);
                            listUcScriptExp.AddRange(temp);
                        }
                    }
                    else
                    {
                        logger.Error("Not handled yet!");
                    }
                }
            }
            foreach (IUserAction userAction in specScenario.UserActions)
            {
                if (userAction is AbstractSpecUserAction specUserAction)
                {
                    if (specUserAction.IgnoreGenerateScripts)
                    {
                        continue;
                    }
                    if (!funcContent.Equals(""))
                    {
                        funcContent += newLine;
                    }
                    try
                    {
                        ScriptsExpression scriptsExpression = GenScriptType(specUserAction, instanceName);
                        // if error
                        if (scriptsExpression == null)
                        {
                            LogError(specUserAction.Expression, specUserAction.Params.ScreenName,
                                     specUserAction.Params.Id, myLog, specUserAction.NodeAffect.ToString());
                            if (ignoreScenarioIfError)
                            {
                                return(null); // ignore current scenario, process with the next one
                            }
                            // in contrast, continue with the remains UserAction-s
                            continue;
                        }
                        funcContent += scriptsExpression.Expression;
                        if (scriptsExpression is UserCodeScriptsExpression ucScriptsExp)
                        {
                            // remove duplicate function
                            Utils.MergeUcScriptsEpx(listUcScriptExp, ucScriptsExp);
                            listUcScriptExp.Add(ucScriptsExp);
                        }
                    }
                    catch (NotImplementedException)
                    {
                        logger.Error("Not implement with " + userAction.GetType().Name);
                    }
                }
                else
                {
                    logger.Error("Not handled yet!");
                }
            }
            return(new Tuple <string, List <UserCodeScriptsExpression> >(funcContent, listUcScriptExp));
        }
 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);
                         }
                     }
                 }
             }
         }
     }
 }