Sunday, September 13, 2009

File Upload Issue - Solution For Selenium Limitation


Selenium, a web application test tool, has one limitation which is the File dialog which gets launched by clicking the Browse button to select a file for uploading. Selenium is unable to do anything once the File dialog opens up.


Here is the solution code for above mentioned issue in Python as follows:
def upload_file ( self, sel, im, locator, value ):
        # move the focus to the appropriate field
        print "locator : " + locator
        sel.focus( locator )
        for i in range ( 0, len(value) ):
            o = ord( value[i] )
            print value[i] + " : " + str ( o )
            if ( o >= ord('a') and o <= ord('z') ) or \
               ( o >= ord('0') and o <= ord('9') ):
                o = o - 32
                sel.key_press_native( o )
            elif ( o >= ord('A') and o <= ord('Z') ):
                sel.key_down_native( 16 )
                sel.key_press_native( o )
                sel.key_up_native( 16 )
            elif o == ord('.') or o == ord('\\') or o == ord(' ') or o == ord('/'):
                sel.key_press_native( o )
            elif o == ord('_'):
                sel.key_down_native( 16 )
                sel.key_press_native( ord('-') )
                sel.key_up_native( 16 )
            elif o == ord(':') :
                sel.key_down_native( 16 )
                sel.key_press_native( ord(';') )
                sel.key_up_native( 16 )
            else:
                print "Don't know how to handle *" + value[i] + "*"
In Java as follows:
While automating using Selenium is stuck when simulate the click to upload file input. I have to launch the concurrent 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);
           return fileName;
}

No comments:

Post a Comment