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

  // Open URL 

  // Click on Browse button

  // 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
   // Press Ctrl + V key - It will paste the file path in windows dialog  

   // Now release V + Ctrl key

   // Click Enter Key 

   // Release Enter Key

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

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

Passionately working as an Automation Developer from 12+ years. Let's connect on LinkedIn


Popular posts from this blog

Top 7 Web Development Trends in the Market

17 Best Demo Websites for Automation Testing Practice

Mastering Selenium Practice: Automating Web Tables with Demo Examples

14 Best Selenium Practice Exercises for Automation Practice

Top 51 Most Important Selenium WebDriver Interview Questions

Selenium IDE Tutorial: How to Automate Web Testing with Easy-to-Use Interface

Top 10 Highly Paid Indian CEOs in the USA

How to Automate Google Search with Selenium WebDriver

Python Behave Tutorial: A Comprehensive Guide to Behavior-Driven Development (BDD)

Automation Practice: Automate Amazon like E-Commerce Website with Selenium