2

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);
1

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);
1
  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    

protected by Community Oct 30 '18 at 17:32

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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