How To Upload A File Using Robot Class

In selenium Web Driver, we can upload a file while creating a script by using Autoit script or by using Robot Class. Here I would be explaining you guys about the file upload by using Robot class. It is quite easy in comparison to Autoit script. The only difficulty is that you need to write more number of steps if you are using Robot class. But anyways it is easy. Let’s have a look how to do it.

1. Go to eclipse, create a java project. Create a package under your project and then create a class.

r4

 

2. Now in the created class, you need to do a little coding. First of all you need to add the browser on which you want to run your script. Here I am taking it as Firefox. So you need to write the following code :

       WebDriver driver= new FirefoxDriver();

z

 

3. You will see some red lines under the WebDriver and FirefoxDriver. To resolve these errors you need to hover the cursor over the red lines, it will show some suggestions regarding the import of some files. You just click on those import statements.

z1

 

4. Once your errors are resolved, you can give some site’s URL where you will get an option of Upload file. For example : http://www180.lunapic.com/. At this site, under Border Tool options, you will get an option of Browse. So go till that option I have written the necessary code here.

driver.get(“http://www180.lunapic.com/”);
driver.findElement(By.linkText(“Borders”)).click();
driver.findElement(By.linkText(“Border Tool”)).click();
driver.findElement(By.xpath(“//*[@id=’actionbox’]/center/form[2]/input[7]”)).click();

z2

5. Till here it was easy to write the code but now you need to switch form the web page to your local system window to chose the file you want to upload. For this we will be using Robot class here. So I am creating an instance of robot class here and then we will be using its functions or features to get the file we want to upload.

z3

 

When you create an instance of class Robot, you will get an error on new Robot() statement. To resolve this just hover the cursor on it, you will get a suggestion to add throws AUTException.

6. Now suppose you want to upload a file which is present in E drive of your local system and the name of the file is 123.txt. So the address to that file will be E:\123.txt. To get this file the Robot class will emulate the keyboard and mouse actions. To do this , we have some simple steps which I will explain one by one. So we have the above address  E:\123.txt

For this we need to press E and then release it. After that we need to press shift, press semicolon, and then release shift(in order to get the colon). And then we need to press Backslash, one, two, and three. The last step is to press Enter key.

To get this address we need to write the events.

7. To press and release the E button, we need to add the following lines of code :

rb.keyPress(java.awt.event.KeyEvent.VK_E);
rb.keyRelease(java.awt.event.KeyEvent.VK_E);

8. To press and release the other keys, you just need to add these lines :

rb.keyPress(java.awt.event.KeyEvent.VK_SHIFT);
rb.keyPress(java.awt.event.KeyEvent.VK_SEMICOLON);
rb.keyRelease(java.awt.event.KeyEvent.VK_SEMICOLON);
rb.keyRelease(java.awt.event.KeyEvent.VK_SHIFT);

// Here to get the colon, we can not use COLON keyword directly, we need to follow the same steps as we do it on our keyboard i.e; first we need to press shift button, and then we need to click on the semicolon button and then we have to release the shift button.

rb.keyPress(java.awt.event.KeyEvent.VK_BACK_SLASH);
rb.keyRelease(java.awt.event.KeyEvent.VK_BACK_SLASH);

rb.keyPress(java.awt.event.KeyEvent.VK_1);
rb.keyRelease(java.awt.event.KeyEvent.VK_1);

rb.keyPress(java.awt.event.KeyEvent.VK_2);
rb.keyRelease(java.awt.event.KeyEvent.VK_2);

rb.keyPress(java.awt.event.KeyEvent.VK_3);
rb.keyRelease(java.awt.event.KeyEvent.VK_3);

rb.keyPress(java.awt.event.KeyEvent.VK_ENTER);
rb.keyRelease(java.awt.event.KeyEvent.VK_ENTER);

//After writing the actions for all the keys, we can directly press Enter button, it will work as submit command.

 

z4

9. That’s it. Now you can run your script and can check whether it is working or not. I know it will work. Just try it out. 😉

Add Comment