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


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

Comments

Popular posts from this blog

17 Best Demo Websites for Automation Testing Practice

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

14 Best Selenium Practice Exercises for Automation Practice

Mastering Selenium Practice: Automating Web Tables with Demo Examples

How to Automate Google Search with Selenium WebDriver

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

What Role Graphic Design Services Play in Marketing

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

Java Date Format Validation with Regular Expressions: A Step-by-Step Guide

Mastering User Interactions with the Actions Class in Selenium WebDriver