Selenium查看鼠标/指针 [英] Selenium View Mouse/Pointer

查看:971
本文介绍了Selenium查看鼠标/指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在运行测试时真正看到硒鼠标?无论是使用Windows光标图像还是某种点或十字线或任何东西!

Is there any way to actually see the selenium mouse when it's running the tests? Either with a windows cursor image or some kind of dot or cross hair or anything at all!

我正在尝试使用拖放功能 selenium java HTML5 网络应用中,并且能够看到光标看看它实际上做了什么真的很有用......

I'm trying to get a drag and drop function working with selenium and java in an HTML5 web app, and being able to see the cursor to see what it's actually doing would be really useful...

推荐答案

最后我不得不使用Java机器人让这个工作。不仅要看鼠标,还因为HTML5 Web应用程序的拖放在selenium中被打破,因为需要两次移动来拖放才能注册。 Selenium只做一个。

In the end I had to use the Java robot to get this working. Not only to see the mouse, but also because for an HTML5 Web App dragging and dropping is broken in selenium as two movements are needed to the drag and drop to register. Selenium only does one.

我的方法从每个对象的中心拖动,如果你想拖动你正在拖动的元素,则允许偏移。

My method drags from the centre of each object and allows for an offset if you want to drag past the element you're dragging to.

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);
}

这篇关于Selenium查看鼠标/指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆