1. What is Robot Class?
- It is a Java class.
- This is used to generate native system input events
- It is used to generate input events like keyboard events, mouse interactions
- For example,
Robot.mouseMove
will actually move the mouse cursor instead of just generating mouse move events
2. What are the basic methods used in Robot class and how to implement them?
Robot robot = new Robot();
- robot.mousePress(KeyEvent.VK_ENTER)
- robot.mouseRelease(KeyEvent.VK_N)
- robot.mouseMove(200,300) To desired screen coordinates
- robot.keyPress(KeyEvent.VK_S)
- robot.keyRelease(KeyEvent_VK_ENTER);
3. How to upload files using Robot class?
driver.findElement(By.id(“upload”)).sendKeys(“/path/file.txt”)
- Using Robot class :
Robot robot = new Robot();
//Store the path of the file to be uploaded using
StringSelection Class Object StringSelection filepath=new StringSelection("D:\\profile.jpg");
//Copy above path to Clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath,null);
//Press Control&V to paste the above path robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
Thread.sleep(1000);
//Release Control & V buttons
robot.keyRelease(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL);
//Press Enter
robot.keyPress(KeyEvent.VK_ENTER);
//Release Enter
robot.keyRelease(KeyEvent.VK_ENTER);
//wait for the file to get uploaded
robot.delay(2000);
No comments:
Post a Comment