问题
I'm trying to use the java.awt.Robot class for testing inside a virtual machine. Everything works as expected if I test it in my host computer, but when I run the same program inside a virtual machine, nothing happens and there is no error/exceptions. I have tried inside virtual machines running Windows XP or Windows 7 on different computers and it never worked.
The code I used for testing on the machines was very simple:
try
{
final Robot robot = new Robot();
robot.mouseMove(500, 500);
}
catch (AWTException e)
{
e.printStackTrace();
}
When I run this, the program just ends and the mouse doesn't move. Is the robot class not working in virtual machines? If it doesn't, is there another way to simulate mouse movement/clicks? Have you ever tried to use this class in a virtual machine and if it worked, what was your setup?
回答1:
IT won't work... similar problem we faced ... when you opened your VM on your laptop/desktop that time your test script will pass.
Try this... Start your test and close the VNC/RDC and then later on check your results it will (fail).
Reason is ... this Robot class is looking for the physical keyboard to be present however when we run the test suite on CI, there we don't have physical keyboard ...
So it wont work.
Alternative solution: Use Selenium methods
回答2:
Maybe it does not work because you have installed inside your machine Guest Additions. I was using VirtualBox and when I uninstalled my Guest Additions program (inside the virtual machine control panel), the problem was solved and my mouse could move just like the tests I did on my host machine. I hope it helps you. It worked for me.
来源:https://stackoverflow.com/questions/22485769/java-awt-robot-not-working-inside-a-virtual-machine