java.awt.Robot class is used to generate native system input events for the purposes of test automation, to perform mouse and keyboard actions.As interacting with windows dialog is a limitation in selenium. So, we need to use either AutoIT or Robot class in Selenium Webdriver to interact the Windows Dialog. In this topic we are discussing about the usage of Robot Class.
Test Scenario
1. Access the website url http://www.indeed.co.in.
2. Click on upload resume link.
3. Implement the Logic using the java.awt.robot to provide the absolute location of the file.
4. Capture the full page of the screenshot by providing the dimensions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package org.tq.selenium; import java.awt.AWTException; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class RobotEx { public static void main(String[] args) throws AWTException, InterruptedException, IOException { WebDriver driver = new FirefoxDriver(); driver.get("http://www.indeed.co.in"); driver.findElement(By.className("resume-promo-link")).click(); driver.findElement(By.xpath("//label[contains(text(),'Upload Resume')]")).click(); //Store the location of the file in clipboard //Clipboard StringSelection strSel = new StringSelection("C:\\SeleniumResume.doc"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strSel, null); //Create an object for robot class Robot robot = new Robot(); //Control key in the keyboard //Ctrl+V robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); robot.keyRelease(KeyEvent.VK_CONTROL); Thread.sleep(3000); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); Dimension d1 = new Dimension(1200,1800); BufferedImage bufImage = robot.createScreenCapture(new Rectangle(d1)); File f = new File("indeedScreenshot.jpg"); ImageIO.write(bufImage,"jpg", f); } } |
Thanks for Upload a File in Selenium WebDriver Using Robot Class this article