我在Windows 7 64位上使用WatiN 2.1.0(C#)和IE 9。
我的问题是调用此函数
ie.FileUpload(Find.ByName(someRegExp)).Set(fileName);
非常慢 我的意思是对象很好找到。此代码打开文件对话框,但它会在很长一段时间后开始输入文件名,大约3到5分钟,比WatiNs超时时间长得多。在此测试的其余部分正常工作之后。
这有什么办法吗?这个大延迟非常烦人,当有更多的文件上传测试用例时,它会显着延长测试持续时间。
答案 0 :(得分:2)
好的,这不是每个人说的Watin解决方案,但我们遇到了完全相同的问题;我们的文件浏览器测试耗费了大量的总测试时间,我通过将Watin用于文件上载来解决它,而是使用(可怕的)UIAutomation框架。
使用示例:
public CustomFileUpload FileUpload
{
get
{
return new CustomFileUpload(WebBrowser.Current.hWnd, "_Layout");
//return Document.FileUpload(Find.ByName("file"));
}
}
您必须在测试项目中向“UIAutomationClient”和“UIAutomationTypes”添加referefences。以下解决方案不是通用的,因此您可能需要调整它以满足您的需求。
public class CustomFileUpload
{
private readonly IntPtr _browserHandle;
private readonly string _tabHeader;
public CustomFileUpload(IntPtr browserHandle, string tabHeader)
{
_browserHandle = browserHandle;
_tabHeader = tabHeader;
}
public void Set(string filePath)
{
Automate(filePath);
}
private void Automate(string filePath)
{
AutomationElement browser = AutomationElement.FromHandle(_browserHandle);
AutomationElement tab = FindTab(browser, _tabHeader);
// IE10 adds the name (or value?) "Browse..." to the upload-button. Need to hack it :)
AutomationElement uploadButton = tab.FindFirst(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
new PropertyCondition(AutomationElement.NameProperty, "Browse..."))) ??
tab.FindFirst(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
new PropertyCondition(AutomationElement.NameProperty, "")));
ClickButton(uploadButton);
var openFileDialog = WaitUntilOpenFileDialogAvailable();
var valuePattern = FindFileNameTextBox(openFileDialog).GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
if (valuePattern == null)
throw new InvalidOperationException("Can't set the file path");
valuePattern.SetValue(filePath);
SetFocusToSomethingElse(browser);
var okButton = WaitUntilOkButtonLoaded(openFileDialog);
ClickButton(okButton);
}
private static AutomationElement FindTab(AutomationElement browser, string tabHeader)
{
return browser.FindFirst(TreeScope.Descendants,
new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane),
new PropertyCondition(AutomationElement.NameProperty, tabHeader)));
}
private static void SetFocusToSomethingElse(AutomationElement elementWhichShouldNotBeSelected)
{
do
{
foreach (AutomationElement element in AutomationElement.RootElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.IsKeyboardFocusableProperty, true)))
{
if (element != elementWhichShouldNotBeSelected)
{
element.SetFocus();
return;
}
}
} while (true);
}
private static AutomationElement WaitUntilOkButtonLoaded(AutomationElement openFileDialog)
{
AutomationElement okButton;
do
{
okButton = openFileDialog.FindFirst(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.IsContentElementProperty, true),
new PropertyCondition(AutomationElement.IsControlElementProperty, true),
new PropertyCondition(AutomationElement.NameProperty, "Open"),
new PropertyCondition(AutomationElement.IsInvokePatternAvailableProperty, true)
));
} while (okButton == null);
return okButton;
}
private static AutomationElement WaitUntilOpenFileDialogAvailable()
{
AutomationElement openFileDialog = null;
do
{
AutomationElement openFileDialogContainer = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "Alternate Modal Top Most"));
if (openFileDialogContainer != null)
openFileDialog = openFileDialogContainer.FindFirst(TreeScope.Children, Condition.TrueCondition);
} while (openFileDialog == null);
return openFileDialog;
}
private static void ClickButton(AutomationElement button)
{
var clickPattern = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
if (clickPattern == null)
throw new InvalidOperationException("Can't find the buttons click pattern");
clickPattern.Invoke();
}
private static AutomationElement FindFileNameTextBox(AutomationElement openFileDialog)
{
AutomationElement findElementToTypePathInto;
do
{
findElementToTypePathInto = openFileDialog.FindFirst(TreeScope.Descendants, new AndCondition(new PropertyCondition(AutomationElement.NameProperty, "File name:"), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
} while (findElementToTypePathInto == null);
return findElementToTypePathInto;
}
}
答案 1 :(得分:1)
我有一个稍微简单的解决方案(再次依靠自动化,但仅用于选择文件) 我在文件输入上使用.ClickNoWait(),这不会导致浏览器挂起。
然后我写了一个扩展方法来设置我想要选择的文件:
public static void UploadFile(this Browser browser, string uploadPath)
{
var trw = new TreeWalker(Condition.TrueCondition);
var mainWindowElement = trw.GetParent(AutomationElement.FromHandle(browser.hWnd));
// Wait for the dialog to open
Thread.Sleep(1000);
// Get the select dialog
var selectDialogElement = mainWindowElement.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Choose File to Upload"));
// Get the file name box and set the path
var selectTextElement = selectDialogElement.FindFirst(
TreeScope.Descendants,
new AndCondition(new PropertyCondition(AutomationElement.NameProperty, "File name:"),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
var selectValue = (ValuePattern)selectTextElement.GetCurrentPattern(ValuePattern.Pattern);
selectValue.SetValue(uploadPath);
// Get the open button and click it
var openButtonElement = selectDialogElement.FindFirst(TreeScope.Descendants,
new AndCondition(new PropertyCondition(AutomationElement.NameProperty, "Open"),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button)));
var openButtonClick = (InvokePattern)openButtonElement.GetCurrentPattern(InvokePattern.Pattern);
openButtonClick.Invoke();
}
示例用法:
var browser = (IE)Document.DomContainer;
Document.FileUpload(Find.BySelector("#FileUpload")).ClickNoWait();
browser.UploadFile("c:\\myfile.txt");
Document.Button(Find.BySelector("#submit")).Click();
UIAutomation将从调用UploadFile(文件路径)接管并找到对话窗口,并填写表单,就像用户那样。
答案 2 :(得分:0)
在WatiN开始在文件上传对话框中输入文件名之前,我也遇到了3到5分钟的延迟。
只要在IE中打开开发人员工具窗格,这似乎就发生在我身上。当它没有打开时,立即开始输入。
关于默认浏览器和类似弹出窗口的提示似乎也会导致延迟。