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
?