Simulate Copy Paste action using Java Robot and Clipboard class
|Sometimes we need to simulate keyboard actions without disturbing the present cursor focus.For Example:
Testing with Selenium WebDriver-When we click on any file upload button a file upload windows opens up , where we need to give the path to the file we want to upload.
There are ways in Selenium WebDriver where we try to input the path to the file to the button element, but in some cases it does not work . So we can paste the path in the window and simulate enter key event without disturbing the present focus by the following way -Also If we are using Robot class anywhere to enter String, instead of checking each byte of String one by one and simulate key according to that – we can directly paste the whole String at once. This can be done using Clipboard class — Simulates copy paste action.
Testing with Selenium WebDriver-When we click on any file upload button a file upload windows opens up , where we need to give the path to the file we want to upload.
There are ways in Selenium WebDriver where we try to input the path to the file to the button element, but in some cases it does not work . So we can paste the path in the window and simulate enter key event without disturbing the present focus by the following way -Also If we are using Robot class anywhere to enter String, instead of checking each byte of String one by one and simulate key according to that – we can directly paste the whole String at once. This can be done using Clipboard class — Simulates copy paste action.
//Create instance of Robot class
Robot robot = new Robot();
//Create instance of Clipboard class
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//Set the String to Enter
StringSelection stringSelection = new StringSelection(“String to enter”);
//Copy the String to Clipboard
clipboard.setContents(stringSelection, null);
//Use Robot class instance to simulate CTRL+C and CTRL+V key events :
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
//Simulate Enter key event
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Basically this is quite helpful to handle window events those can not be handled by Selenium WebDriver.
- Bring the auto Sync magic of Protractor to Selenium with Java - October 23, 2015
- Restarting Appium Server could improve various server freezing issues and may improve execution time - January 20, 2015
- Appium with iOS 8 and XCode 6 : What’s new? - November 5, 2014
- REST API automation testing using Apache HttpClient – The Approach - October 3, 2014
- An Overview of mobile application : Moving forward to automation - October 1, 2014
- An introduction to REST - September 29, 2014
- Run ChromeDriver with Chrome Driver Service to reduce script execution time significantly - September 26, 2014
- Selenium WebDriver – Get Cookies from an existing session and add those to a newly instantiated WebDriver browser instance. - September 26, 2014
- Simulate Copy Paste action using Java Robot and Clipboard class - September 26, 2014
- Android : How to test if Android Application has memory leaks - August 11, 2014
-
Ramesh P