Using Automation Peers to Manipulate UI Elements at Runtime

,

When performing code-driven UI testing, often you need to manipulate elements in ways that are not possible using the API of the elements themselves. The Button class, for example, does not have a PerformTap method to raise the Tap event. The built-in control classes are generally well encapsulated and are not designed to simulate interaction via code. For this we turn to the Microsoft UI Automation framework, which consists of a secondary API for manipulating UI elements from code. The Automation framework is designed for accessibility software, allowing third-party software to manipulate the UI on behalf of a user with a disability.

The Automation API is able to manipulate elements, such as a Button, using internal methods of the various built-in FrameworkElement types, which are ordinarily off-limits.

In this example, a button is placed on the ChatClientView page. When tapped, it sets the view’s custom ButtonClicked property to true. The XAML for the button is as follows:

<Button x:Name="button_AutomationTest"
        Content="AutomationTestButton"
        Click="button_AutomationTest_Click" />

The code-beside for the view contains the event handler for the Click event of the button:

public bool ButtonClicked { get; private set; }

void button_AutomationTest_Click(object sender, RoutedEventArgs e)
{
    ButtonClicked = true;
}

An AutomationPeer, located in the System.Windows.Automation.Peers namespace, is used to verify that the button was indeed clicked. The AutomationPeer object is then used to retrieve an IInvokeProvider specific to the Button class, which allows you to invoke the button’s Click event, shown in the following excerpt:

[TestMethod]
[Asynchronous]
public void DemonstrateButtonClick()
{
    AutomationPeer peer
              = FrameworkElementAutomationPeer.CreatePeerForElement(
                                            view.button_AutomationTest);
    IInvokeProvider provider
              = (IInvokeProvider)peer.GetPattern(PatternInterface.Invoke);
    provider.Invoke();

    EnqueueCallback(() => Assert.IsTrue(view.ButtonClicked));
    EnqueueTestComplete();
}

Behind the scenes, the AutomationPeer is calling an internal method of the ButtonBase named AutomationButtonBaseClick, which raises the button’s Click event.


Note

The ButtonAutomationPeer raises the button’s Click event and not its Tap event. Unfortunately, the Tap event is not supported by the Automation API in the current 8.0 release of the Windows Phone SDK.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.45.49