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.
This class has many inbuild method which helps us to perform our task.
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
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
- 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.
- If you are using X, Y coordinates for your test then the test will behave differently on different screens (resolution)
- 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
stqatools says
Really great blog related to robot class in selenium
Mukesh Otwani says
Thanks…:)
stqatools says
Thanks, mukesh for this great robot class in selenium in java please provide same blog using python also thanks.
Mukesh Otwani says
Hi there,
I’ll post for python soon…:)
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
Mukesh Otwani says
Hi Swamy, if that is in the same window then directly inspect and perform the operation.
Note- Please check frame as well
pachiyappan says
Thanks Mukesh.
Pachiyappan says
How to upload multiple files at once using robot class.
Mukesh Otwani says
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/
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.
Mukesh Otwani says
Hi Swetha,
You are always welcome…:)
Satwik says
What is VK_ENTER over here?
Mukesh Otwani says
Hi Satwik,
Its Virtual Key Enter
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.
Mukesh Otwani says
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
Seema says
nice content..easily understandable.
pls post some postman tutorials for API testing & API Automation
Mukesh Otwani says
Hi Seema,
Thanks for your comments…:)
Requested contents are already in pipeline.
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)
Maruti says
good contents post more like this.
Mukesh Otwani says
Thanks Maruti…:)
Ankit Kulhade says
It is really helpful. Thnaks
Mukesh Otwani says
Hi Ankit,
Thanks…:)
Rahul Gaike says
Hi Mukesh!
Thanks a lot for your site and knowledge sharing!!
Mukesh Otwani says
Hi Rahul,
Thanks for your overwhelming comments…:)
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.
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.
Mukesh Otwani says
Hi Lakshmi,
Thanks for your valuable appreciation. I’ll always try my level best to serve my subscribers…:)
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
Mukesh Otwani says
Hi Rama,
Clicking on the checkbox is same like you click on any button or element.
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
Mukesh Otwani says
Hi Sandeep,
I’ll will do it soon…:)
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.
Mukesh Otwani says
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.
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?
Mukesh Otwani says
Hi mahesh,
Yes, it is possible. Please check this link http://learn-automation.com/handle-multiple-windows-in-selenium-webdriver/
Shriya says
hi Mukesh,
For uploading a file in selenium webdriver, which is best – Robot class or autoit ?
Mukesh Otwani says
Hi Shriya,
AutoIT is best in windows environment else robot class.
Shailendra singh panwar says
Hello mukesh sir ,
Is too helpfull wbsite for learn automation thanks for share your knowledge
Thanks alot
Mukesh Otwani says
Hi Shailendra,
Thanks for your comments.
Learner says
Am I supposed to install any special jar or any other files for this method to work?
Mukesh Otwani says
No James Robot class is part of Java AWT
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 ???
Mukesh Otwani says
Yes we can do that.
Girish says
Nice Blog 🙂
Mukesh Otwani says
Thanks Girish
celine says
Is the first line,
necessary for this code to work?
Mukesh Otwani says
no it will work without span too
Reshma Sultana says
Hi Mukesh,
In above program you mentioned “@SuppressWarnings(“unused”)”. Could you please explain me what does that mean?
Mukesh otwani says
Hi Reshma,
This is optional using above code you wont find any warnings in ur code.
Reshma Sultana says
ok.Thank you.
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?
Mukesh Otwani says
Hi Aileen,
Here VK stands for virtual keys.
Manas says
Nice one..Thanks
Mukesh Otwani says
Hi Manas,
Thanks 🙂
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
Mukesh Otwani says
Hi Mani,
It will hit enter and by default it will hit login button. If you have multiple buttons then it won’t work.