微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-Selenium视图鼠标/指针

在运行测试时,有什么方法可以真正看到硒鼠标吗?带有Windows光标图像或某种点或十字线或其他任何东西!

我正在尝试在HTML5 Web应用程序中使用与硒和Java一起使用的拖放功能,并且能够看到光标以查看其实际作用将是非常有用的…

解决方法:

最后,我不得不使用Java机器人来完成这项工作.不仅是为了看到鼠标,还因为对于HTML5 Web App来说,硒中的拖放操作是中断的,因为拖放需要两个动作才能进行注册.硒只做一个.

我的方法从每个对象的中心拖动,如果要拖动到要拖动到的元素上方,则可以使用偏移量.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐