Selenium查看鼠标/指针

时间:2013-09-05 10:36:28

标签: java html5 testing selenium

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

我正在尝试使用拖放功能在selenium网络应用中使用javaHTML5,并且能够看到光标以查看它实际在做什么会非常有用......

2 个答案:

答案 0 :(得分:3)

最后,我不得不使用Java机器人来实现这一目标。不仅要看鼠标,还要因为HTML5 Web应用程序的拖放在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);
}

答案 1 :(得分:1)

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

“mouseDown”,“mouseMoveAt”和“mouseUp”命令也是非常好的选择。

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

相关问题