【问题标题】:Selenium View Mouse/PointerSelenium View 鼠标/指针
【发布时间】:2013-09-09 03:41:03
【问题描述】:

有没有办法在运行测试时实际看到 selenium 鼠标?可以使用 Windows 光标图像或某种点或十字线或任何东西!

我正在尝试在HTML5 网络应用程序中使用seleniumjava 的拖放功能,并且能够看到光标以查看它实际在做什么将非常有用.. .

【问题讨论】:

  • 我发现了它,但它似乎不起作用 - 我不确定它是否与最新版本的 selenium 兼容,或者我是否必须手动启用它或其他什么..
  • 另外我认为它是用于从浏览器本身运行测试 - 我是从 netbeans 运行它们
  • 嗯.. 如果您找到合适的答案,请更新您的帖子。 :)
  • @user2339071 我添加了一个为我修复拖放并允许看到鼠标的答案

标签: java html testing selenium


【解决方案1】:

您可以使用 Selenium 的“dragAndDrop”和“dragAndDropToObject”命令来拖放元素。

“mouseDown”、“mouseMoveAt”和“mouseUp”命令也是很好的替代品。

Here is 在 selenium IDE 中两种方式的非常好的示例。您可以将该代码转换为 java 使用。

【讨论】:

【解决方案2】:

最后我不得不使用 Java 机器人来完成这项工作。不仅要看到鼠标,还因为 HTML5 Web App 的拖放在 selenium 中被破坏,因为拖放需要两个动作才能注册。 Selenium 只做一个。

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

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

【讨论】:

猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 2011-08-24
  • 2010-09-19
  • 1970-01-01
  • 2012-06-05
  • 2010-10-15
  • 1970-01-01
  • 2017-07-03
相关资源
最近更新 更多