1

I'm having a test case to be automated, where when the user moves the mouse outside the web page, there should be a popup appearing.

I was planing to move the mouse pointer to the yellow highlighted area.

Is there any way to move the mouse pointer to outside the actual web page.

I'm using selenium Java. enter image description here

1
  • what happen when you use action class movebyoffset and keep the body as the reference element ?
    – PDHide
    Commented Nov 1, 2021 at 10:33

2 Answers 2

1

It's not possible to find an element outside of the web page using Selenium.

To simulate the mouse leaving the web page, you can use JavaScript to trigger a mouseleave event on the body element of the web page. Here's some sample code to do that:

var event = new MouseEvent('mouseleave', {
view: window,
bubbles: true,
cancelable: true
});

This code creates a new MouseEvent object with the mouseleave event type, and dispatches it on the document.body element. This simulates the mouse leaving the web page and should trigger the desired popup to appear.

Note that you can execute this code using the executeScript method of the JavascriptExecutor interface in Selenium, like this:

Thread.sleep(5000);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("var event = new MouseEvent('mouseleave', {view: window, bubbles: true, cancelable: true}); document.body.dispatchEvent(event);");

I hope this helps! Let me know if you have any further questions.

0

It appears the test case in this situation is to "show a pop-up modal to the user if the app sees the mouse leaving the document window".

Selenium is only able to use the mouse while inside the document window (DOM) of the browser. It is not able to move the mouse outside the document window to interact with the browser address bar, back/forward/refresh buttons, etc. That's why there are built-in commands to do those tasks for you.

From the Selenium docs:

Mouse represents a mouse event. Mouse actions are performed by using low-level interface which allows us to provide virtualized device input action to the web browser.

In other words, it looks like a mouse cursor, but it's a virtual mouse cursor. What you're wanting to do is outside the scope of Selenium.

Keep in mind that this test case likely falls under the principle of "you can't automate everything, nor should you automate everything."

1
  • Thanks, BTW I was able to do that by resizing( Dimension smallerWindow = new Dimension(100,100); ) the browser window and then again maximizing the browser window.
    – ChathuD
    Commented Nov 2, 2021 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.