Beispiel #1
0
        public override void RunCommand(object sender, Core.Script.ScriptAction parentCommand)
        {
            var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;



            bool ifResult = false;


            if (v_IfActionType == "Value")
            {
                string value1 = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                  where rw.Field <string>("Parameter Name") == "Value1"
                                  select rw.Field <string>("Parameter Value")).FirstOrDefault());
                string operand = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                   where rw.Field <string>("Parameter Name") == "Operand"
                                   select rw.Field <string>("Parameter Value")).FirstOrDefault());
                string value2 = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                  where rw.Field <string>("Parameter Name") == "Value2"
                                  select rw.Field <string>("Parameter Value")).FirstOrDefault());

                value1 = value1.ConvertToUserVariable(sender);
                value2 = value2.ConvertToUserVariable(sender);



                decimal cdecValue1, cdecValue2;

                switch (operand)
                {
                case "is equal to":
                    ifResult = (value1 == value2);
                    break;

                case "is not equal to":
                    ifResult = (value1 != value2);
                    break;

                case "is greater than":
                    cdecValue1 = Convert.ToDecimal(value1);
                    cdecValue2 = Convert.ToDecimal(value2);
                    ifResult   = (cdecValue1 > cdecValue2);
                    break;

                case "is greater than or equal to":
                    cdecValue1 = Convert.ToDecimal(value1);
                    cdecValue2 = Convert.ToDecimal(value2);
                    ifResult   = (cdecValue1 >= cdecValue2);
                    break;

                case "is less than":
                    cdecValue1 = Convert.ToDecimal(value1);
                    cdecValue2 = Convert.ToDecimal(value2);
                    ifResult   = (cdecValue1 < cdecValue2);
                    break;

                case "is less than or equal to":
                    cdecValue1 = Convert.ToDecimal(value1);
                    cdecValue2 = Convert.ToDecimal(value2);
                    ifResult   = (cdecValue1 <= cdecValue2);
                    break;
                }
            }
            else if (v_IfActionType == "Date Compare")
            {
                string value1 = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                  where rw.Field <string>("Parameter Name") == "Value1"
                                  select rw.Field <string>("Parameter Value")).FirstOrDefault());
                string operand = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                   where rw.Field <string>("Parameter Name") == "Operand"
                                   select rw.Field <string>("Parameter Value")).FirstOrDefault());
                string value2 = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                  where rw.Field <string>("Parameter Name") == "Value2"
                                  select rw.Field <string>("Parameter Value")).FirstOrDefault());

                value1 = value1.ConvertToUserVariable(sender);
                value2 = value2.ConvertToUserVariable(sender);



                DateTime dt1, dt2;
                dt1 = DateTime.Parse(value1);
                dt2 = DateTime.Parse(value2);
                switch (operand)
                {
                case "is equal to":
                    ifResult = (dt1 == dt2);
                    break;

                case "is not equal to":
                    ifResult = (dt1 != dt2);
                    break;

                case "is greater than":
                    ifResult = (dt1 > dt2);
                    break;

                case "is greater than or equal to":
                    ifResult = (dt1 >= dt2);
                    break;

                case "is less than":
                    ifResult = (dt1 < dt2);
                    break;

                case "is less than or equal to":
                    ifResult = (dt1 <= dt2);
                    break;
                }
            }
            else if (v_IfActionType == "Variable Compare")
            {
                string value1 = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                  where rw.Field <string>("Parameter Name") == "Value1"
                                  select rw.Field <string>("Parameter Value")).FirstOrDefault());
                string operand = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                   where rw.Field <string>("Parameter Name") == "Operand"
                                   select rw.Field <string>("Parameter Value")).FirstOrDefault());
                string value2 = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                  where rw.Field <string>("Parameter Name") == "Value2"
                                  select rw.Field <string>("Parameter Value")).FirstOrDefault());

                string caseSensitive = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                         where rw.Field <string>("Parameter Name") == "Case Sensitive"
                                         select rw.Field <string>("Parameter Value")).FirstOrDefault());

                value1 = value1.ConvertToUserVariable(sender);
                value2 = value2.ConvertToUserVariable(sender);

                if (caseSensitive == "No")
                {
                    value1 = value1.ToUpper();
                    value2 = value2.ToUpper();
                }



                switch (operand)
                {
                case "contains":
                    ifResult = (value1.Contains(value2));
                    break;

                case "does not contain":
                    ifResult = (!value1.Contains(value2));
                    break;

                case "is equal to":
                    ifResult = (value1 == value2);
                    break;

                case "is not equal to":
                    ifResult = (value1 != value2);
                    break;
                }
            }
            else if (v_IfActionType == "Variable Has Value")
            {
                string variableName = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                        where rw.Field <string>("Parameter Name") == "Variable Name"
                                        select rw.Field <string>("Parameter Value")).FirstOrDefault());

                var actualVariable = variableName.ConvertToUserVariable(sender).Trim();

                if (!string.IsNullOrEmpty(actualVariable))
                {
                    ifResult = true;
                }
                else
                {
                    ifResult = false;
                }
            }
            else if (v_IfActionType == "Variable Is Numeric")
            {
                string variableName = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                        where rw.Field <string>("Parameter Name") == "Variable Name"
                                        select rw.Field <string>("Parameter Value")).FirstOrDefault());

                var actualVariable = variableName.ConvertToUserVariable(sender).Trim();

                var numericTest = decimal.TryParse(actualVariable, out decimal parsedResult);

                if (numericTest)
                {
                    ifResult = true;
                }
                else
                {
                    ifResult = false;
                }
            }
            else if (v_IfActionType == "Error Occured")
            {
                //get line number
                string userLineNumber = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                          where rw.Field <string>("Parameter Name") == "Line Number"
                                          select rw.Field <string>("Parameter Value")).FirstOrDefault());

                //convert to variable
                string variableLineNumber = userLineNumber.ConvertToUserVariable(sender);

                //convert to int
                int lineNumber = int.Parse(variableLineNumber);

                //determine if error happened
                if (engine.ErrorsOccured.Where(f => f.LineNumber == lineNumber).Count() > 0)
                {
                    var error = engine.ErrorsOccured.Where(f => f.LineNumber == lineNumber).FirstOrDefault();
                    error.ErrorMessage.StoreInUserVariable(sender, "Error.Message");
                    error.LineNumber.ToString().StoreInUserVariable(sender, "Error.Line");
                    error.StackTrace.StoreInUserVariable(sender, "Error.StackTrace");

                    ifResult = true;
                }
                else
                {
                    ifResult = false;
                }
            }
            else if (v_IfActionType == "Error Did Not Occur")
            {
                //get line number
                string userLineNumber = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                          where rw.Field <string>("Parameter Name") == "Line Number"
                                          select rw.Field <string>("Parameter Value")).FirstOrDefault());

                //convert to variable
                string variableLineNumber = userLineNumber.ConvertToUserVariable(sender);

                //convert to int
                int lineNumber = int.Parse(variableLineNumber);

                //determine if error happened
                if (engine.ErrorsOccured.Where(f => f.LineNumber == lineNumber).Count() == 0)
                {
                    ifResult = true;
                }
                else
                {
                    var error = engine.ErrorsOccured.Where(f => f.LineNumber == lineNumber).FirstOrDefault();
                    error.ErrorMessage.StoreInUserVariable(sender, "Error.Message");
                    error.LineNumber.ToString().StoreInUserVariable(sender, "Error.Line");
                    error.StackTrace.StoreInUserVariable(sender, "Error.StackTrace");

                    ifResult = false;
                }
            }
            else if (v_IfActionType == "Window Name Exists")
            {
                //get user supplied name
                string windowName = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                      where rw.Field <string>("Parameter Name") == "Window Name"
                                      select rw.Field <string>("Parameter Value")).FirstOrDefault());
                //variable translation
                string variablizedWindowName = windowName.ConvertToUserVariable(sender);

                //search for window
                IntPtr windowPtr = User32Functions.FindWindow(variablizedWindowName);

                //conditional
                if (windowPtr != IntPtr.Zero)
                {
                    ifResult = true;
                }
            }
            else if (v_IfActionType == "Active Window Name Is")
            {
                string windowName = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                      where rw.Field <string>("Parameter Name") == "Window Name"
                                      select rw.Field <string>("Parameter Value")).FirstOrDefault());

                string variablizedWindowName = windowName.ConvertToUserVariable(sender);

                var currentWindowTitle = User32Functions.GetActiveWindowTitle();

                if (currentWindowTitle == variablizedWindowName)
                {
                    ifResult = true;
                }
            }
            else if (v_IfActionType == "File Exists")
            {
                string fileName = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                    where rw.Field <string>("Parameter Name") == "File Path"
                                    select rw.Field <string>("Parameter Value")).FirstOrDefault());

                string trueWhenFileExists = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                              where rw.Field <string>("Parameter Name") == "True When"
                                              select rw.Field <string>("Parameter Value")).FirstOrDefault());

                var userFileSelected = fileName.ConvertToUserVariable(sender);

                bool existCheck = false;
                if (trueWhenFileExists == "It Does Exist")
                {
                    existCheck = true;
                }


                if (System.IO.File.Exists(userFileSelected) == existCheck)
                {
                    ifResult = true;
                }
            }
            else if (v_IfActionType == "Folder Exists")
            {
                string folderName = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                      where rw.Field <string>("Parameter Name") == "Folder Path"
                                      select rw.Field <string>("Parameter Value")).FirstOrDefault());

                string trueWhenFileExists = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                              where rw.Field <string>("Parameter Name") == "True When"
                                              select rw.Field <string>("Parameter Value")).FirstOrDefault());

                var userFolderSelected = folderName.ConvertToUserVariable(sender);

                bool existCheck = false;
                if (trueWhenFileExists == "It Does Exist")
                {
                    existCheck = true;
                }


                if (System.IO.Directory.Exists(folderName) == existCheck)
                {
                    ifResult = true;
                }
            }
            else if (v_IfActionType == "Web Element Exists")
            {
                string parameterName = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                         where rw.Field <string>("Parameter Name") == "Element Search Parameter"
                                         select rw.Field <string>("Parameter Value")).FirstOrDefault());

                string searchMethod = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                        where rw.Field <string>("Parameter Name") == "Element Search Method"
                                        select rw.Field <string>("Parameter Value")).FirstOrDefault());

                SeleniumBrowserElementActionCommand newElementActionCommand = new SeleniumBrowserElementActionCommand();
                newElementActionCommand.v_SeleniumSearchType = searchMethod;
                bool elementExists = newElementActionCommand.ElementExists(sender, searchMethod, parameterName);
                ifResult = elementExists;
            }
            else if (v_IfActionType == "GUI Element Exists")
            {
                string windowName = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                      where rw.Field <string>("Parameter Name") == "Window Name"
                                      select rw.Field <string>("Parameter Value")).FirstOrDefault().ConvertToUserVariable(sender));

                string elementSearchParam = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                              where rw.Field <string>("Parameter Name") == "Element Search Parameter"
                                              select rw.Field <string>("Parameter Value")).FirstOrDefault().ConvertToUserVariable(sender));

                string elementSearchMethod = ((from rw in v_IfActionParameterTable.AsEnumerable()
                                               where rw.Field <string>("Parameter Name") == "Element Search Method"
                                               select rw.Field <string>("Parameter Value")).FirstOrDefault().ConvertToUserVariable(sender));


                UIAutomationCommand newUIACommand = new UIAutomationCommand();
                newUIACommand.v_WindowName = windowName;
                newUIACommand.v_UIASearchParameters.Rows.Add(true, elementSearchMethod, elementSearchParam);
                var handle = newUIACommand.SearchForGUIElement(sender, windowName);

                if (handle is null)
                {
                    ifResult = false;
                }
                else
                {
                    ifResult = true;
                }
            }
            else
            {
                throw new Exception("If type not recognized!");
            }



            int startIndex, endIndex, elseIndex;

            if (parentCommand.AdditionalScriptCommands.Any(item => item.ScriptCommand is Core.Automation.Commands.ElseCommand))
            {
                elseIndex = parentCommand.AdditionalScriptCommands.FindIndex(a => a.ScriptCommand is Core.Automation.Commands.ElseCommand);

                if (ifResult)
                {
                    startIndex = 0;
                    endIndex   = elseIndex;
                }
                else
                {
                    startIndex = elseIndex + 1;
                    endIndex   = parentCommand.AdditionalScriptCommands.Count;
                }
            }
            else if (ifResult)
            {
                startIndex = 0;
                endIndex   = parentCommand.AdditionalScriptCommands.Count;
            }
            else
            {
                return;
            }

            for (int i = startIndex; i < endIndex; i++)
            {
                if ((engine.IsCancellationPending) || (engine.CurrentLoopCancelled))
                {
                    return;
                }

                engine.ExecuteCommand(parentCommand.AdditionalScriptCommands[i]);
            }
        }
Beispiel #2
0
            protected override void OnMessage(MessageEventArgs e)
            {
                Console.WriteLine(e.Data);
                JObject resp = JObject.Parse(e.Data);

                var command = new taskt.Core.Automation.Commands.SeleniumBrowserElementActionCommand();

                command.v_Comment = (string)resp.SelectToken("method") + " " +
                                    (string)resp.SelectToken("objectname") + " on " +
                                    (string)resp.SelectToken("page.title") + " page";

                command.v_WebActionParameterTable = new System.Data.DataTable
                {
                    TableName = "WebActionParamTable" + DateTime.Now.ToString("MMddyy.hhmmss")
                };
                command.v_WebActionParameterTable.Columns.Add("Parameter Name");
                command.v_WebActionParameterTable.Columns.Add("Parameter Value");

                string action = (string)resp.SelectToken("method");
                var    props  = resp.SelectToken("prop");
                // command.miscprops = new List<taskt.Core.KeyValuePair<string, string>>(7);
                var optional_props = new Dictionary <string, string>(7);

                if (!(props is null) && (!string.IsNullOrEmpty(props.ToString())))
                {
                    foreach (var prop in props)
                    {
                        JToken token = (JToken)prop;
                        if (!string.IsNullOrEmpty((string)token.SelectToken("val")))
                        {
                            optional_props.Add((string)token.SelectToken("prop"), (string)token.SelectToken("val"));
                        }
                    }
                }

                if (string.IsNullOrEmpty(action))
                {
                }

                if (action.Equals("Click"))
                {
                    command.v_SeleniumSearchType      = "Find Element By XPath";
                    command.v_SeleniumElementAction   = "Invoke Click";
                    command.v_SeleniumSearchParameter = (string)resp.SelectToken("prop[4].val");

                    var clickStep = new steps();
                    clickStep.id     = command.CommandID;
                    clickStep.action = command.v_SeleniumElementAction;
                    clickStep.compid = command.v_SeleniumSearchParameter;

                    App.Current.Dispatcher.Invoke((System.Action) delegate
                    {
                        StepsCollection.Add(clickStep);
                        lstScriptActions.Items.Add(CreateScriptCommandListViewItem(command));
                    });



                    //assign cell as a combobox

                    //  SAPScriptRecoder.outputList.Add(command);
                }

                if (action.Equals("Set") || action.Equals("setEncrypted"))
                {
                    command.v_SeleniumSearchType      = "Find Element By XPath";
                    command.v_SeleniumElementAction   = "Set Text";
                    command.v_SeleniumSearchParameter = (string)resp.SelectToken("prop[4].val");
                    command.v_WebActionParameterTable.Rows.Add(new Object[]
                                                               { "Text To Set", (string)resp.SelectToken("input") });
                    command.v_WebActionParameterTable.Rows.Add(new Object[]
                                                               { "Clear Element Before Setting Text", "No" });
                    DataGridViewComboBoxCell comparisonComboBox = new DataGridViewComboBoxCell();
                    comparisonComboBox.Items.Add("Yes");
                    comparisonComboBox.Items.Add("No");
                }

                if (action.Equals("selectByVisibleText"))
                {
                    command.v_SeleniumSearchType      = "Find Element By XPath";
                    command.v_SeleniumElementAction   = "Select By Visible Text";
                    command.v_SeleniumSearchParameter = (string)resp.SelectToken("prop[4].val");
                    command.v_WebActionParameterTable.Rows.Add(new Object[]
                                                               { "Text To Set", (string)resp.SelectToken("input") });
                    command.v_WebActionParameterTable.Rows.Add(new Object[]
                                                               { "Clear Element Before Setting Text", "No" });
                    DataGridViewComboBoxCell comparisonComboBox = new DataGridViewComboBoxCell();
                    comparisonComboBox.Items.Add("Yes");
                    comparisonComboBox.Items.Add("No");
                    //  SAPScriptRecoder.outputList.Add(command);
                }

                if (action.Equals("SwitchTab"))
                {
                    if (!string.IsNullOrEmpty((string)resp.SelectToken("input")))
                    {
                        var switchWindowCommand = new taskt.Core.Automation.Commands.SeleniumBrowserSwitchWindowCommand();
                        switchWindowCommand.v_WindowMatchType    = "Window Title";
                        switchWindowCommand.v_MatchSpecification = "Exact Match";
                        switchWindowCommand.v_MatchParameter     = (string)resp.SelectToken("input");
                    }
                }

                if (action.Equals("waitForPageLoaded"))
                {
                    var pausecommand = new taskt.Core.Automation.Commands.PauseCommand();
                    pausecommand.v_PauseLength = "10000";
                    // SAPScriptRecoder.outputList.Add(command);
                }
            }