What is Robot Class in Selenium Webdriver?

Selenium can't interact with Window dialogs on it's own.
You all must know that Selenium can only handle browser windows and not OS windows. So, when we have to upload any file on a webpage and as soon as we click on the Browse or Upload button, a windows tab opens up from which we have to select the file. We cannot handle that windows tab directly with Selenium Webdriver.
But we can use other libraries and integrate them with Selenium code to handle window based dialogs. Robot class is one of them. We can use Robot class to upload or download a file using selenium.




What is Robot Class in Selenium Webdriver?

Robot Class is used to perform Keyboard actions. It has direct support to perform actions like press and release key. Robot class has key codes for each key present on the keyboard. So, if we want to press any key, then we need to call that key code.

In automation we make use of Robot class to handle Windows tabs by interacting with them via virtual keyboard. One important thing to keep in mind that, in robot class keyPress and keyRelease functions come in a combo. So, if you are pressing a key then you have to call release key function after that.

How can we use Robot class in Selenium to handle window dialogs?
  • How to Upload a file in Selenium Webdriver
  • How to Download a file in Selenium Webdriver

Scenario 1 - Handle Upload File Window dialog and select the file to be uploaded

Test Steps:
  1. Go to File Upload Page.
  2. Click on the Upload file button.
  3. Select the file or copy the path of the file. (Windows dialog)
  4. And click on 'Ok' button. (Windows dialog)
From above steps we can execute first two steps using selenium and will perform 3rd and 4th step using Robot class.

Code to upload image using Robot Class:

package com.techlistic.selenium;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FileUpload {

 public static void main(String[] args) {

  // Launch Browser
  WebDriver driver = new FirefoxDriver();

  // Maximize Window
  driver.manage().window().maximize();

  // Open URL 
  driver.get("https://www.techlistic.com/p/selenium-practice-form.html");

  // Click on Browse button
  driver.findElement(By.className("browse")).click();

  // Wait for 3 seconds
  try { Thread.sleep(3000);}
  catch (InterruptedException e){ e.printStackTrace();}

  // Get Location of the file to be uploaded 
  String fileLocation = System.getProperty("user.dir") + "\\test data\\" + "sample-upload-file.docx";
  StringSelection filepath = new StringSelection(fileLocation);

  // Copy the file path
  Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filepath, null);

  // Try block
  try {
   // Create object of Robot class
   Robot robot = new Robot();

   // Press Ctrl key
   robot.keyPress(KeyEvent.VK_CONTROL);
   // Press Ctrl + V key - It will paste the file path in windows dialog  
   robot.keyPress(KeyEvent.VK_V);

   // Now release V + Ctrl key
   robot.keyRelease(KeyEvent.VK_V);
   robot.keyRelease(KeyEvent.VK_CONTROL);

   // Click Enter Key 
   robot.keyPress(KeyEvent.VK_ENTER);

   // Release Enter Key
   robot.keyRelease(KeyEvent.VK_ENTER);

  } catch (AWTException e) {e.printStackTrace();}
 }

Find Broken Links with Selenium  << Previous      ||      Next >>  Hanlde Dynamic Web Table in Selenium

Refer Selenium Webdriver Tutorials Series

Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Selenium Automation Testing Practice

Automate an e-Commerce website with Selenium Webdriver

How to Handle Dynamic Web Table in Selenium WebDriver

Top 50 Selenium Webdriver Interview Questions

Automate Browser Actions and Menu links with Selenium Webdriver

Top 25 Selenium Webdriver Commands for Test Automation