Synchronization
The Test Studio Testing framework has many features implemented to aid you in synchronizing with your application as it's being tested. There are six basic types of waits implemented in the framework that you can use for synchronization:
| Wait Type | Description |
|---|---|
|
ForExists | This method waits for the element to exist in the Visual Tree. It accepts an optional timeout parameter. |
|
ForExistsNot | This method waits for the element to no longer exist in the Visual Tree. It accepts an optional timeout parameter |
|
ForVisible | This method waits for the element to both exist in the Visual Tree and its Visibility attribute to equal "Visible". It accepts an optional timeout parameter. |
|
ForVisibleNot | This method waits for the Visibility property of the element to not equal 'Visible' or for the element to no longer exist in the Visual Tree. It accepts an optional timeout parameter. |
|
ForNoMotion | This method waits for the element to stop moving on the drawing surface. It takes a check interval and an optional timeout parameter. |
|
For(Predicate) | This method takes a custom predicate and waits for that predicate to return true. It accepts an optional error message and an optional timeout parameter. |
Waiting for an Element to Exist
Before you can use the VisualWait.ForExists method to wait for an element to exist you need to create what is called an "Element Proxy". An element proxy is a lightweight FrameworkElement that doesn't actually represent a real element but contains information on how to find the desired element in the Visual Tree. To create an element proxy implement code like this:
app.Find.Strategy = FindStrategy.WhenNotVisibleReturnElementProxy;
FrameworkElement myElementProxy = app.Find.ByName("MyElement");Now that you have an element proxy you use it to wait for the element to exist using this line of code:
// Wait 15 seconds for the element to exist
proxy.Wait.ForExists(15000);Using ForExistsNot, ForVisible, ForVisibleNot, ForNoMotion
Using any of these functions is pretty straightforward. Just call the method with appropriate parameters, for example:
FrameworkElement ticket = app.FindName("airlineTicket");
ticket.Wait.ForExistsNot(); // Wait for the element to no longer exist
ticket.Wait.ForVisible(3000); // Wait up to 3 seconds for the element to become visible
ticket.Wait.ForVisibleNot(2500); // Wait up to 2.5 seconds for the element to no longer be visible
ticket.Wait.ForNoMotion(250, 4500); // Wait up to 4.5 seconds for the element to stop moving. Check every 1/4 second.Creating Your Own Wait Condition
If none of the built-in framework wait for methods meet your needs, the framework has the ability to use your own coded conditional predicate instead. This feature relieves you of the burden of having to fully implement your own custom wait loop.
You can implement your conditional predicate in a function or a lambda expression. Here is a sample of how to implement and use your own conditional predicate in a function:
FrameworkElement guideButton = app.FindName("guideButton");
guideButton.Wait.For(myComparator);
public bool myComparator(FrameworkElement elem)
{
return elem.Visibility == ArtOfTest.WebAii.Silverlight.UI.Visibility.Visible;
}To do the same thing in a lambda expression would look like this:
FrameworkElement guideButton = app.FindName("guideButton");
guideButton.Wait.For(new System.Predicate<FrameworkElement>((fe) => fe.Visibility == ArtOfTest.WebAii.Silverlight.UI.Visibility.Visible));