示例#1
0
        public override object DoRun()
        {
            var searchStrategy = new LocationHeuristictSearchStrategy(this.Selenium);
            var lookupResult   = searchStrategy.DirectLookup(TagNames, SubjectName, Locale, Direction, Order, false);

            if (Interpreter?.IsAnalysis == true)
            {
                Console.Out.WriteColoredLine(ConsoleColor.Magenta, $"Main Result: \n\t{lookupResult.MainResult}\nAll results:\n\t{string.Join("\n\t", lookupResult.AllValidResults)}");
            }

            var chromeHandle = Selenium.BrowserHandle;
            var browserBox   = new UserBindings.RECT();

            UserBindings.GetWindowRect(chromeHandle, ref browserBox);

            var e = lookupResult.AllValidResults.ElementAt(Order);

            Selenium.PutElementOnScreen(e.WebElement);

            var refreshedPosition = e.WebElement.AsBufferedElement().Rectangle;

            var centerX = refreshedPosition.X + refreshedPosition.Width / 2;
            var centerY = refreshedPosition.Y + refreshedPosition.Height / 2;

            var p = new Point(centerX, centerY);

            Selenium.ConvertFromPageToWindow(ref p);

            if (p.X < 0 || p.X > browserBox.Right || p.Y < 0 || p.Y > browserBox.Bottom)
            {
                throw new NotFoundException("Element not found");
            }
            else
            {
                if (Interpreter?.IsDebugMode == true)
                {
                    BrowserOverlay
                    .HighlightElements((Order + 1).ToString(), (Expect.ToString().ToLower().Equals(true.ToString().ToLower()) ? Color.GreenYellow : Color.Red), lookupResult.MainResult)
                    .ShowFor(750, "Highlighted element will be Clicked");
                }
                return(lookupResult.AllValidResults.ElementAt(Order).WebElement.Selected);
            }

            throw new NotFoundException("Element not found");

            throw new NotImplementedException($"Checking visibility of nth ({Order}) elements is not implemented.");
        }
示例#2
0
        public override object DoRun()
        {
            var query = new LocationHeuristictSearchStrategy(Selenium);

            var result = query.DirectLookup(SearchedTagNames, VisibleTextOfTheButton, NeighbourToLookFrom, Direction, Order, LookForOrthogonalNeighboursOnly, exactMatchOnly: true);

            if (Interpreter?.IsAnalysis == true)
            {
                Console.Out.WriteColoredLine(ConsoleColor.Magenta, _instruction?.ToAnalysisString());
                Console.Out.WriteColoredLine(ConsoleColor.Magenta, $"Main Result: \n\t{result.MainResult}\nAll results:\n\t{string.Join("\n\t", result.AllValidResults)}");
            }

            if (result.Success == false)
            {
                throw new LookupFailureException(result, $"Failed {ToString()}.\nCannot find element {(Order > 0 ? (Order + 1).ToString() : "")}({result.AllValidResults.Count()} results found)");
            }

            switch (Technique)
            {
            case Technique.Show:
                BrowserOverlay
                .HighlightElements((Order + 1).ToString(), Color.GreenYellow, result.MainResult)
                .HighlightElements((Order + 1).ToString(), Color.CadetBlue, result.AllValidResults.Except(new[] { result.AllValidResults.ElementAt(Order) }))
                .ShowUntilNextKeyword("Highlighted element will be Clicked");
                return(new OverlayAnswer(BrowserOverlay.Artifacts, "Highlighting complete."));

            case Technique.Javascript:
                Selenium.WebDriver.Click(result.MainResult.WebElement);
                break;

            case Technique.MouseAndKeyboard:
                Selenium.BringToFront();
                var screenLocation = Selenium.PutElementOnScreen(result.MainResult.WebElement);
                if (Interpreter?.IsAnalysis == true)
                {
                    BrowserOverlay
                    .HighlightElements((Order + 1).ToString(), Color.GreenYellow, result.MainResult)
                    .ShowFor(750, "Highlighted element will be Dragged");
                }
                UserInteropAdapter.PressOnPoint(Selenium.BrowserHandle, screenLocation);


                if (Interpreter.IsAnalysis)
                {
                    var oldPosition = Cursor.Position;

                    var tempPoint = new Point(screenLocation.X, screenLocation.Y);
                    UserBindings.ClientToScreen(Selenium.BrowserHandle, ref tempPoint);
                    Cursor.Position = tempPoint;
                    var steps   = 50m;
                    var xOffset = X / steps;
                    var yOffset = Y / steps;

                    for (int i = 0; i < steps; i++)
                    {
                        Cursor.Position = new Point((int)(tempPoint.X + i * xOffset), (int)(tempPoint.Y + i * yOffset));
                        Thread.Sleep(30);
                    }

                    Cursor.Position = oldPosition;
                }

                screenLocation.X += X;
                screenLocation.Y += Y;
                Thread.Sleep(20);

                UserInteropAdapter.ReleaseOnPoint(Selenium.BrowserHandle, screenLocation);
                Thread.Sleep(50);
                break;
            }

            return(new SuccessAnswer($"Performed {ToString()}"));
        }