3

I'm trying to build a framework using Selenium. There is a scenario where I need to download a file, and there is another where I need to upload one. I've gone through the option in AutoIt, but it doesn't allow me to put in a varying filename. Then I came across Robot where in I can send custom strings. However, for some reason, the piece of code is not entering text into the field. I see that the cursor is in the field, but the text is not getting entered. What could be the reason?

Also, is there an option to make the mouse click on a particular button, say Open/Cancel using Robot?(Just like AutoIT can do)

    String text = "sampledoc.txt";
    StringSelection stringSelection = new StringSelection(text);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(stringSelection, stringSelection);

    r.keyPress(KeyEvent.VK_CONTROL);
    r.keyPress(KeyEvent.VK_V);
    r.keyRelease(KeyEvent.VK_V);
    r.keyRelease(KeyEvent.VK_CONTROL);
2
  • For uploads using Robot, check out this awesome answer by Manali J here : sqa.stackexchange.com/a/12871/7313 Commented Nov 5, 2015 at 8:11
  • great solution when all other methods failed to work..Thanks :)
    – Vanya
    Commented Feb 10, 2017 at 6:13

3 Answers 3

2

Upload

No need to use Autoit or Robot class for uploading a file in browser.

using WebDriver itself we can achieve that.

Pass the absolute file path to file locator.

driver.findElement(By.cssSelector("input[type='file']")).sendKeys("/home/test/uploadthis.png");

Download

For downloading a file we need to set it in browser preferences. It varies based on browsers.

Firefox

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.dir","/home/Desktop/");
profile.setPreference("browser.helperApps.neverAsk.openFile","text/csv");

DesiredCapabilities dc = DesiredCapabilities.firefox(); 
dc.setCapability(FirefoxDriver.PROFILE, profile);

WebDriver driver = new FirefoxDriver(dc);
2
  1. Click on Upload File
  2. Copy your Path in Clipboard (as Shown in the code)
  3. Press Ctrl+Vand hit Enter using robot class

    StringSelection selection = new StringSelection(file);
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    clipboard.setContents(selection, selection);
    
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    
0

For me, pressing an Enter before the Control+V works. Code:

        StringSelection ss = new StringSelection(DocSampleFilePath);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); //copy the above string to clip board     
        Robot robot;
        robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(2000); 
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL); //paste the copied string into the dialog box
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER); //enter    

Not the answer you're looking for? Browse other questions tagged or ask your own question.