Selenium RC: How to Upload and Submit Files Using Selenium and AutoIt

by amirg on August 11, 2010

Article by Amir Ghahrai

Many web applications contain HTML forms which interact with the user. The user is able to enter data in to the form fields and submit the data to a server. Selenium is able to handle form inputs such as entering text in text fields, selecting radio buttons, selecting a value from a drop-down menu and so on. Things get a little tricky when dealing with input form fields of type “file”, to allow users to browse for a file and attach the file to the HTML form and submit the file.

So, what is the problem?
I can’t seem to use Selenium Core to upload a file; when I try to type in the file upload text field, nothing happens!

There seems to be two inter-related problems:

1 – Unfortunately, this is yet another JavaScript security restriction; JS is not allowed to modify the value of input type=”file” form fields. You can work around this by running your tests under Selenium IDE or under Selenium RC running in the experimental “*chrome” mode for Firefox, but at present there is no straight forward way to do this in Selenium Core.

2 – Handling of the “Choose File” dialog box with Selenium alone is not possible. We need to have another program running to select the path and file from the “Choose File” dialog box.

So, How can we upload files?
Fortunately, there exists a workaround to the above problems. This is where the magic of AutoIt and Selenium combination can work wonders!

First we will write a simple script in AutoIt and save the file as an executable: (please read documentation on AutoIt website to learn how to save the scripts as an executable file.

WinWaitActive("Choose file")
Send("C:\attach\samplefile.txt") \\location of the file you want to attach to the form and submit
Send("{ENTER}")

We shall name the above file as attachFile.exe

Now, within a Java code, we can run a process which will execute the above program just before when we want to upload and submit a file.

package com.company;

import java.io.IOException;
import com.thoughtworks.selenium.Selenium;

public class AddAttachment {
   public static void attach(Selenium selenium, String fileName) {
      try {
         String[] commands = new String[]{};
         commands = new String[]{"c:\\test\\attachFile.exe"}; //location of the autoit executable
         Runtime.getRuntime().exec(commands);
      } catch (IOException e) {}

      //autoit executable is now waiting for a "Choose file" dialog to popup
      selenium.click("name=browseButton");
      //once the "Choose file" dialog is opened, the autoit will input the path and file name
   }
}

The above seems to be the easiest way to deal with file uploads and attachments using Selenium.

Related Posts

{ 9 comments… read them below or add one }

Sunny September 23, 2010 at 3:03 pm

Hi,

When am using the above AutoIt script to upload file it is working fine for IE but its not working for firefox. Please let me know the reason for not working with firefox to upload file…

thanks

admin September 23, 2010 at 3:57 pm

Can you tell me at what stage it’s not working? Unfortunately, I’m not able to start FireFox with Selenium for start anyway, but my assumption is that maybe the dialogue box that is opened via FF browser, has a different name? and Autoit script is only looking for the dialog box with name ‘Choose File’.
Let me know at which point it is not working.

-Amir

Sunny October 7, 2010 at 6:45 am

Hi Amir,
Please find the following code
The html code to upload file :

Code in selenium to upload file:

Utility.Browser.waitForPageToLoad(“60000″);
Runtime.getRuntime().exec(“D:\\Myfirstautoitscript\\upload.exe”);
Utility.Browser.click(“file0″);

When click command is executed as mentioned in above code am seeing response result as true in selenium RC. But action was not performed actually…I mean when click command is executed the “file upload” dialog box was not at all opened as in IE which opens “Choose File” dialog box..
I don’t know what was restricting the firefox in opening that dialog box when click command was execute….If that dialog box opens then I can hope that autoit script might work as exepected…Please help me in this issue…

midhun November 23, 2010 at 3:08 pm

Lovely solution.. I was trying for the same from past few days, with little modification I am able to run this script for flash application.. Thanks alot for such a wonderful script and explaining it precisely..

tester May 2, 2011 at 11:03 am

Hi,

What external files (i means .jar files ) i need to add to execute this script. Please post me at tester26.tester@gmail.com

your reply is appreciated.

xiaodan June 7, 2011 at 10:25 am

HI,
when i click browse button with selenium IDE,It doesn’t show “Choose File” dialog box .also i try clickAt,clickAtfor…etc.please post me at wangxiaokun133@126.com.

your reply is appreciated

Nasser December 9, 2011 at 7:41 pm

Hello everyone,

I am trying to use Selenium IDE to open Microsoft outlook inbox, compose and attach a file to the email I want to send. Selenium IDE can’t automatically open the attachment. Can some tell me what to do? I am totally new to Selenium IDE. Is there any script function I can add?

Nasser

Roosevelt P December 14, 2011 at 8:02 pm

How do I make even make the dialog box to appear? Clicking the browser file input field does not even popup the “Choose File” dialog window ><!

Atif December 20, 2011 at 2:22 pm

How to upload file using Selenium IDE…Please help in this regard. thanks.

Leave a Comment

*

{ 1 trackback }