Upload files with Selenium IDE
Apr.03, 2009
I started using selenium about 2 weeks ago. Find it as a very good QA tool.
But after a few successfully written tests I met the problem: Selenium is not able to use file input field. JavaScript permission restriction doesn’t allow it.
Here is my solution written with JInvoke library: Jinvoke provides the classes to simulate the input to file chooser form.
The problem I meat is selenium is stuck when simulate the click on upload file input. I have to launch the concurent thread that do the file name input. Thread code(Note file name should be given in Java format like ‘c:/boot.ini):
Here is selenium call method:
:java, selenium
Here is my solution written with JInvoke library: Jinvoke provides the classes to simulate the input to file chooser form.
The problem I meat is selenium is stuck when simulate the click on upload file input. I have to launch the concurent thread that do the file name input. Thread code(Note file name should be given in Java format like ‘c:/boot.ini):
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.*;
/**
* @author Bogdan Gusiev
* Date 29.03.2009
*/
public class FileChooserThread extends Thread {
public FileChooserThread(String file) {
super(new FileRunner(file));
}
}
class FileRunner implements Runnable {
private String fullName;
public FileRunner(String fileName) {
this.fullName = fileName;
}
public void run() {
try {
Thread.sleep(1000);
Robot robot = new Robot(); //input simulation class
for (char c : fullName.toCharArray()) {
if (c == ':') {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_SEMICOLON);
robot.keyRelease(KeyEvent.VK_SHIFT);
} else if (c == '/') {
robot.keyPress(KeyEvent.VK_BACK_SLASH);
} else {
robot.keyPress(KeyStroke.getKeyStroke(
Character.toUpperCase(c), 0).getKeyCode());
}
}
robot.keyPress(KeyEvent.VK_ENTER);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
protected void chooseFile(String element, String fileName) {
Number positionLeft = selenium.getElementPositionLeft(element);
Number positionTop = selenium.getElementPositionTop(element);
new FileChooserThread(fileName).start(); //launch input thread.
//this method will held current thread while FileChooser gives the file name
selenium.clickAt("file", positionLeft + "," + positionTop);
}
May 12th, 2009 on 9:59 pm
I automated file uploads with the iMacros addon (FF and IE!): http://www.iopus.com/imacros/firefox/
Some of my tests use Selenium, others run in iMacros…
July 25th, 2009 on 5:05 am
One issue, the method chooseFile is a void, but it returns the String fileName.
October 15th, 2009 on 11:44 am
Hi there,
Using thread, my selenium stuck too when “file open dialog” come out!
Do you have any clue?
BTW, did you input path on file open dialog?
I saw your thread started to run before doing selenium.clickAt(”file”, positionLeft + “,” + positionTop); ?
suppose the action flow is opposite? or you use this trying to avoid “stuck” issue?
but what if thread start before the clickat(”file”) ?
October 15th, 2009 on 12:00 pm
Hi Jim, As you can see here the thread has a little sleep time.
Thread.sleep(1000);
It waits while the main thread will do the click.
The file name passes as the constructor argument to FileChooserThread and the Robot class is doing the input. Thanks,
Bogdan
October 17th, 2009 on 4:38 am
Bogdan,
Thanks it works for me~ BTW, do you have any idea to let selenium manipulate flash object?
I found 2 main methods on google,
1. to inject swj object to insert interface for javascript to call. but in our case we cannot modify flash object-to-test.
2. use FlexMonkey, I’m still researching to see if selenium can call it by commandline and wait for it’s job finish signal. Any suggestions? Thanks again
December 11th, 2009 on 3:37 pm
Thanks, Can you please let me know what is this variable “element” and from where it’s coming and what it contains? thanks,
Nikesh
March 17th, 2010 on 5:13 pm
Uploading Files using Selenium RC in C#
After googling all around to overcome the Selenium incapability of handling File Upload control finally I could write a simple function in C# which will do my job. In the function given below _selObj is the object of DefaultSelenium class. public bool TypeIntoFileUpload(string controlId, string filePath)
{
try
{
string newFilePath = filePath.Replace(’\\’, ‘/’);
_selObj.WindowFocus();
_selObj.Focus(controlId);
string jscript=”";
jscript += “if(selenium.browserbot.getCurrentWindow().clipboardData){window.clipboardData.setData(’Text’,'” + newFilePath + “‘);}”;
_selObj.GetEval(jscript);
byte VK_CONTROL = 0×11;
byte VK_V = 0×56;
_selObj.KeyDownNative(Convert.ToString(VK_CONTROL));
_selObj.KeyPressNative(Convert.ToString(VK_V));
_selObj.KeyUpNative(Convert.ToString(VK_CONTROL)); return true;
}
catch (Exception exc)
{
return false;
}
}
April 21st, 2010 on 4:05 pm
@Nikesh I think it refers to the button or “quoteBox” which you have to press in order to get the link to the file.
If I’m wrong, please correct me.
July 24th, 2010 on 11:19 pm
Perfect tutorial. I was looking for this…