ADSENSE HERE!
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):
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);
}
}
}
Here is selenium call method:
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);
}
ADSENSE HERE!
I automated file uploads with the iMacros addon (FF and IE!): http://www.iopus.com/imacros/firefox/
ReplyDeleteSome of my tests use Selenium, others run in iMacros...
One issue, the method chooseFile is a void, but it returns the String fileName.
ReplyDeleteHi there,
ReplyDeleteUsing 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") ?
Hi Jim,
ReplyDeleteAs 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
Bogdan,
ReplyDeleteThanks 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 :D
Thanks,
ReplyDeleteCan you please let me know what is this variable "element" and from where it's coming and what it contains?
thanks,
Nikesh
Uploading Files using Selenium RC in C#
ReplyDeleteAfter 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 = 0x11;
byte VK_V = 0x56;
_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;
}
}
@Nikesh I think it refers to the button or "quoteBox" which you have to press in order to get the link to the file.
ReplyDeleteIf I'm wrong, please correct me.
Perfect tutorial. I was looking for this...
ReplyDeleteit doesnt work for me.i clicked upload button and then tried ur code.in IE
ReplyDeleteplease help me
selenium.clickAt("file", positionLeft + "," + positionTop);
ReplyDeletewhat is the "file" string?? u mean fileName