• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / What is Robot Class in Selenium Webdriver and Usage

What is Robot Class in Selenium Webdriver and Usage

January 9, 2019 by Mukesh Otwani 58 Comments

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 

Filed Under: Basic Selenium Tagged With: Robot class

Reader Interactions

Comments

  1. stqatools says

    September 26, 2019 at 10:56 PM

    Really great blog related to robot class in selenium

    Reply
    • Mukesh Otwani says

      September 27, 2019 at 9:17 AM

      Thanks…:)

      Reply
  2. stqatools says

    September 9, 2019 at 11:54 PM

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

    Reply
    • Mukesh Otwani says

      September 12, 2019 at 10:31 AM

      Hi there,

      I’ll post for python soon…:)

      Reply
  3. Swamy says

    April 20, 2019 at 1:37 AM

    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

    Reply
    • Mukesh Otwani says

      April 23, 2019 at 12:33 AM

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

      Note- Please check frame as well

      Reply
  4. pachiyappan says

    April 10, 2019 at 6:07 AM

    Thanks Mukesh.

    Reply
  5. Pachiyappan says

    March 9, 2019 at 9:07 PM

    How to upload multiple files at once using robot class.

    Reply
    • Mukesh Otwani says

      March 11, 2019 at 9:22 PM

      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 http://learn-automation.com/upload-multiple-files-in-selenium-webdriver/

      Reply
  6. W3Softech says

    February 7, 2019 at 3:17 PM

    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.

    Reply
    • Mukesh Otwani says

      February 8, 2019 at 10:40 AM

      Hi Swetha,

      You are always welcome…:)

      Reply
  7. Satwik says

    February 5, 2019 at 7:31 PM

    What is VK_ENTER over here?

    Reply
    • Mukesh Otwani says

      February 6, 2019 at 8:54 AM

      Hi Satwik,

      Its Virtual Key Enter

      Reply
      • Bala says

        February 14, 2019 at 1:10 PM

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

        Reply
        • Mukesh Otwani says

          February 15, 2019 at 12:24 PM

          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

          Reply
  8. Seema says

    January 31, 2019 at 1:11 AM

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

    Reply
    • Mukesh Otwani says

      January 31, 2019 at 10:38 AM

      Hi Seema,

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

      Reply
  9. Prathamesh Patil says

    January 25, 2019 at 4:24 PM

    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

    Reply
    • Mukesh Otwani says

      January 27, 2019 at 1:29 AM

      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)

      Reply
  10. Maruti says

    January 24, 2019 at 2:59 PM

    good contents post more like this.

    Reply
    • Mukesh Otwani says

      January 24, 2019 at 3:32 PM

      Thanks Maruti…:)

      Reply
  11. Ankit Kulhade says

    March 17, 2018 at 3:11 PM

    It is really helpful. Thnaks

    Reply
    • Mukesh Otwani says

      March 18, 2018 at 1:00 PM

      Hi Ankit,

      Thanks…:)

      Reply
  12. Rahul Gaike says

    February 14, 2018 at 11:51 PM

    Hi Mukesh!

    Thanks a lot for your site and knowledge sharing!!

    Reply
    • Mukesh Otwani says

      February 15, 2018 at 4:03 PM

      Hi Rahul,

      Thanks for your overwhelming comments…:)

      Reply
  13. Sidhartha Sankar Mohanty says

    February 14, 2018 at 12:47 AM

    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….

    Reply
    • Mukesh Otwani says

      February 14, 2018 at 9:58 AM

      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.

      Reply
  14. Lakshmi says

    January 5, 2018 at 5:02 AM

    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.

    Reply
    • Mukesh Otwani says

      January 5, 2018 at 11:09 AM

      Hi Lakshmi,

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

      Reply
      • Rama says

        February 7, 2018 at 2:08 AM

        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

        Reply
        • Mukesh Otwani says

          February 7, 2018 at 9:44 AM

          Hi Rama,

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

          Reply
  15. sandeep says

    December 26, 2017 at 3:22 PM

    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

    Reply
    • Mukesh Otwani says

      December 28, 2017 at 10:15 AM

      Hi Sandeep,

      I’ll will do it soon…:)

      Reply
  16. Harshal says

    December 12, 2017 at 5:26 PM

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

    Reply
    • Mukesh Otwani says

      December 13, 2017 at 7:01 PM

      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.

      Reply
  17. Mahesh chavan says

    December 8, 2017 at 5:00 PM

    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?

    Reply
    • Mukesh Otwani says

      December 13, 2017 at 11:39 AM

      Hi mahesh,

      Yes, it is possible. Please check this link http://learn-automation.com/handle-multiple-windows-in-selenium-webdriver/

      Reply
  18. Shriya says

    November 23, 2017 at 4:49 PM

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

    Reply
    • Mukesh Otwani says

      November 23, 2017 at 4:58 PM

      Hi Shriya,

      AutoIT is best in windows environment else robot class.

      Reply
  19. Shailendra singh panwar says

    January 26, 2017 at 7:08 PM

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

    Reply
    • Mukesh Otwani says

      January 26, 2017 at 10:12 PM

      Hi Shailendra,

      Thanks for your comments.

      Reply
  20. Learner says

    January 20, 2017 at 9:29 AM

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

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:06 PM

      No James Robot class is part of Java AWT

      Reply
  21. prajeeth says

    January 20, 2017 at 12:34 AM

    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 ???

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 5:05 PM

      Yes we can do that.

      Reply
  22. Girish says

    December 1, 2016 at 5:30 PM

    Nice Blog 🙂

    Reply
    • Mukesh Otwani says

      December 6, 2016 at 12:23 PM

      Thanks Girish

      Reply
  23. celine says

    May 31, 2016 at 12:54 PM

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

    Reply
    • Mukesh Otwani says

      June 1, 2016 at 4:17 PM

      no it will work without span too

      Reply
  24. Reshma Sultana says

    April 29, 2016 at 10:11 AM

    Hi Mukesh,

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

    Reply
    • Mukesh otwani says

      April 30, 2016 at 3:51 PM

      Hi Reshma,

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

      Reply
      • Reshma Sultana says

        April 30, 2016 at 7:50 PM

        ok.Thank you.

        Reply
  25. Aileen says

    February 26, 2016 at 11:41 AM

    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?

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 2:10 PM

      Hi Aileen,

      Here VK stands for virtual keys.

      Reply
  26. Manas says

    February 3, 2016 at 2:26 PM

    Nice one..Thanks

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:19 PM

      Hi Manas,

      Thanks 🙂

      Reply
  27. ManiRaj says

    January 5, 2016 at 11:46 AM

    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

    Reply
    • Mukesh Otwani says

      January 6, 2016 at 12:56 AM

      Hi Mani,

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

      Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?