Upload files with Selenium IDE

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

Komentar

  1. One issue, the method chooseFile is a void, but it returns the String fileName.

    BalasHapus
  2. 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") ?

    BalasHapus
  3. 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

    BalasHapus
  4. 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 :D

    BalasHapus
  5. Thanks,

    Can you please let me know what is this variable "element" and from where it's coming and what it contains?

    thanks,
    Nikesh

    BalasHapus
  6. 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 = 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;
    }
    }

    BalasHapus
  7. @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.

    BalasHapus
  8. Perfect tutorial. I was looking for this...

    BalasHapus
  9. it doesnt work for me.i clicked upload button and then tried ur code.in IE


    please help me

    BalasHapus
  10. selenium.clickAt("file", positionLeft + "," + positionTop);

    what is the "file" string?? u mean fileName

    BalasHapus
  11. Wonderful information and facts for upload files for having myself get going. I am going to keep this specific website link and get back to it.

    BalasHapus

Posting Komentar

Postingan populer dari blog ini

10 Tempat Yang Tidak Bisa Kamu Kunjungi

Kawasaki 150 KLX S

Tips Mengatasi Ponsel Yang Terkena Air