When would you use the UITestControl.Find method?

2019-05-30 10:09发布

问题:

When checking whether a page/screen has loaded, I often use the UITestControl.WaitForControlExist() method, but I am finding example code that follows the ctl.WaitForControlExist() with otherCtl.Find() calls on the parent controls. Like this:

var tab = UIMainMenuExtWindow.UIItemWindow.UIRibbonClient.UISolutionsTabPage;
tab.WaitForControlExist(3000);
UIMainMenuExtWindow.Find();
UIMainMenuExtWindow.UIItemWindow.Find();
UIMainMenuExtWindow.UIItemWindow.UIRibbonClient.Find();
UIMainMenuExtWindow.UIItemWindow.UIRibbonClient.UISolutionsTabPage.Find();
tab.Find();
Mouse.Click(tab);

Does this code make sense? What is the purpose of the 'Find()' calls?

回答1:

After setting the SearchProperties and FilterProperties etc for a UI Control, the Find method causes the search to be performed. Commonly the Find is not called explicitly but it (or possibly some equivalent internal method) is called implicitly when the UI Control is evaluated in an expression as the parent of another control.

Consider:

this.uimap.uiTopLevel.ActionMethod();

In the above statement the value of uiTopLevel must be evaluated to find the object whose ActionMethod can be called. That evaluation requires the Find method.

The Find method may need to be called explicitly when an application replaces part of its display with another identical copy. The UI Controls, when first evaluated, get a reference to the original copy of the control. When the test tries to access the second version it may get a "control not found" or "hidden control" exception (forget the exact terminology of these exceptions). By re-evaluating the control, ie by calling the Find method explicitly, the new version of the control can be found.