Exemple #1
0
        //general method to handle actions

        //general method to handle action on existing elements

        //helper method to complete the work/actions



        //ensure good assertions

        //https://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp

        public ElementResult ExecuteAction(ElementObject ele, ElementResult starter = null)
        {
            if (TE.TestTimeLimit < DateTime.Now)
            {
                TE.Assert.Fail($"Test Exceeded Max Time Limit of: {TE.TestTimeLimit.Subtract(TE.StartTime)} (hh:mm:ss)");
            }

            var eleName = starter == null ? ele.ElementName : starter.ElementName;

            if (ele.ReportStep)
            {
                switch (ele.Action)
                {
                case ElementAction.Click:
                case ElementAction.EnterText:
                case ElementAction.Dropdown:
                case ElementAction.DropdownIndex:
                case ElementAction.RadioBtn:
                case ElementAction.MultiDropdown:
                case ElementAction.Hover:
                    TE.BeginTestCaseStep($"Execute action {ele.Action} on element: {eleName}",
                                         ele.Random || ele.ProtectedValue ? "Random Value" : ele.Text);
                    break;
                }
            }
            var element = starter;

            //check if 'ele' has an element in it or not.
            if (!string.IsNullOrEmpty(ele.EleRef))
            {
                if (ele.EleType == ElementType.AccessabilityId)
                {
                    element = FindAppiumElement(ele, starter?.RawEle);
                }
                else
                {
                    var finder = ElementFinder(ele);
                    element = FindElement(ele, finder, starter?.RawEle);
                }
            }

            return(CompleteAction(ele, element));
        }
Exemple #2
0
        public List <ElementResult> ExecuteActions(ElementObject ele, ElementResult starter = null)
        {
            if (TE.TestTimeLimit < DateTime.Now)
            {
                TE.Assert.Fail($"Test Exceeded Max Time Limit of: {TE.TestTimeLimit.Subtract(TE.StartTime).TotalSeconds} seconds");
            }

            var eleName = starter == null ? ele.ElementName : starter.ElementName;

            if (ele.ReportStep)
            {
                switch (ele.Action)
                {
                case ElementAction.Click:
                case ElementAction.EnterText:
                case ElementAction.Dropdown:
                case ElementAction.DropdownIndex:
                case ElementAction.RadioBtn:
                case ElementAction.MultiDropdown:
                case ElementAction.Hover:
                    TE.BeginTestCaseStep($"Execute action {ele.Action} on elements: {eleName}",
                                         ele.Random || ele.ProtectedValue ? "Random Value" : ele.Text);
                    break;
                }
            }
            List <ElementResult> elements = null;

            if (ele.EleType == ElementType.AccessabilityId)
            {
                elements = FindAppiumElements(ele, starter?.RawEle);
            }
            else
            {
                var finder = ElementFinder(ele);
                elements = FindElements(ele, finder, starter?.RawEle);
            }
            var rtn = new List <ElementResult>();

            elements.ForEach(x => rtn.Add(CompleteAction(ele, x)));
            return(rtn);
        }
Exemple #3
0
        private ElementResult CompleteAction(ElementObject eleObj, ElementResult eleRes)
        {
            SelectElement select;
            var           ele = eleRes.RawEle;
            var           rsp = new ElementResult(TE)
            {
                Success = false, RawEle = ele, ElementName = eleObj.ElementName
            };

            if (ele == null && !(eleObj.Action == ElementAction.Wait && eleObj.WaitType == Wait.Invisible))
            {
                return(rsp);
            }

            if (eleObj.Random)
            {
                eleObj.Text = TE.Rand.GetRandomString(eleObj.RandomLength);
            }

            try
            {
                if (eleObj.Scroll)
                {
                    try
                    {
                        rsp.ScrollTo(eleObj.ScrollLoc);
                    }
                    catch
                    {
                        try
                        {
                            //scroll till element is within the center 50% of the screen??????????? Just not sure how to drag..
                            //TouchActions action = new TouchActions(Driver);

                            //var visibleText = "Submit";
                            //((AndroidDriver<IWebElement>)Driver).FindElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"" + visibleText + "\").instance(0))");

                            //TouchActions action = new TouchActions(Driver);
                            //action.Scroll(ele, 10, 100).Perform();
                        }
                        catch (Exception e) { Console.WriteLine(e.Message); }
                    }
                    //                    (((IWrapsDriver)ele).WrappedDriver as IJavaScriptExecutor).ScrollToElement(ele);
                }

                switch (eleObj.Action)
                {
                case ElementAction.Click:
                    ele.Click();
                    break;

                case ElementAction.Hover:
                    var act = new Actions(Driver);
                    act.MoveToElement(ele).Build().Perform();
                    TE.Pause(150);    //default pause for hover to take effect
                    break;

                case ElementAction.EnterText:
                    if (eleObj.Clear)
                    {
                        ele.Clear();

                        ele.SendKeys(Keys.Control + "a");
                        ele.SendKeys(Keys.Backspace);
                    }
                    ele.SendKeys(eleObj.Text);
                    break;

                case ElementAction.Dropdown:
                    if (eleObj.Random)
                    {
                        eleObj.Action = ElementAction.DropdownIndex;
                        return(CompleteAction(eleObj, eleRes));
                    }

                    select = new SelectElement(ele);
                    if (select.IsMultiple && eleObj.Clear)
                    {
                        select.DeselectAll();
                    }
                    rsp.Text = SetDropdown(select, eleObj.Text);
                    break;

                case ElementAction.GetDropdown:
                    select = new SelectElement(ele);
                    if (select.IsMultiple)
                    {
                        var tempLst = new List <string>();
                        select.AllSelectedOptions.ToList().ForEach(x => tempLst.Add(x.Text));
                        rsp.Text = string.Join('|', tempLst);
                    }
                    else
                    {
                        rsp.Text = select.SelectedOption.Text;
                    }
                    break;

                case ElementAction.DropdownIndex:
                    select = new SelectElement(ele);
                    if (select.IsMultiple && eleObj.Clear)
                    {
                        select.DeselectAll();
                    }

                    var start = eleObj.Text.Length > 1 ? 1 : 0;

                    var index = TE.Rand.Rnd.Next(start, select.Options.Count);
                    if (int.TryParse(eleObj.Text, out int indexVal) || !eleObj.Random)
                    {
                        index = indexVal;
                    }
                    select.SelectByIndex(index);
                    rsp.Text = select.Options[index].Text;
                    break;

                case ElementAction.MultiDropdown:
                    select = new SelectElement(ele);
                    if (select.IsMultiple && eleObj.Clear)
                    {
                        select.DeselectAll();
                    }

                    foreach (var option in eleObj.Text.Split('|'))
                    {
                        SetDropdown(select, option);
                    }
                    break;

                case ElementAction.RadioBtn:
                    if (!eleObj.Text.ToLower().Equals(ele.Selected.ToString().ToLower()))
                    {
                        ele.Click();
                    }
                    break;

                case ElementAction.GetCheckbox:
                    rsp.Text = ele.Selected.ToString();
                    break;

                case ElementAction.GetText:
                    if (ele.TagName.Equals("select"))
                    {
                        select = new SelectElement(ele);
                        if (select.IsMultiple)
                        {
                            var ops = new List <string>();
                            select.AllSelectedOptions.ToList().ForEach(x => ops.Add(x.Text));
                            rsp.Text = string.Join("\n", ops);
                        }
                        else
                        {
                            rsp.Text = select.SelectedOption.Text;
                        }
                    }
                    else
                    {
                        rsp.Text = ele.Text;
                    }

                    if (string.IsNullOrEmpty(rsp.Text))
                    {
                        rsp.Text = ele.GetAttribute("value");
                    }

                    if (rsp.Text == null)
                    {
                        rsp.Text = "";
                    }

                    break;

                case ElementAction.GetAttribute:
                    rsp.Text = ele.GetAttribute(eleObj.Text);
                    break;

                case ElementAction.GetCSS:
                    rsp.Text = ele.GetCssValue(eleObj.Text);
                    break;

                case ElementAction.GetProperty:
                    rsp.Text = ele.GetProperty(eleObj.Text);
                    break;

                case ElementAction.Wait:
                    //nothing to do here
                    break;

                default:
                    throw new NotImplementedException("This action has not been implemented. Please implement it.");
                }
            }
            catch (Exception e)
            {
                rsp.Exception = e;
                rsp.Success   = false;
                return(rsp);
            }

            rsp.Success = true;
            return(rsp);
        }