Question

I'm trying to set up tests for a upload using the plupload queue widget. I'm using Splinter for in-browser test, but I couldn't find a way to make it happen. Splinter has some methods to attach files, but only if it's a simple file field. Another way would be click the button to browse the files, and choose the file... but I don't think it's possible using Splinter (or selenium), is it? Or with drag-n-drop of the files.

Anyone has any suggestion of the best way to automatize theses tests?

Was it helpful?

Solution

Its possible to automate user actions done on PLUpload control using Selenium- WebDriver. Please find the WebDriver C# code below, which clicks on a flash button object and selects a file using keyboard events,

using System;
using System.Windows.Forms;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Interactions;
using NUnit.Framework;
namespace BusinessCreation
{
    class PlUpload
    {
        static void Main(string[] args)
        {
               IWebDriver driver = new FirefoxDriver();
               driver.Navigate().GoToUrl("http://www.plupload.com/example_queuewidget.php");
               driver.FindElement(By.XPath("//object[@data='/plupload/js/plupload.flash.swf']")).Click();
               SendKeys.SendWait(@"C:\Users\Public\Pictures\Sample Pictures\Dock.jpg");
               SendKeys.SendWait(@"{Enter}");
         }
    }
}

OTHER TIPS

I had a similar problem with the PlUpload widget. Thank you to CheryJose for putting me on the right track.

First of all I had to create a small class to find out which windows were open and return them as a dictionary of windows.

public static IDictionary<string, IntPtr> GetOpenWindows()
{
    IntPtr lShellWindow = GetShellWindow();
    Dictionary<string, IntPtr> lWindows = new Dictionary<string, IntPtr>();
    EnumWindows(delegate(IntPtr hWnd, int lParam)
    {
        if (hWnd == lShellWindow) return true;
        if (!IsWindowVisible(hWnd)) return true;
        int lLength = GetWindowTextLength(hWnd);
        if (lLength == 0) return true;

        StringBuilder lBuilder = new StringBuilder(lLength);
        GetWindowText(hWnd, lBuilder, lLength + 1);
        lWindows[lBuilder.ToString()] = hWnd;
        return true;
    }, 0);

    return lWindows;
}

public delegate bool EnumDelegate(IntPtr hWnd, int lParam);
public delegate bool EnumedWindow(IntPtr handleWindow, ArrayList handles);

[DllImport("USER32.DLL")]
public static extern bool EnumWindows(EnumDelegate enumFunc, int lParam);

[DllImport("USER32.DLL")]
public static extern IntPtr GetShellWindow();

[DllImport("USER32.DLL")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("USER32.DLL")]
public static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("USER32.DLL")]
public static extern bool IsWindowVisible(IntPtr hWnd);

[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

In the Selenium page code I click on the button to launch the PlUpload window and put in a short wait. (This is not shown in the code)

Then I use the code below to find all open windows

IDictionary<string, IntPtr> getOpenWindows = GetOpenWindows();

Switch to the PlUpload window (the name of the window is different across browsers. Be Aware!)

IntPtr hWnd = getOpenWindows["File Upload"];
SetForegroundWindow(hWnd);

Type in the path for the file

SendKeys.SendWait(filename);

Press Enter

SendKeys.SendWait(@"{Enter}");

The PlUpload window will close so we switch back to the browser window (in this case Firefox)

hWnd = getOpenWindows["Mozilla Firefox"];
SetForegroundWindow(hWnd);

There are a couple of issues with this as the window titles are different depending on which browser is being used, so this will need to be taken into account for a complete solution. In addition, when this section of code is executing do not bring any other windows to the foreground as this window will receive the 'SendKeys' rather than the required window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top