问题
In our Page Object Model for Page
we initialize a Container
in the constructor and we define a HtmlEdit
called MyTextBox
and a method that uses this text box to search.
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;
}
}
Here we want to use a UITestControl
as container so that we can search within a specific area of the page. The FindHtmlEditById
extension method in the getter finds the element by its html id as follows:
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;
}
}
So FindHtmlEditById
searches for an element with a certain id within the scope of the Container
.
When we execute the code and executions arrives at pasting text into MyTextBox
we get the following error:
MyTextBox.CopyPastedText = 'MyTextBox.CopyPastedText' threw an exception of type 'System.NotSupportedException'
Furthermore, the ControlType
of MyTextBox
is Window
as can be seen here:
When the type of the Container
field in the the Page
is changed to BrowserWindow
as follows:
private readonly BrowserWindow _container;
public BrowserWindow Container
{
get { return _container; }
}
MyTextBox
is properly recognized as a HtmlEdit
:
BrowserWindow
inherits from UITestControl
. Why then does the Container
needs to be specified as a BrowserWindow
to work? Why does it not work as a UITestControl
?
回答1:
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.
回答2:
BrowserWindow
does not inherit from UITestControl
, but is its own class under UITesting
namespace. As such, it will have its own properties, methods, etc. Create your object as a BrowserWindow object to solve your exception.
However, to answer your more general inheritance question, even if BrowserWindow
inherited from UITestControl
, when dealing with inheritance: While the child will inherit all properties and methods from the parent, the parent will not have any of the methods that are created or overridden in its children. Thus, BrowserWindow
can have a method .FindMyHtmlById()
, but that wouldn't be available if your object was a UITestControl
type.
来源:https://stackoverflow.com/questions/31658799/codedui-not-recognizing-htmlcontrol-when-searched-within-scope-of-uitestcontrol