在Page
的页面对象模型中,我们在构造函数中初始化Container
,我们定义了一个名为HtmlEdit
的{{1}}和一个使用此文本框进行搜索的方法
MyTextBox
这里我们要使用public class Page
{
private readonly UITestControl _container;
private HtmlEdit _myTextBox;
private const string MyTextBoxId = "MyTextBoxHtmlId";
protected Page()
{ }
protected Page(Process process)
{
CurrentBrowser = BrowserWindow.FromProcess(process);
_container = CurrentBrowser;
}
public UITestControl Container
{
get { return _container; }
}
protected HtmlEdit MyTextBox
{
get
{
if (_myTextBox == null)
{
_myTextBox = Container.FindHtmlEditById(MyTextBoxId);
}
return _myTextBox;
}
}
public Page SearchMethod(string accountId)
{
MyTextBox.CopyPastedText = accountId;
// Do the search
return this;
}
}
作为容器,以便我们可以在页面的特定区域内进行搜索。 getter中的UITestControl
扩展方法按其html id查找元素,如下所示:
FindHtmlEditById
因此public static class Extensions
{
public static HtmlEdit FindHtmlEditById(this UITestControl control, string id)
{
return new HtmlEdit(control).FindById(id);
}
public static TUIControl FindById<TUIControl>(this TUIControl control, string id)
where TUIControl : HtmlControl
{
control.SearchProperties.Add(HtmlControl.PropertyNames.Id, id,
PropertyExpressionOperator.Contains);
return control;
}
}
会在FindHtmlEditById
的范围内搜索具有特定ID的元素。
当我们执行代码并执行将文本粘贴到Container
时,我们会收到以下错误:
MyTextBox.CopyPastedText ='MyTextBox.CopyPastedText'引发类型'System.NotSupportedException'的异常
此外,MyTextBox
的{{1}}为ControlType
,如下所示:
当MyTextBox
中Window
字段的类型更改为Container
时,如下所示:
Page
BrowserWindow
被正确识别为private readonly BrowserWindow _container;
public BrowserWindow Container
{
get { return _container; }
}
:
MyTextBox
继承自HtmlEdit
。为什么BrowserWindow
需要指定为UITestControl
才能运作?为什么它不能用作Container
?
答案 0 :(得分:2)
Can you provide a zip with a repro so I can have a closer look? It is obvious from the screenshots that in the one case you get a Window control back in stead of the HtmlEdit you are searching for, but as the reason why I can not give a conclusive answer. There might be a bug in your code with the CurrentBrowser since that seems to be a construct that is not correct, but also can be a flaw in the pasted code only. I copied the code and tried to reproduce using the bing homepage as the page to past the text in, and that works like a charm. So I suspect the search results in something different in your target website. One thing that might be causing this is the contains operator you use on the search, since this could potentially result in multiple matches depending on the page you are conducting the search on. When it returns multiple controls your call wil also fail, but looking at the whatch you shoed you did not get multiple controls but a window with a windows class edit, which much more looks like a WinEdit control then a HtmlEdit control.
答案 1 :(得分:1)
BrowserWindow
不会从UITestControl
继承,而是在UITesting
namespace下自己的类。因此,它将拥有自己的属性,方法等。将对象创建为BrowserWindow对象以解决异常。
但是,要回答更一般的继承问题,即使BrowserWindow
继承自UITestControl
,在处理继承时:虽然子进程将从父进程继承所有属性和方法,但父进程不会拥有在其子级中创建或覆盖的任何方法。因此,BrowserWindow
可以使用方法.FindMyHtmlById()
,但如果您的对象是UITestControl
类型,则该方法无法使用。