Basic Selenium

What is Robot Class in Selenium Webdriver and Usage

Robot Class in Selenium

Hello, Welcome to Selenium Tutorials, In this post, we will see how to use Robot class in Selenium Webdriver. I have used Robot class a couple of times in my test script and it worked well.

I uploaded one article on upload file using robot class How to upload files in Selenium using Robot class.

I have also used the Robot class for capturing screenshot as well which captures the complete screenshot of the window.

 

 

Robot Class in a separate class which is part of Java not a part of Selenium, Robot class is mainly created for automating Java Platform implementation.

Robot Class in Selenium

This class has many inbuild method which helps us to perform our task.

Methods of Robot Class

 

Now let’s discuss Robot class in Webdriver.

Robot class is a separate class in Java which will allow us to perform multiple tasks based on our requirement. It generally will throw AWT exception so we need to deal with it accordingly.

Using Robot class, we can simulate keyboard event in Selenium.

To use keyboard events you have to use to a method of Robot class.

Methods of Robot Class

keyPress()

keyRelease()

We also have to use KeyEvent which is a separate class which is responsible for keyStroke

Each key has to be press and release respectively

KeyEvent in Robot Class

 

Let’s discuss the Scenario Which covers enter key 

1-Open Facebook.

2- Enter Username and password.

3- Using the robot class press Enter button.

Program for  Robot Class in Selenium Webdriver

package Robot_Class;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

@SuppressWarnings("unused")
public class TestLogin {
 
 @Test
 public void TestRobo()throws Exception
 {
  // Open Firefox
  WebDriver driver=new FirefoxDriver();
                
  // Maximize the window
  driver.manage().window().maximize();

  // Open facebook
  driver.get("http://www.facebook.com");

  // Enter Username
  driver.findElement(By.id("email")).sendKeys("Selenium@gmail.com");

  // Enter password
  driver.findElement(By.id("pass")).sendKeys("mukesh");

  // Create object of Robot class
  Robot r=new Robot();

   // Press Enter
   r.keyPress(KeyEvent.VK_ENTER);

   // Release Enter
   r.keyRelease(KeyEvent.VK_ENTER);
                  
}

}

 

Limitation of Robot class

  1. Robot class works on the current window so if you have multiple window present or multiple browsers running your test then it may not behave as expected.
  2. If you are using X, Y coordinates for your test then the test will behave differently on different screens (resolution)
  3. If you are running your test in VM (virtual machine) then as well script failure is more.

 

If you like this post and if you feel its informative then please comment below and share with others as well.

Keep visiting, have a nice day :)

 

For More updates Learn Automation page

For any query join Selenium group- Selenium Group 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

58 thoughts on “What is Robot Class in Selenium Webdriver and Usage

  1. stqatools says:

    Really great blog related to robot class in selenium

  2. stqatools says:

    Thanks, mukesh for this great robot class in selenium in java please provide same blog using python also thanks.

    1. Hi there,

      I’ll post for python soon…:)

  3. Swamy says:

    Hi Mukesh,

    I have a situation when I click a add button , it generates the Modal dialog with error message and I need acknowledge OK button. its working as expected in manual testing. But in automation I am not able to catch the same window. I am using Selenium and Java.

    -Regards
    Swamy

    1. Hi Swamy, if that is in the same window then directly inspect and perform the operation.

      Note- Please check frame as well

  4. Pachiyappan says:

    How to upload multiple files at once using robot class.

    1. Hi Pachiyappan,

      Using robot class is kind of work around, I mean not strongly proven way. I would recommend you to use AutoIT for this purpose. Please check this link https://vistasadprojects.com/mukeshotwani-blogs-v2/upload-multiple-files-in-selenium-webdriver/

  5. W3Softech says:

    Hello Mukesh,
    Thanks for the suggestions that you are contributed here and would like to read this blog regularly to get more updates regarding Selenium.

    1. Hi Swetha,

      You are always welcome…:)

  6. Satwik says:

    What is VK_ENTER over here?

    1. Hi Satwik,

      Its Virtual Key Enter

      1. Bala says:

        As per Java Standard we can say VK_ENTER is Enum Constant also.
        In which java allows to access constants with Class Name directly.

        1. Hi Bala,

          I haven’t gone through this in detail. Please check this link https://docs.oracle.com/javase/8/docs/api/java/awt/event/KeyEvent.html

  7. Seema says:

    nice content..easily understandable.
    pls post some postman tutorials for API testing & API Automation

    1. Hi Seema,

      Thanks for your comments…:)
      Requested contents are already in pipeline.

  8. Prathamesh Patil says:

    Hi Mukesh,
    Thanks for very well descripted selenium concepts.
    Let me ask you, According to you which one should use mostly AutoIt or Robot class?

    Thank you

    1. Hi Prathamesh,

      First of all thanks for your appreciation…:)
      Now lets move to your doubts, I would recommend you to prefer AutoIt because it locates windows property very well while Robot is mostly used for keyboard event simulation which runs blindly(wherever mouse control lies on screen)

  9. Maruti says:

    good contents post more like this.

  10. Ankit Kulhade says:

    It is really helpful. Thnaks

    1. Hi Ankit,

      Thanks…:)

  11. Rahul Gaike says:

    Hi Mukesh!

    Thanks a lot for your site and knowledge sharing!!

    1. Hi Rahul,

      Thanks for your overwhelming comments…:)

  12. Sidhartha Sankar Mohanty says:

    Hey,
    Actually i need to know the major difference between Robot Class and Action Class? when to use what ?
    just let me know…..it will be helpful….

    1. Hi Sidhartha,

      Actions class comes from Selenium APIs which provides some advanced events like mouse hovering, drag-drop, double-click etc. while robot class comes from Java APIs purely to simulate keyboard and mouse events and other desktop screen related events. Actions class works only on Web UI while robot class works anywhere.

  13. Lakshmi says:

    Hi Mukesh,
    Thanks for making such a wonderful , informative videos.I am a big fan of your videos.All your videos are too good , simple and easy to understand.It is very much helpful.I always look for your videos Please keep posting the videos.Thanks a lot for the amazing work.

    1. Hi Lakshmi,

      Thanks for your valuable appreciation. I’ll always try my level best to serve my subscribers…:)

      1. Rama says:

        Hi Mukesh,
        how do you identify the checkbox elements (TicketLess travel and Same as Billing address) on this page? They both have the same attributes.
        Thanks.
        http://newtours.demoaut.com/mercurypurchase.php

        1. Hi Rama,

          Clicking on the checkbox is same like you click on any button or element.

  14. sandeep says:

    Hi mukesh,
    Your blogs are really good and helpful. Keep posting more and more content 🙂
    can you pls upload a video for the current topic : What is Robot Class in Selenium Webdriver and Usage

    1. Hi Sandeep,

      I’ll will do it soon…:)

  15. Harshal says:

    Hi sir,
    which is the best class among three(AutoIT,
    Sikulli,Robot) for uploading a file.
    and what is the drawback of Robot class.

    1. Hi Harshal,

      Autoit is best option among all these on windows platform. And drawback of robot class is that it simulates only keyboard events which depends on control focus.

  16. Mahesh chavan says:

    Hello Sir,
    I want to use selenium code inside webapplication.
    Like on webpage on button click open new url and perform click action there. Is it possible?

  17. Shriya says:

    hi Mukesh,
    For uploading a file in selenium webdriver, which is best – Robot class or autoit ?

    1. Hi Shriya,

      AutoIT is best in windows environment else robot class.

  18. Shailendra singh panwar says:

    Hello mukesh sir ,
    Is too helpfull wbsite for learn automation thanks for share your knowledge
    Thanks alot

    1. Hi Shailendra,

      Thanks for your comments.

  19. Learner says:

    Am I supposed to install any special jar or any other files for this method to work?

    1. No James Robot class is part of Java AWT

  20. prajeeth says:

    Hi,
    Suppose i want to click on any key in keyboard and on any element through mouse..
    Can we work on above scenario using Robot ???

    1. Yes we can do that.

  21. Girish says:

    Nice Blog 🙂

  22. celine says:

    Is the first line,
    necessary for this code to work?

    1. no it will work without span too

  23. Reshma Sultana says:

    Hi Mukesh,

    In above program you mentioned “@SuppressWarnings(“unused”)”. Could you please explain me what does that mean?

    1. Hi Reshma,

      This is optional using above code you wont find any warnings in ur code.

      1. Reshma Sultana says:

        ok.Thank you.

  24. Aileen says:

    Hello….
    In the above example , It is written that “KeyEvent.VK_ENTER”. I want to ask that what is VK_ENTER here? Can you please make me understand?

    1. Hi Aileen,

      Here VK stands for virtual keys.

  25. Manas says:

    Nice one..Thanks

    1. Hi Manas,

      Thanks 🙂

  26. ManiRaj says:

    Hi Mukesh,

    How the keyevent will work without specifying the locator of the webelement. How it know that it should click on the login button but not on the other button.

    Thanks,
    Maniraj

    1. Hi Mani,

      It will hit enter and by default it will hit login button. If you have multiple buttons then it won’t work.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.