Incorporating Robot API with Selenium

Using Robot API with Selenium

Selenium has become the world’s leading test automation tool for testing web based applications and the performance of selenium can be enhanced by incorporating many other tools and add-ons. For example, in some selenium tests, there is a need to use keyboard or mouse to interact with pop-up windows, alerts etc. Selenium webdriver cannot handle this but it can do it if it is incorporated with Robot API with Selenium.

Benefits of Robot API

  • Robot API simulates keyboard or mouse.
  • Robot API will be useful for uploading and downloading of files while using selenium webdriver.
  • It can be easily integrated with the current testing framework.

Documentation of Robot API

You should be aware of basic definition, syntax and the usage of all functions and methods of Robot API and documentation of API will help you in this. It can be seen in the official website of Oracle or you can create the documentation on your machine. A 6 step process will help you to create the documentation of Robot API on your machine.

  1. Copy and extract src.zip file which is available in JDK folder.
  2. Extract the file and navigate till src/java/awt.
  3. After copying the current location of the awt folder, prompt open command.
  4. Change the current directory location in cmd to awt folder and type “ javadoc*.java.” wait for a while and when the process is completed, there will be a few HTML files in the folder.
  5. Open index.html
  6. Here is the full documentation of awt package and click on the Robot link.

Robot API internal methods and usages

Robot API is used to interact with mouse and keyboard events when the browser is automated. Some of the commonly used methods are as follows.

  • keyPress()
  • MousePress()
  • MouseMove()
  • KeyRelease()
  • MouseRelease()

Here is a sample code to automate general use cases.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

class Excercise1 {

     public static void main(String[] args) throws AWTException, InterruptedException {
          WebDriver driver = new FirefoxDriver();
          driver.get(“http://spreadsheetpage.com/index.php/file/C35/P10/”); // sample url
          driver.findElement(By.xpath(“.//a[@href=contains(text(),’yearly-calendar.xls’)]”)).click();
          Robot robot = new Robot();  // Robot class throws AWT Exception
          Thread.sleep(2000); // Thread.sleep throws InterruptedException
          robot.keyPress(KeyEvent.VK_DOWN);  // press arrow down key of keyboard to navigate and select Save radio button
          
          Thread.sleep(2000);  // sleep has only been used to showcase each event separately
          robot.keyPress(KeyEvent.VK_TAB);
          Thread.sleep(2000);
          robot.keyPress(KeyEvent.VK_TAB);
          Thread.sleep(2000);
          robot.keyPress(KeyEvent.VK_TAB);
          Thread.sleep(2000);
          robot.keyPress(KeyEvent.VK_ENTER);
      // press enter key of keyboard to perform above selected action
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>