Selenium WebDriver - Upload a File using Robot Class

Selenium can't interact with Window diaologs 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 thankfully we can handle it by integrating Selenium with some other tools. One such tool is Robot Class.

What is Robot Class?
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.

Use Cases of Robot Class in Selenium Automation:

  • Handle Upload File Windows dialog and select the file to be uploaded
  • Handle Download File Window Dialog and click on Save or Cancel file button


Scenario 1 - Handle Upload File Windows 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 Roboto class.

Practical 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();}
 }


Join Telegram channel at https://t.me/techlistic
Feel free to ask queries or share your thoughts in comments or email me.

Comments

Post a Comment

Popular Posts

Top 10 Demo Websites to Practice Selenium Webdriver Online

Top 25 Must to know Selenium Webdriver Commands List

Selenium WebDriver - Take Screenshot and Partial Screenshot Commands

Selenium WebDriver - Browser and Navigation Commands

Selenium Essentials - How to become expert in Automation Testing?