public bool DetermineStatementTruth(object sender)
        {
            var engine = (AutomationEngineInstance)sender;

            bool ifResult = false;

            if (v_IfActionType == "Value 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.ConvertUserVariableToString(engine);
                value2 = value2.ConvertUserVariableToString(engine);

                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.ConvertUserVariableToString(engine);
                value2 = value2.ConvertUserVariableToString(engine);

                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.ConvertUserVariableToString(engine);
                value2 = value2.ConvertUserVariableToString(engine);

                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.ConvertUserVariableToString(engine).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.ConvertUserVariableToString(engine).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.ConvertUserVariableToString(engine);

                //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(engine, "Error.Message");
                    error.LineNumber.ToString().StoreInUserVariable(engine, "Error.Line");
                    error.StackTrace.StoreInUserVariable(engine, "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.ConvertUserVariableToString(engine);

                //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(engine, "Error.Message");
                    error.LineNumber.ToString().StoreInUserVariable(engine, "Error.Line");
                    error.StackTrace.StoreInUserVariable(engine, "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.ConvertUserVariableToString(engine);

                //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.ConvertUserVariableToString(engine);

                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.ConvertUserVariableToString(engine);

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

                if (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.ConvertUserVariableToString(engine);

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

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

                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());

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


                SeleniumElementActionCommand newElementActionCommand = new SeleniumElementActionCommand();
                newElementActionCommand.v_SeleniumSearchType = searchMethod;
                newElementActionCommand.v_InstanceName       = instanceName.ConvertUserVariableToString(engine);
                bool elementExists = newElementActionCommand.ElementExists(sender, searchMethod, parameterName);
                ifResult = elementExists;

                if (trueWhenElementExists == "It Does Not Exist")
                {
                    ifResult = !ifResult;
                }
            }
            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().ConvertUserVariableToString(engine));

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

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

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

                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;
                }

                if (trueWhenElementExists == "It Does Not Exist")
                {
                    ifResult = !ifResult;
                }
            }
            else if (v_IfActionType == "Image Element Exists")
            {
                string imageName = (from rw in v_IfActionParameterTable.AsEnumerable()
                                    where rw.Field <string>("Parameter Name") == "Captured Image"
                                    select rw.Field <string>("Parameter Value")).FirstOrDefault();
                double accuracy;
                try
                {
                    accuracy = double.Parse((from rw in v_IfActionParameterTable.AsEnumerable()
                                             where rw.Field <string>("Parameter Name") == "Accuracy (0-1)"
                                             select rw.Field <string>("Parameter Value")).FirstOrDefault().ConvertUserVariableToString(engine));
                    if (accuracy > 1 || accuracy < 0)
                    {
                        throw new ArgumentOutOfRangeException("Accuracy value is out of range (0-1)");
                    }
                }
                catch (Exception)
                {
                    throw new InvalidDataException("Accuracy value is invalid");
                }

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

                var imageVariable = imageName.ConvertUserVariableToObject(engine);

                Bitmap capturedImage;
                if (imageVariable != null && imageVariable is Bitmap)
                {
                    capturedImage = (Bitmap)imageVariable;
                }
                else
                {
                    throw new ArgumentException("Provided Argument is not a 'Bitmap' Image");
                }

                SurfaceAutomationCommand surfaceACommand = new SurfaceAutomationCommand();
                var element = surfaceACommand.FindImageElement(capturedImage, accuracy);
                CommandControls.ShowAllForms();

                if (element != null)
                {
                    ifResult = true;
                }
                else
                {
                    ifResult = false;
                }

                if (trueWhenImageExists == "It Does Not Exist")
                {
                    ifResult = !ifResult;
                }
            }
            else
            {
                throw new Exception("If type not recognized!");
            }

            return(ifResult);
        }
Esempio n. 2
0
        public void SeleniumAction_SelectionChangeCommitted(object sender, EventArgs e)
        {
            SeleniumElementActionCommand cmd = this;
            DataTable actionParameters       = cmd.v_WebActionParameterTable;

            if (sender != null)
            {
                actionParameters.Rows.Clear();
            }

            switch (_elementActionDropdown.SelectedItem)
            {
            case "Invoke Click":
            case "Clear Element":
                foreach (var ctrl in _elementParameterControls)
                {
                    ctrl.Hide();
                }
                break;

            case "Left Click":
            case "Middle Click":
            case "Right Click":
            case "Double Left Click":
                foreach (var ctrl in _elementParameterControls)
                {
                    ctrl.Show();
                }

                if (sender != null)
                {
                    actionParameters.Rows.Add("X Adjustment", 0);
                    actionParameters.Rows.Add("Y Adjustment", 0);
                }
                break;

            case "Set Text":
                foreach (var ctrl in _elementParameterControls)
                {
                    ctrl.Show();
                }

                if (sender != null)
                {
                    actionParameters.Rows.Add("Text To Set");
                    actionParameters.Rows.Add("Clear Element Before Setting Text");
                    actionParameters.Rows.Add("Encrypted Text");
                    actionParameters.Rows.Add("Optional - Click to Encrypt 'Text To Set'");

                    DataGridViewComboBoxCell encryptedBox = new DataGridViewComboBoxCell();
                    encryptedBox.Items.Add("Not Encrypted");
                    encryptedBox.Items.Add("Encrypted");
                    _elementsGridViewHelper.Rows[2].Cells[1]       = encryptedBox;
                    _elementsGridViewHelper.Rows[2].Cells[1].Value = "Not Encrypted";

                    var buttonCell = new DataGridViewButtonCell();
                    _elementsGridViewHelper.Rows[3].Cells[1]       = buttonCell;
                    _elementsGridViewHelper.Rows[3].Cells[1].Value = "Encrypt Text";
                    _elementsGridViewHelper.CellContentClick      += ElementsGridViewHelper_CellContentClick;
                }

                DataGridViewComboBoxCell comparisonComboBox = new DataGridViewComboBoxCell();
                comparisonComboBox.Items.Add("Yes");
                comparisonComboBox.Items.Add("No");

                //assign cell as a combobox
                if (sender != null)
                {
                    _elementsGridViewHelper.Rows[1].Cells[1].Value = "No";
                }

                _elementsGridViewHelper.Rows[1].Cells[1] = comparisonComboBox;
                break;

            case "Set Secure Text":
                foreach (var ctrl in _elementParameterControls)
                {
                    ctrl.Show();
                }

                if (sender != null)
                {
                    actionParameters.Rows.Add("Secure String Variable");
                    actionParameters.Rows.Add("Clear Element Before Setting Text");
                }
                DataGridViewComboBoxCell _comparisonComboBox = new DataGridViewComboBoxCell();
                _comparisonComboBox.Items.Add("Yes");
                _comparisonComboBox.Items.Add("No");

                //assign cell as a combobox
                if (sender != null)
                {
                    _elementsGridViewHelper.Rows[1].Cells[1].Value = "No";
                }

                _elementsGridViewHelper.Rows[1].Cells[1] = _comparisonComboBox;
                break;

            case "Get Text":
            case "Get Matching Element(s)":
            case "Get Table":
            case "Get Count":
                foreach (var ctrl in _elementParameterControls)
                {
                    ctrl.Show();
                }

                if (sender != null)
                {
                    actionParameters.Rows.Add("Variable Name");
                }
                break;

            case "Get Attribute":
                foreach (var ctrl in _elementParameterControls)
                {
                    ctrl.Show();
                }

                if (sender != null)
                {
                    actionParameters.Rows.Add("Attribute Name");
                    actionParameters.Rows.Add("Variable Name");
                }
                break;

            case "Get Options":
                actionParameters.Rows.Add("Attribute Name");
                actionParameters.Rows.Add("Variable Name");
                break;

            case "Select Option":
                actionParameters.Rows.Add("Selection Type");
                actionParameters.Rows.Add("Selection Parameter");

                DataGridViewComboBoxCell selectionTypeBox = new DataGridViewComboBoxCell();
                selectionTypeBox.Items.Add("Select By Index");
                selectionTypeBox.Items.Add("Select By Text");
                selectionTypeBox.Items.Add("Select By Value");
                selectionTypeBox.Items.Add("Deselect By Index");
                selectionTypeBox.Items.Add("Deselect By Text");
                selectionTypeBox.Items.Add("Deselect By Value");
                selectionTypeBox.Items.Add("Deselect All");

                //assign cell as a combobox
                if (sender != null)
                {
                    _elementsGridViewHelper.Rows[0].Cells[1].Value = "Select By Text";
                }

                _elementsGridViewHelper.Rows[0].Cells[1] = selectionTypeBox;
                break;

            case "Wait For Element To Exist":
                foreach (var ctrl in _elementParameterControls)
                {
                    ctrl.Show();
                }

                if (sender != null)
                {
                    actionParameters.Rows.Add("Timeout (Seconds)");
                }
                break;

            case "Switch to frame":
                foreach (var ctrl in _elementParameterControls)
                {
                    ctrl.Hide();
                }
                break;

            default:
                break;
            }
            _elementsGridViewHelper.DataSource = v_WebActionParameterTable;
        }