2008-10-30
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 7 of 8 )
The final test method breaks with the synchronous, self-contained nature of unit tests by introducing events. The UI Automation framework supplies events related to the control patterns, like InvokedEvent or WindowOpenedEvent, as well as more general events such as focus or property changes. To demonstrate how events can be used, we will watch for our application to be closed through the WindowClosedEvent.
First, we will need a class variable for our event handler. Next, within the test method, we call the static Automation.AddAutomationEventHandler. With this method, we inform the UI Automation framework that we are listening for the Window Closed event for our application and specify our event handler. Finally, we close the application using the Window pattern.
The event handler receives the generic AutomationEventArgs object that contains the identifier for what event occurred. Once we know that the Window Closed event occurred, we cast the event arguments to a WindowClosedEventArgs object. This specific object provides the RuntimeId for the window that was just closed. Additional code verifies that the RuntimeId matches the one for our application and that the RuntimeId does not match the RuntimeIds for any child windows of the desktop.
Finally, we need to unsubscribe from any automation events once our tests have completed. After each test completes running, I call Automation.RemoveAllEventHandlers in the TestCleanup method. Other removal methods allow for specifying specific events and automation elements; since our application is closed at this point, RemoveAllEventHandlers is the safest method.
private AutomationEventHandler UIAutomationEventHandler;
[TestMethod()]
public void CloseApplicationTest()
{
// Listen for the WindowClosed event on the application window
Automation.AddAutomationEventHandler(
WindowPattern.WindowClosedEvent,
AppElement, TreeScope.Element,
UIAutomationEventHandler = new AutomationEventHandler(
OnUIAutomationEvent));
// Use the Window pattern to close the application window
WindowPattern windowPattern = GetPattern(AppElement,
WindowPattern.Pattern) as WindowPattern;
windowPattern.Close();
}
private void OnUIAutomationEvent(object sender, AutomationEventArgs e)
{
// Verify that the WindowClosed event occurred
if (e.EventId == WindowPattern.WindowClosedEvent)
{
// Cast the AutomationEventArgs paramater to WindowClosedEventArgs
// now that we know it is the WindowClosed event
WindowClosedEventArgs args = e as WindowClosedEventArgs;
// Validate the RuntimeId for the window that
// closed against the RuntimeId
// for the application we have been testing
Assert.AreEqual<int[]>(AppRuntimeId, args.GetRuntimeId());
// Retrieve all children of the desktop
AutomationElementCollection windows =
AutomationElement.RootElement.FindAll(TreeScope.Children,
Condition.TrueCondition);
// Verify that none of the desktop children have the same RuntimeId
// as the application we have been testing and just closed
foreach (AutomationElement window in windows)
{
if (Automation.Compare(args.GetRuntimeId(),
(int[])window.GetCurrentPropertyValue(
AutomationElement.RuntimeIdProperty)))
{
Assert.Fail();
}
}
}
else
Assert.Fail();
}
[TestCleanup()]
public void TestCleanup()
{
// Unsubscribe from all event handlers now
// that the automation test is complete
Automation.RemoveAllEventHandlers();
}
![]() |
|


