我正在尝试使用plupload queue小部件为上传设置测试。我正在使用Splinter进行浏览器内测试,但我找不到实现它的方法。Splinter有一些附加文件的方法,但仅当它是一个简单的文件字段时。另一种方法是单击按钮浏览文件,然后选择文件...但我认为使用Splinter (或selenium)是不可能的,不是吗?或者通过文件的拖放。
谁有任何建议的最好的方法来自动化这些测试?
发布于 2013-03-22 21:12:49
可以使用Selenium- WebDriver自动执行用户在PLUpload控件上执行的操作。请找到下面的WebDriver C#代码,它单击一个flash按钮对象并使用键盘事件选择一个文件。
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}");
}
}
}发布于 2014-07-29 18:09:22
我在使用PlUpload小部件时遇到了类似的问题。感谢CheryJose让我走上了正确的道路。
首先,我必须创建一个小类来找出哪些窗口是打开的,并将它们作为一个窗口字典返回。
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);在Selenium页面代码中,我单击按钮以启动PlUpload窗口,并等待片刻。(这在代码中没有显示)
然后,我使用下面的代码查找所有打开的窗口
IDictionary<string, IntPtr> getOpenWindows = GetOpenWindows();切换到PlUpload窗口(窗口的名称因浏览器而异。请注意!)
IntPtr hWnd = getOpenWindows["File Upload"];
SetForegroundWindow(hWnd);键入文件的路径
SendKeys.SendWait(filename);按Enter键
SendKeys.SendWait(@"{Enter}");PlUpload窗口将关闭,因此我们切换回浏览器窗口(在本例中为Firefox)
hWnd = getOpenWindows["Mozilla Firefox"];
SetForegroundWindow(hWnd);这有几个问题,因为窗口标题根据使用的浏览器不同而不同,因此需要考虑到这一点才能得到完整的解决方案。此外,在执行这段代码时,不要将任何其他窗口带到前台,因为此窗口将接收“SendKeys”,而不是所需的窗口。
https://stackoverflow.com/questions/15531728
复制相似问题