Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Monday, 17 November 2014

How to upload file in Selenium Webdriver using Robot Class

Upload File 

Problems in Selenium while handling window prompt

Selenium find some difficulties to find object and perform actions which is related to mouse or keyboard type of activity.It is difficult to handle Keyboard and mouse events/Action  using selenium commands.There are some specific scenario where selenium command is launched but actual event did not fired.At that time Excecution stops  and reports error.

Selenium doesn't provide support to handle browser pop-ups and window based pop ups.There are some methods in Robot class which are used to do the interaction with popups in WebApplications.For handling the pop-up and window-prompt we need to use Robot class..

What is Robot Class?



By using Robot Class we can perform actions on keyboard or mouse events.Robot class is very easy to use with automation process. It can be easily integrate with current automation framework.

Some of the methods of robot class which we are going to use

.keyPress();
KeyPress is a method  if we pressctrl button of keyboard.
.keyRelease();
If we have press a ctrl button we have to release that button also.
Here i am giving you an example if i press ctrl v from keyboard.Then first i have to 
Make an object of Robot Class and implement the robot methods.

Robot r = new Robot();

r.keyPress(KeyEvent.VK_CONTROL); //This is for pressing  ctrl button  of Keyboard

 r.keyPress(KeyEvent.VK_V);  //This is for pressing  v button of keyboard



Now we  have to release that keys so we will write..

r.keyRelease(KeyEvent.VK_V);   //This is for relasing the ctrl button of KeyBoard
 r.keyRelease(KeyEvent.VK_CONTROL);//This is for releasing V button of keyBoard.

Here i this scenario i have used Robot class.

Scenario
1.Go to 
http://api.checklist.com/login
2.Enter Email as rgulati883@gmail.com
3.Enter Password as jaiguruji
4.Click on Login
5.Click on Checklist1
6.Click on upload
7.Then add path of your File
8.Click on open button.
9.File Added





1.Enter email= rgulati883@gmail.com

2.Enter password = jaiguruji

3.Click on Login



4Click on Checklist1(0)

5.Click on upload


6.So next step is we copy the path where our file on desktop.
 //  StringSelection ss = new StringSelection("Your file path ");

 StringSelection ss = new StringSelection("C:\\Users\\Hp\\Desktop\\FileuploadTest.txt");
 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
 When you do a cut or copy of text in the operating system, the text is
  stored in the clipboard 


 The following method returns the content that is currently in the 

 clipboard.


7.Now take the curser to the textbox of your File name 



// Calling key press event to press Enter Key from keyboard to place cursor in window textbox
 r.keyPress(KeyEvent.VK_ENTER);
 //Releaseing the Enter Key
 r.keyRelease(KeyEvent.VK_ENTER);

8.After that we copy and paste that path in this FilepathText box. 

/*

  * Now we are going to trigger CTRL+V action so first we will press CTRL and then V and finally will

  * release these key.

  */

 r.keyPress(KeyEvent.VK_CONTROL);  
 r.keyPress(KeyEvent.VK_V);
 
 r.keyRelease(KeyEvent.VK_V);  
 r.keyRelease(KeyEvent.VK_CONTROL);
 
9.Now click on Open 
// After pasting path now we are going to click on Open and that can be 
 //triggered by pressing enter key from Keyboard.
 r.keyPress(KeyEvent.VK_ENTER);
 r.keyRelease(KeyEvent.VK_ENTER);
 //Release open 
10 File is added 
package test;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestFileupload {

public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Hp\\Desktop\\Chromepth\\chromedriver.exe");
     WebDriver driver =  new ChromeDriver();
// WebDriver driver = new FirefoxDriver();
driver.get("http://api.checklist.com/login");
driver.findElement(By.xpath("//*[@id='email']")).sendKeys("rgulati883@gmail.com");
driver.findElement(By.xpath("//*[@id='loginForm']/div[2]/input")).sendKeys("jaiguruji");
driver.findElement(By.xpath("//button[@class='btn btn btn-primary pull-right']")).click();
Thread.sleep(8000);
driver.findElement(By.xpath("//*[@id='userChecklists']/li[1]/a/span")).click();
WebElement UploadImg = driver.findElement(By.xpath("//*[@id='taskUploadFile']"));
 UploadImg.click();
 
                  //  StringSelection ss = new StringSelection("Your file path ");
 StringSelection ss = new StringSelection("C:\\Users\\Hp\\Desktop\\FileuploadTest.txt");
 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
 Robot r = new Robot();
 
 
 // Calling key press event to press Enter Key from keyboard to place cursor in window textbox
 r.keyPress(KeyEvent.VK_ENTER);
 //Releaseing the Enter Key
 r.keyRelease(KeyEvent.VK_ENTER);
 /*
  * Now we are going to trigger CTRL+V action so first we will press CTRL and then V and finally will
  * release these key.
  */
 r.keyPress(KeyEvent.VK_CONTROL);  
 r.keyPress(KeyEvent.VK_V);
 
 r.keyRelease(KeyEvent.VK_V);  
 r.keyRelease(KeyEvent.VK_CONTROL);
  
 // After pasting path now we are going to click on Open and that can be 
 //triggered by pressing enter key from Keyboard.
 r.keyPress(KeyEvent.VK_ENTER);
 r.keyRelease(KeyEvent.VK_ENTER);
 //Release open 
  
}

}
                                                                                      
//After using this code file is added.

    23 comments:

    1. Your code is not working.
      After clicking the browse button window gets open but nothing happens after that

      ReplyDelete
    2. I have tried your code to try to upload a file and I am getting stuck at the point where the Upload window opens and path to the file gets pasted in the text box and then nothing happens. It is as if the enter key is not getting pressed and released after the pasting of file path. Following is my code:

      StringSelection ss = new StringSelection("C:\\Users\\IBM_ADMIN\\Desktop\\Apps_For_Thick_Web_Consoles\\exe\\kbsetup.exe");
      Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
      Robot r = new Robot();
      r.keyPress(KeyEvent.VK_ENTER);
      r.keyRelease(KeyEvent.VK_ENTER);
      r.keyPress(KeyEvent.VK_CONTROL);
      r.keyPress(KeyEvent.VK_V);
      r.keyRelease(KeyEvent.VK_CONTROL);
      r.keyRelease(KeyEvent.VK_V);
      r.keyPress(KeyEvent.VK_ENTER);
      r.keyRelease(KeyEvent.VK_ENTER);

      Any pointers you can give in how to troubleshoot this would be greatly appreciated.

      ReplyDelete
      Replies
      1. hi hitesh, just give after the last line in the above code like
        r.delay(1000);

        Delete
    3. hi hitesh,sorry i misguided and not in the last line,it will come after the releasing control +v,i.e,r.keyRelease(KeyEvent.VK_V); after this u should use r.delay(1000); it worked for me

      ReplyDelete
    4. How to close the opened frame??

      ReplyDelete
    5. What if i have to attach any file in gmail,& not the pasted one in particular..??
      is that possible in ubuntu..??

      ReplyDelete
    6. Does this method work in IE..it works in other browsers...but seems not in IE...lpls confirm

      ReplyDelete
    7. That's great! I always thought that once you brought up the Windows file upload dialog that Selenium stopped until you took the dialog down because Windows, not the browser, was now in control. My experiments even seemed to show that. Just to learn about the Robot functionality,I adapted your code, above, to mine and it worked!!! I was afraid I was going to have to take some drastic steps to get my file upload tests to work before I found this. Thank you! Thank you! Thank you!

      ReplyDelete
    8. Does this work on a machine which uses Windows Server 2012 ? I tried this but it never pastes the filepath in filename input.

      ReplyDelete
      Replies
      1. @Musaffir: Even I am facing the same issue my file to upload script works in chrome but not in IE (I tried using both Robot & autoIT)). My IE browser version is 11.0.9600.18059 not sure is this only on this version of IE. Let me know if you find any alternative solution.

        Delete
    9. @Musaffir: Even I am facing the same issue my file to upload script works in chrome but not in IE (I tried using both Robot & autoIT)). My IE browser version is 11.0.9600.18059 not sure is this only on this version of IE. Let me know if you find any alternative solution.

      ReplyDelete
    10. Thank you... the code works after the correction is made

      ReplyDelete
    11. you need more publicize this so many people who know about it are rare for people to know this
      Salesforce Training in Hyderabad

      ReplyDelete
    12. nice post about uploading in selenium..
      SAS Institute introduced the SAS Certified Professional Program,training proper understanding of how the SAS software works. Among the five certification programs that SAS Institute has come up with, SAS training can be considered as the entry point into the big data and the data analytics industry.
      SAS online training in hyderabad

      ReplyDelete
    13. This works fine for me but when switch to web page I received (org.openqa.selenium.UnhandledAlertException: Modal dialog present:
      Exception and not allow me to proceed

      ReplyDelete
    14. How can we upload multiple files using robot class?kindly help me to proceed

      ReplyDelete
    15. nice blog has been shared by you before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
      Selenium Training in Bangalore

      ReplyDelete
    16. nice blog has been shared by you. before i read this blog i didn't have any knowledge about this. so keep on sharing such kind of an interesting blogs.
      Devops Training in Bangalore

      ReplyDelete
    17. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!

      java Training in Bangalore

      ReplyDelete
    18. Thank you for offering such a unique information really helpful for learners one of the recommended blog... Selenium Training in Chennai | Selenium Training institutes in Chennai

      ReplyDelete

    Popular Posts

     
    subscribe
    Subscribe Us
    emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: