Java Selenium视图鼠标/指针
当selenium鼠标运行测试时,有没有办法看到它?要么用windows光标图像,要么用点或十字线或其他任何东西 我正在尝试在Java Selenium视图鼠标/指针,java,html,testing,selenium,Java,Html,Testing,Selenium,当selenium鼠标运行测试时,有没有办法看到它?要么用windows光标图像,要么用点或十字线或其他任何东西 我正在尝试在HTML5web应用程序中使用selenium和java实现拖放功能,并且能够看到光标以查看其实际执行的操作将非常有用…您可以使用selenium的“dragAndDrop”和“dragAndDropToObject”命令来拖放元素 “mouseDown”、“mouseMoveAt”和“mouseUp”命令也是很好的选择 在selenium IDE中,这两种方法都是很好的
HTML5
web应用程序中使用selenium
和java
实现拖放功能,并且能够看到光标以查看其实际执行的操作将非常有用…您可以使用selenium的“dragAndDrop”和“dragAndDropToObject”命令来拖放元素
“mouseDown”、“mouseMoveAt”和“mouseUp”命令也是很好的选择
在selenium IDE中,这两种方法都是很好的例子。您可以将代码转换为java以供使用。最后,我不得不使用java robot来实现这一点。不仅是为了看到鼠标,还因为对于HTML5 Web应用程序,拖放在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);
}
尝试此链接:我发现它似乎不起作用-我不确定它是否与最新版本的selenium兼容,或者是否必须手动启用它或其他功能..此外,我认为它是用于从浏览器本身运行测试-我正在从NetBeansHMM运行它们。。如果您找到合适的答案,请更新您的帖子。:)@user2339071我添加了一个答案,为我修复了拖放功能,并允许看到鼠标。事实上,众所周知,selenium中的HTML5拖放功能不起作用。我已经对问题进行了分类,并将用答案更新我的问题。我仅使用robot进行此拖放,因为它在selenium中不起作用(请参阅bug)