[フレーム]

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

by 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

Reader Interactions

Comments

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

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

    • Mukesh Otwani says

      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)

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

    • Mukesh Otwani says

      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.

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

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

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

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

  8. Shailendra singh panwar says

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

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

  10. Reshma Sultana says

    Hi Mukesh,

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

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

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

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.

AltStyle によって変換されたページ (->オリジナル) /