3

I am using Selenium RC and Java and I need to automate the file upload feature.I tried a lot, Clicking on Browse button is not executing and rather getting stuck. I tried in FireFox but to no avail.

Below is my code:I used different commands to load file ...like "type" & "AttachFile".pls can any one help me how to upload a file from "File Upload window".

RemoteControlConfiguration rcc =new RemoteControlConfiguration();
        rcc.setPort(3333);
        SeleniumServer ss = new SeleniumServer(rcc);
        ss.start();



DefaultSelenium  browser = new DefaultSelenium("localhost" , 3333 , "*firefox" ,"http://");
        browser.start();
        browser.open("http://sqa.stackexchange.com/questions/ask");
        browser.windowMaximize();
        browser.setSpeed("20000");
        browser.click("id=nav-askquestion");
        browser.click("id=wmd-image-button");

//Used type command
             browser.type("id=upload-file-input", "C:\\Documents and Settings\\Home\\Desktop\\dropdown.jpg");

//Used AttachFile command
  browser.attachFile("id=upload-file-input","C:\\Documents and Settings\\Home\\Desktop\\dropdown.jpg");

//Used Runtime command
    Runtime.getRuntime().exec("C:\\Documents and Settings\\Home\\Desktop\\dropdown.jpg");![enter image description here][1]
1
  • Can u Just post what kind of error message you are getting ..
    – selva
    Commented Feb 7, 2013 at 12:56

2 Answers 2

2

If the form is normal html form, you can skip opening the file upload dialog (which you don't need to test as hopefully you haven't implemented it yourself), and just enter the filename to the form field.

WebElement uploadInputField = browser.findElement(By.Id("upload-file-input"));
uploadInputField.sendKeys("C:\\Documents and Settings\\Home\\Desktop\\dropdown.jpg");
1
  • 1
    that's exactly correct. i use it all over the place in my tests. make sure you use the \\ instead of just a single \
    – squeemish
    Commented Feb 7, 2013 at 19:29
0

According to this, the second parameter to the browser.attachFile method is a URL, not a file path. In your case, the URL will be something like file:///C|/Documents and Settings/Home/Desktop/dropdown.jpg.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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