Selenium View Mouse / Pointer

Is there a way to see a selenium mouse when it runs tests? Either with the image of the Windows cursor, or with some point or crosshair, or anything at all!

I am trying to get a drag and drop function that works with seleniumand javain a web application HTML5, and the ability to see the cursor to see what it actually does will be really useful ..

+3
source share
2 answers

In the end, I had to use a Java robot to make it work. Not only in order to see the mouse, but also because for the HTML5 web application, the drag and drop is split into selenium, because for the drag and drop, two movements are required for registration. Selenium does only one.

My method is dragging from the center of each object and allows offset if you want to drag the item you are dragging.

public void dragAndDropElement(WebElement dragFrom, WebElement dragTo, int xOffset) throws Exception {
    //Setup robot
    Robot robot = new Robot();
    robot.setAutoDelay(50);

    //Fullscreen page so selenium coordinates are same as robot coordinates
    robot.keyPress(KeyEvent.VK_F11);
    Thread.sleep(2000);

    //Get size of elements
    Dimension fromSize = dragFrom.getSize();
    Dimension toSize = dragTo.getSize();

    //Get centre distance
    int xCentreFrom = fromSize.width / 2;
    int yCentreFrom = fromSize.height / 2;
    int xCentreTo = toSize.width / 2;
    int yCentreTo = toSize.height / 2;

    //Get x and y of WebElement to drag to
    Point toLocation = dragTo.getLocation();
    Point fromLocation = dragFrom.getLocation();

    //Make Mouse coordinate centre of element and account for offset
    toLocation.x += xOffset + xCentreTo;
    toLocation.y += yCentreTo;
    fromLocation.x += xCentreFrom;
    fromLocation.y += yCentreFrom;

    //Move mouse to drag from location
    robot.mouseMove(fromLocation.x, fromLocation.y);

    //Click and drag
    robot.mousePress(InputEvent.BUTTON1_MASK);

    //Drag events require more than one movement to register
    //Just appearing at destination doesn't work so move halfway first
    robot.mouseMove(((toLocation.x - fromLocation.x) / 2) + fromLocation.x, ((toLocation.y - fromLocation.y) / 2) + fromLocation.y);

    //Move to final position
    robot.mouseMove(toLocation.x, toLocation.y);

    //Drop
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
+3
source

You can use the "dragAndDrop" and "dragAndDropToObject" Selenium commands to drag an item.

The mouseDown, mouseMoveAt, and mouseUp commands are also very good alternatives.

Selenium IDE. java .

+1

Source: https://habr.com/ru/post/1685713/


All Articles