Robot Class in Selenium:
- Using Robot Class we can handle keyboard and mouse events in Selenium.
- Robot Class can help in upload / download files using selenium web driver.
- Methods in Robot Class can be effectively used to do the interaction with popups in Web Applications.
- Robot Class can easily integrated with current automation framework (keyword, data-driven or hybrid).
Robot Class Handle Below Options:
- Press Escape key from keyboard using Different Robot class Methods.
- Handle Mouse Buttons Using Robot class.
- Scroll Mouse Using Robot class.
- Screen Capture Using Robot class.
Package:
- Using below packages we handle Robot Class Methods.
1 2 3 4 5 | import java.awt.Robot; import java.awt.event.KeyEvent; import java.awt.AWTException; |
Creating Object of robot class in Selenium:
- Create Instance / Object of Robot() Class to Use Methods.
1 | Robot robot=new Robot(); |
Methods under Robot Class are:
- .keyPress();
- .mousePress();
- .mouseMove();
- .keyRelease();
- .mouseRelease();
Press Escape key on keyboard using Different Robot class Methods:
- .keyPress()
- Using keyPress() Method we Press any key from KeyBoard.
1 2 3 | // This will press Escape key on keyboard. robot.keyPress(KeyEvent.VK_ESC); |
- .keyRelease()
- Using keyRelease() Method we Release the Pressed any key from KeyBoard.
1 2 3 | // This will release the CAPS_LOCK key. robot.keyRelease(KeyEvent.VK_CAPS_LOCK); |
- .mousePress()
- Using mousePress() Method we press Left mouse button from Mouse.
1 2 3 | // This will press Left mouse button. robot.mousePress(InputEvent.BUTTON1_MASK); |
- .mouseRelease()
- Using mouseRelease() Method we Release pressed Left mouse button from Mouse.
1 2 3 | // This will release Left mouse button. robot.mouseRelease(InputEvent.BUTTON1_MASK); |
- .mouseMove()
- Using mouseMove() Method use to Move cursor pointer to X and Y co-ordinates.
1 2 3 | //This will move the mouse pointer to X and Y co-ordinates. robot.mouseMove(coordinates.getX(), coordinates.getY()); |
Note: keyPress will send an event that a key has been pressed down. keyRelease will send the event that the key has been released .
Steps to Press Escape key on keyboard Robot class:
- Create object of Robot class.
- Using keyPress method press Escape key from KeyBoard.
- Using keyRelease method release the CAPS_LOCK key.
- Using mousePress Method press Left mouse button.
- Using mouseRelease Method release the Left mouse button.
- Using getLocation() method Get Location and using mouseMove(coordinates.getX(), coordinates.getY() method move mouse on X,Y Co-Ordinate.
Example: Press Escape key on keyboard using Different Robot class Methods:
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 | import java.awt.Robot; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import com.gargoylesoftware.htmlunit.javascript.host.geo.Coordinates; public class Robot_Class { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Robot class Robot robot = new Robot(); // This will press Escape key on keyboard. robot.keyPress(KeyEvent.VK_ESCAPE); // This will release the CAPS_LOCK key. robot.keyRelease(KeyEvent.VK_CAPS_LOCK); // This will press Left mouse button. robot.mousePress(InputEvent.BUTTON1_MASK); // This will release Left mouse button. robot.mouseRelease(InputEvent.BUTTON1_MASK); Point coordinates = driver.findElement(By.id("img_location")).getLocation(); robot.mouseMove(coordinates.getX(), coordinates.getY()); } } |
Example: Handle Mouse Buttons Using Robot class:
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 | import java.awt.Robot; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import org.openqa.selenium.By; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import com.gargoylesoftware.htmlunit.javascript.host.geo.Coordinates; public class Robot_Mouse_Class { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Robot class Robot robot = new Robot(); // Press Left button robot.mousePress(InputEvent.BUTTON1_MASK); // Release Left button robot.mouseRelease(InputEvent.BUTTON1_MASK); // Press Middle button robot.mousePress(InputEvent.BUTTON2_MASK); // Release Middle button robot.mouseRelease(InputEvent.BUTTON2_MASK); // Press Right button robot.mousePress(InputEvent.BUTTON3_MASK); // Release Right button robot.mouseRelease(InputEvent.BUTTON3_MASK); } } |
Example: Scroll Mouse Using Robot class:
- Scroll Mouse using mouseWheel() Method.
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 | import java.awt.Robot; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Robot_Mouse_Class { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Robot class Robot robot = new Robot(); // Scroll MouseWheel robot.mouseWheel(5); } } |
Example: Screen Capture Using Robot class:
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 | import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class Robot_Mouse_Class { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.stqatools.com"); // Create object of Robot class Robot robot = new Robot(); // Get Screen Size java.awt.Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); // Capture ScreenShot BufferedImage img = robot.createScreenCapture(new Rectangle(size)); // Store image into file File path = new File("D://profile.jpg"); // Write image path ImageIO.write(img, "JPG", path); } } |
Robot Class in Selenium Webdriver and What is Robot Class in Selenium Webdriver and Upload file using Robot class and File upload using Robot class