Пример #1
0
        private static void DrawMarker(int absoluteX, int absoluteY)
        {
            string script =
                @"(function showRectangle(x, y) {
                var rectangle = document.createElement('div');
                rectangle.style.background = 'red';
                rectangle.style.position = 'absolute';
                rectangle.style.borderRadius = '50%';
                
                rectangle.style.left = (x - 5) + 'px';
                rectangle.style.top  = (y - 5) + 'px';

                rectangle.style.width  = '10px';
                rectangle.style.height = '10px';

                document.body.appendChild(rectangle);
                setTimeout(function cbRemoveRectangle() {
                    if (rectangle) {
                        rectangle.remove();
                    }
                }, 2000);
            })(arguments[0], arguments[1]);";

            SwdBrowser.ExecuteJavaScript(script, absoluteX, absoluteY);
        }
        internal void RunScript()
        {
            string scriptFromEditor = view.GetJavaScriptCodeFromEditor();

            Exception outException;
            bool      isOk   = false;
            object    result = null;

            isOk = UIActions.PerformSlowOperation(
                "Operation: RunScript() / Executing JavaScript Snippet",
                () =>
            {
                result = SwdBrowser.ExecuteJavaScript(scriptFromEditor);
            },
                out outException,
                null,
                TimeSpan.FromMinutes(1)
                );

            if (!isOk)
            {
                MyLog.Error("RunScript() Failed to execute JavaScript snippet");
                MyLog.Exception(outException);
                if (outException != null)
                {
                    throw outException;
                }
            }

            string consoleOut = DumpObject(result);

            view.AppendConsole(consoleOut);
        }
        public void CloseDriver_should_close_the_opened_browser_instance()
        {
            WebDriverOptions options = new WebDriverOptions()
            {
                BrowserName = WebDriverOptions.browser_Firefox,
                IsRemote    = false,
            };

            string specialTitle = "SwdBrowser.CloseDriver TEST TEST";

            string[] specialWindows = new string[] { };

            specialWindows = GetDesktopWindowsWithSpecialTitle(specialTitle);
            specialWindows.Length.Should().Be(0, "Expected no windows with title <{0}> at the beginning", specialTitle);

            SwdBrowser.Initialize(options);

            string changeTitleScript = string.Format("document.title = '{0}'", specialTitle);

            SwdBrowser.ExecuteJavaScript(changeTitleScript);

            specialWindows = GetDesktopWindowsWithSpecialTitle(specialTitle);
            specialWindows.Length.Should().Be(1, "Expected only 1 window with title <{0}> after new driver was created", specialTitle);

            SwdBrowser.CloseDriver();

            specialWindows = GetDesktopWindowsWithSpecialTitle(specialTitle);
            specialWindows.Length.Should().Be(0, "Expected no windows with title <{0}> after the driver was closed", specialTitle);
        }
        private void imgBox_Click(object sender, EventArgs args)
        {
            if (!ModifierKeys.HasFlag(Keys.Control))
            {
                return;
            }

            MouseEventArgs mouse = args as MouseEventArgs;



            int absoluteX = mouse.X; // + ;
            int absoluteY = mouse.Y; // + ;


            absoluteX = (Convert.ToInt32(absoluteX / imgBox.ZoomFactor) + Convert.ToInt32(imgBox.HorizontalScroll.Value / imgBox.ZoomFactor));
            absoluteY = Convert.ToInt32(absoluteY / imgBox.ZoomFactor) + Convert.ToInt32(imgBox.VerticalScroll.Value / imgBox.ZoomFactor);


            string script =
                @"(function showRectangle(x, y) {
                var rectangle = document.createElement('div');
                rectangle.style.border = '3px solid red';
                rectangle.style.position = 'absolute';
                
                rectangle.style.left = x + 'px';
                rectangle.style.top  = y + 'px';

                rectangle.style.width  = '100px';
                rectangle.style.height = '20px';

                document.body.appendChild(rectangle);
                setTimeout(function cbRemoveRectangle() {
                    doNastyStuff();
                }, 2000);
            })(arguments[0], arguments[1]);";

            SwdBrowser.ExecuteJavaScript(script, absoluteX, absoluteY);
        }
Пример #5
0
        void UserCommand_OnMouseClick(UserCommands.MouseClickCommand evt)
        {
            MouseEventArgs mouse         = evt.MouseEvent;
            var            imageViewPort = imgBox.GetImageViewPort();

            int absoluteX = mouse.X - imageViewPort.Left;
            int absoluteY = mouse.Y - imageViewPort.Top;


            absoluteX = (Convert.ToInt32(absoluteX / imgBox.ZoomFactor) + Convert.ToInt32(imgBox.HorizontalScroll.Value / imgBox.ZoomFactor));
            absoluteY = Convert.ToInt32(absoluteY / imgBox.ZoomFactor) + Convert.ToInt32(imgBox.VerticalScroll.Value / imgBox.ZoomFactor);


            txtLog.DoInvokeAction(() =>
            {
                txtLog.Text += "\r\n" + "X " + absoluteX.ToString() + " Y " + absoluteY.ToString();
            });



            if (!ModifierKeys.HasFlag(WINKeys.Control))
            {
            }
            ;



            var clickCommand =
                @"return (function SWD_GET_ELEMENT() {
                  var absoluteX = {## absoluteX ##};
                  var absoluteY = {## absoluteY ##};
                  var view = {
                      Left: window.scrollX,
                      Top: window.scrollY,        
                      Right: window.scrollX + window.innerWidth,
                      Bottom: window.scrollY + window.innerHeight
                  };
              
                  var elementInsideViewPort = (view.Left <= absoluteX && absoluteX <= view.Right) &&
                                              (view.Top <= absoluteY && absoluteY <= view.Bottom);
                  if (!elementInsideViewPort) {
                    window.scroll(Math.max(absoluteX - 30, 0), Math.max(absoluteY - 30, 0));
                  }
                  absoluteX = absoluteX - window.scrollX;
                  absoluteY = absoluteY - window.scrollY;
                  return document.elementFromPoint(absoluteX, absoluteY);
              })()"
                .Replace("{## absoluteX ##}", absoluteX.ToString())
                .Replace("{## absoluteY ##}", absoluteY.ToString());

            IWebElement element = (IWebElement)SwdBrowser.ExecuteJavaScript(clickCommand);

            // element.Click();

            if (element == null)
            {
                MyLog.Error("Element is null after performing command " + clickCommand ?? "<null>");
                return;
            }
            if (element.TagName == "iframe")
            {
                try
                {
                    var frameLocation = element.Location;
                    absoluteX = absoluteX - frameLocation.X;
                    absoluteY = absoluteY - frameLocation.Y;

                    MyLog.Debug("Switching to IFrame");
                    SwdBrowser.GetDriver().SwitchTo().Frame(element);

                    clickCommand = String.Format(@"return document.elementFromPoint({0}, {1});", absoluteX, absoluteY);
                    element      = (IWebElement)SwdBrowser.ExecuteJavaScript(clickCommand);
                    ClickOnElement(absoluteX, absoluteY, element);
                }
                finally
                {
                    SwdBrowser.SwitchToDefaultContent();
                }
            }
            else
            {
                ClickOnElement(absoluteX, absoluteY, element);
            }
        }