public CommandReturn Swipe(string target)
        {
            var returnValue = new CommandReturn();

            try
            {
                dynamic jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(target);

                int startx   = jsonResponse.startx;
                int starty   = jsonResponse.starty;
                int endx     = jsonResponse.endx;
                int endy     = jsonResponse.endy;
                int duration = jsonResponse.duration;

                Swipe(TouchActions, startx, starty, endx, endy, duration);

                returnValue.resultValue = ResultValue.COMMAND_OK;

                Thread.Sleep(500);
            }
            catch (Exception ex)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = "Swipe failed : " + ex.Message;
            }
            return(returnValue);
        }
        public CommandReturn SelectAndWait(string target, string value, int timeout)
        {
            var returnValue = new CommandReturn();

            try
            {
                string errorMessage = "";
                var    element      = GetElement(target, ref errorMessage);
                if (element != null)
                {
                    Thread.Sleep(2000);

                    var selectElementW = new SelectElement(element);
                    SelectOption(selectElementW, value);
                    returnValue.resultValue = ResultValue.COMMAND_OK;
                }
                else
                {
                    returnValue.resultValue = ResultValue.ERROR;
                    returnValue.message     = errorMessage;
                }
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }
            return(returnValue);
        }
        public CommandReturn BackGroundApp(string target, string value)
        {
            var returnCommand = new CommandReturn();

            try
            {
                int  time = 1;
                bool res  = int.TryParse(target, out time);

                if (res)
                {
                    AppInteraction?.BackgroundApp(time);
                    returnCommand.resultValue = ResultValue.COMMAND_OK;
                }
                else
                {
                    returnCommand.resultValue = ResultValue.ERROR;
                    returnCommand.message     = "wrong parameters : time in seconds awaited for target";
                }
            }
            catch (Exception ex)
            {
                returnCommand.resultValue = ResultValue.ERROR;
                returnCommand.message     = ex.Message;
            }

            return(returnCommand);
        }
        public CommandReturn Echo(string target, string value, out string stepValue)
        {
            var returnValue = new CommandReturn();

            stepValue = target;
            returnValue.resultValue = ResultValue.COMMAND_OK;

            return(returnValue);
        }
        public CommandReturn Tap(string target, string value)
        {
            var returnValue = new CommandReturn();

            try
            {
                int nbOfFingers = 1;
                try
                {
                    nbOfFingers = String.IsNullOrEmpty(value) ? 1 : Convert.ToInt32(value);
                }
                catch
                {
                }

                if (target.StartsWith("{x:"))
                {
                    dynamic jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(target);

                    int x = jsonResponse.x;
                    int y = jsonResponse.y;

                    Thread.Sleep(2000);

                    Tapxy(TouchActions, nbOfFingers, x, y);

                    returnValue.resultValue = ResultValue.COMMAND_OK;
                }
                else
                {
                    string errorMessage = "";

                    var element = GetElement(target, ref errorMessage);
                    if (element != null)
                    {
                        Thread.Sleep(2000);

                        Tap(TouchActions, nbOfFingers, element);

                        returnValue.resultValue = ResultValue.COMMAND_OK;
                    }
                    else
                    {
                        returnValue.resultValue = ResultValue.ERROR;
                        returnValue.message     = errorMessage;
                    }
                }
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }
            return(returnValue);
        }
        public CommandReturn WaitForVisible(string target, string value, int timeout)
        {
            var returnValue = new CommandReturn();

            try
            {
                var stopwatch = Stopwatch.StartNew();
                stopwatch.Restart();
                string      errorMessage = "";
                var         firstLoop    = true;
                IWebElement element;

                do
                {
                    element = _GetElement(target, ref errorMessage);

                    if (element == null || !element.Displayed)
                    {
                        Thread.Sleep(500);
                    }
                    else
                    {
                        break;
                    }

                    if (firstLoop)
                    {
                        firstLoop = false;
                    }
                } while (stopwatch.ElapsedMilliseconds < Convert.ToDouble(timeout));

                if (element == null || !element.Displayed)
                {
                    returnValue.resultValue = ResultValue.ERROR;
                    returnValue.message     = "Timed out before finding the target element";
                }
                else
                {
                    returnValue.resultValue = ResultValue.COMMAND_OK;
                }
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }

            return(returnValue);
        }
        public CommandReturn DeviceOrientation(string orientation)
        {
            var returnValue = new CommandReturn();

            try
            {
                Rotatable.Orientation = orientation.ToLower() == "landscape" ? ScreenOrientation.Landscape : ScreenOrientation.Portrait;

                returnValue.resultValue = ResultValue.COMMAND_OK;
                return(returnValue);
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }
            return(returnValue);
        }
        public CommandReturn PageSource(out string stepValue)
        {
            var returnValue = new CommandReturn();

            string pageSource = "";

            try
            {
                pageSource = AppiumDriver.PageSource;

                stepValue = pageSource;
                returnValue.resultValue = ResultValue.COMMAND_OK;
            }
            catch (Exception)
            {
                stepValue = pageSource;
                returnValue.resultValue = ResultValue.ERROR;
            }
            return(returnValue);
        }
        public CommandReturn VerifyElementPresent(string target, string value)
        {
            var returnValue = new CommandReturn();

            try
            {
                var    result       = false;
                string errorMessage = "";
                var    element      = GetElement(target, ref errorMessage);
                result = element != null;

                returnValue.resultValue = result ? ResultValue.VERIFICATION_OK : ResultValue.VERIFICATION_ERROR;
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }
            return(returnValue);
        }
        public CommandReturn Click(string target)
        {
            var returnValue = new CommandReturn();

            try
            {
                string errorMessage = "";
                var    element      = GetElement(target, ref errorMessage);

                if (element != null)
                {
                    Thread.Sleep(2000);

                    if (element.Displayed)
                    {
                        element.Click();
                    }
                    else
                    {
                        var location = element.Location;
                        Tapxy(TouchActions, 1, location.X, location.Y);
                    }


                    returnValue.resultValue = ResultValue.COMMAND_OK;
                }
                else
                {
                    returnValue.resultValue = ResultValue.ERROR;
                    returnValue.message     = errorMessage;
                }
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }
            return(returnValue);
        }
        public CommandReturn StoreVisible(string target, string value, ref Dictionary <string, string> seleniumVariables)
        {
            var returnValue = new CommandReturn();

            try
            {
                string errorMessage = "";
                var    element      = _GetElement(target, ref errorMessage);

                if (seleniumVariables.ContainsKey(value))
                {
                    seleniumVariables.Remove(value);
                }

                if (element != null)
                {
                    if (element.Displayed)
                    {
                        seleniumVariables.Add(value, "true");
                    }
                    else
                    {
                        seleniumVariables.Add(value, "false");
                    }
                }
                else
                {
                    seleniumVariables.Add(value, "false");
                }
                returnValue.resultValue = ResultValue.COMMAND_OK;
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }

            return(returnValue);
        }
        public CommandReturn SendKeys(string target, string value)
        {
            var returnValue = new CommandReturn();

            try
            {
                string errorMessage = "";
                var    element      = GetElement(target, ref errorMessage);

                if (element != null)
                {
                    Thread.Sleep(2000);


                    if (element.Displayed)
                    {
                        element.SendKeys(value);
                    }
                    else
                    {
                        returnValue.resultValue = ResultValue.ERROR;
                        return(returnValue);
                    }
                    returnValue.resultValue = ResultValue.COMMAND_OK;
                }
                else
                {
                    returnValue.resultValue = ResultValue.ERROR;
                    returnValue.message     = errorMessage;
                }
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }
            return(returnValue);
        }
        public CommandReturn VerifyText(string target, string value)
        {
            var  returnValue = new CommandReturn();
            bool result;

            try
            {
                string errorMessage = "";
                var    element      = GetElement(target, ref errorMessage);

                if (element == null)
                {
                    returnValue.resultValue = ResultValue.ERROR;
                    returnValue.message     = errorMessage;
                    return(returnValue);
                }
                var nameAtt = element.GetAttribute("name");

                if (value.Contains("regexp:"))
                {
                    var pattern = @value.Replace("regexp:", "");
                    var regex   = new System.Text.RegularExpressions.Regex(pattern);
                    result = regex.IsMatch(element.Text.TrimStart(' ').TrimEnd(' '));
                    var textDisplayed = element.Text;
                    if (!result)
                    {
                        if (nameAtt != null)
                        {
                            var alternativText = element.GetAttribute("name");
                            result        = regex.IsMatch(alternativText.TrimStart(' ').TrimEnd(' '));
                            textDisplayed = alternativText;
                        }
                        if (!result)
                        {
                            returnValue.message = textDisplayed + " doesn't match with [" + value + "]";
                        }
                    }
                }
                else if (value.Contains("regexpi:"))
                {
                    var pattern = @value.Replace("regexpi:", "").ToLowerInvariant();
                    var regex   = new System.Text.RegularExpressions.Regex(pattern);
                    result = regex.IsMatch(element.Text.ToLowerInvariant().TrimStart(' ').TrimEnd(' '));
                    var textDisplayed = element.Text;
                    if (!result)
                    {
                        if (nameAtt != null)
                        {
                            var alternativText = element.GetAttribute("name");
                            result        = regex.IsMatch(alternativText.TrimStart(' ').TrimEnd(' '));
                            textDisplayed = alternativText;
                        }
                        if (!result)
                        {
                            returnValue.message = textDisplayed + " doesn't match with [" + value + "]";
                        }
                    }
                }
                else
                {
                    result = String.Equals(element.Text.ToLowerInvariant().TrimStart(' ').TrimEnd(' '), value.ToLowerInvariant().TrimStart(' ').TrimEnd(' '));
                    var textDisplayed = element.Text;
                    if (!result)
                    {
                        if (nameAtt != null)
                        {
                            var alternativText = element.GetAttribute("name");
                            result        = String.Equals(alternativText.ToLowerInvariant().TrimStart(' ').TrimEnd(' '), value.ToLowerInvariant().TrimStart(' ').TrimEnd(' '));
                            textDisplayed = alternativText;
                        }
                        if (!result)
                        {
                            returnValue.message = textDisplayed + " doesn't match with [" + value + "]";
                        }
                    }
                }
                returnValue.isOk        = result;
                returnValue.resultValue = result ? ResultValue.VERIFICATION_OK : ResultValue.VERIFICATION_ERROR;
                return(returnValue);
            }
            catch (Exception e)
            {
                returnValue.resultValue = ResultValue.ERROR;
                returnValue.message     = e.Message;
            }
            return(returnValue);
        }
Example #14
0
        public CommandReturn ExecuteCommand(string command, string target, string value, string condition, int?timeout, ref Dictionary <string, string> persistantVariables, out string stepValue)
        {
            stepValue = "";
            CommandReturn ret;

            bool playStep = true;

            // Option : Conditional execution (Selenium condition comment <-- condition=${variable1} == 5 --> )
            if (!String.IsNullOrEmpty(condition))
            {
                // internal variables
                foreach (var variable in persistantVariables)
                {
                    condition = condition.Replace("${" + variable.Key + "}", variable.Value);
                }

                // eval condition
                try
                {
                    var expression2 = condition.Replace("\"", "'").ToLower();
                    var expression  = new CompiledExpression()
                    {
                        StringToParse = expression2
                    };
                    var  result     = expression.Eval();
                    bool?boolResult = result as bool?;
                    playStep = (boolResult ?? true) != false;
                }
                catch (Exception ex)
                {
                    var commandReturn = new CommandReturn();
                    commandReturn.resultValue = ResultValue.ERROR;
                    commandReturn.isOk        = false;
                    commandReturn.message     = "Expression evaluator error: " + ex.Message;
                    return(commandReturn);
                }
            }

            if (!playStep)
            {
                var commandReturn = new CommandReturn();
                commandReturn.resultValue = ResultValue.COMMAND_OK;
                commandReturn.isOk        = true;
                commandReturn.message     = "Step not played by condition";
                return(commandReturn);
            }

            try
            {
                stepValue = "";
                string sPattern = @"\${\w*?}";

                if (System.Text.RegularExpressions.Regex.IsMatch(target, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                {
                    if (persistantVariables != null)
                    {
                        var rgx = new System.Text.RegularExpressions.Regex(sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                        foreach (System.Text.RegularExpressions.Match m in rgx.Matches(target))
                        {
                            if (!string.IsNullOrWhiteSpace(m.Value))
                            {
                                string varFound = persistantVariables[m.Value.Replace("${", "").Replace("}", "")];
                                target = target.Replace(m.Value, varFound);
                            }
                        }
                    }
                }

                if (System.Text.RegularExpressions.Regex.IsMatch(value, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
                {
                    if (persistantVariables != null)
                    {
                        var rgx = new System.Text.RegularExpressions.Regex(sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                        foreach (System.Text.RegularExpressions.Match m in rgx.Matches(value))
                        {
                            if (!string.IsNullOrWhiteSpace(m.Value))
                            {
                                string varFound = persistantVariables[m.Value.Replace("${", "").Replace("}", "")];
                                value = value.Replace(m.Value, varFound);
                            }
                        }
                    }
                }

                if (_appiumMetaPilot == null)
                {
                    var commandReturn = new CommandReturn();
                    commandReturn.resultValue = ResultValue.ERROR;
                    commandReturn.isOk        = false;
                    commandReturn.message     = "appiumPilot is null";

                    return(commandReturn);
                }

                if (_appiumMetaPilot.AppiumDriver == null)
                {
                    var commandReturn = new CommandReturn();
                    commandReturn.resultValue = ResultValue.ERROR;
                    commandReturn.message     = "iosDriver & androidDriver are null";
                    return(commandReturn);
                }


                switch (command)
                {
                case "backGroundApp":
                    ret = _appiumMetaPilot.BackGroundApp(target, value);
                    break;

                case "click":
                case "clickAndWait":
                    ret = _appiumMetaPilot.Click(target);
                    break;

                case "sendkeys":
                case "sendKeys":
                case "type":
                    ret = _appiumMetaPilot.SendKeys(target, value);
                    break;

                case "verifyText":
                    ret = _appiumMetaPilot.VerifyText(target, value);
                    break;

                case "tap":
                    ret = _appiumMetaPilot.Tap(target, value);
                    break;

                case "waitForElementPresent":
                case "waitforvisible":
                    ret = _appiumMetaPilot.WaitForVisible(target, value, timeout ?? 30);
                    break;

                case "deviceOrientation":
                    ret = _appiumMetaPilot.DeviceOrientation(target);
                    break;

                case "echo":
                    ret = _appiumMetaPilot.Echo(target, value, out stepValue);
                    break;

                case "storeVisible":
                    ret = _appiumMetaPilot.StoreVisible(target, value, ref persistantVariables);
                    break;

                case "swipe":
                    ret = _appiumMetaPilot.Swipe(target);
                    break;

                case "selectAndWait":
                    ret = _appiumMetaPilot.SelectAndWait(target, value, timeout ?? 30);
                    break;

                case "assertElementPresent":
                case "verifyElementPresent":
                    ret = _appiumMetaPilot.VerifyElementPresent(target, value);
                    break;

                case "pause":
                case "Pause":
                    var returnValue = new CommandReturn();
                    if (!int.TryParse(target, out var secs))
                    {
                        secs = 0;
                    }
                    Thread.Sleep(secs);
                    returnValue.resultValue = ResultValue.COMMAND_OK;
                    ret = returnValue;
                    break;

                default:
                    ret             = new CommandReturn();
                    ret.resultValue = ResultValue.INVALID_COMMAND;
                    break;
                }
                if (ret.resultValue == ResultValue.COMMAND_OK || ret.resultValue == ResultValue.VERIFICATION_OK)
                {
                    ret.isOk = true;
                }
                else
                {
                    ret.isOk = false;
                }
            }
            catch (Exception ex)
            {
                ret             = new CommandReturn();
                ret.resultValue = ResultValue.ERROR;
                ret.message     = "Error in ExecuteCommand()";
                ret.isOk        = false;
            }
            return(ret);
        }