Getting started with WPF UI Automation
To get started, let's use a simple application and demo some basic
automation scenarios like button click and set text. Then we will expand
into more advanced topics and scenarios. Let's take the following
simple application that contains a button and a text box.
When you click the button, it simply changes "Push the button" to "Hello world!" in the text box. The application looks like this:
To automate the application above to set the text, click the button and
then verify the hello text in Telerik Testing Framework, we can simply
write the following code using the WPF Extension (which resides
under ArtOfTest.WebAii.Wpf). The template for this method is based on a VsUnit test. However, the code itself can be reused in other type of Unit tests - it's just the template that will be different.
2: public void wpfDemo()
4: // Launch the application instance from its location in file system
5: WpfApplication wpfApp = Manager.LaunchNewApplication(@"C:\Projects\WpfApplication2\bin\Debug\WpfApplication2.exe");
7: // Validate the title of the main window
10: //Get the button
11: Button b = wpfApp.MainWindow.Find.ByName<Button>("button1");
13: //Click the button
16: //Get the text box
17: TextBox tb = wpfApp.MainWindow.Find.ByName<TextBox>("textBlock1");
19: //Verify the text
20: Assert.IsTrue(tb.Text.Equals("Hello World!"));
1: <TestMethod> _
2: Public Sub wpfDemo()
3: ' Launch the application instance from its location in file system
4: Dim wpfApp As WpfApplication = Manager.LaunchNewApplication("C:\Projects\WpfApplication2\bin\Debug\WpfApplication2.exe")
6: ' Validate the title of the main window
9: 'Get the button
10: Dim b As Button = wpfApp.MainWindow.Find.ByName(Of Button)("button1")
12: 'Click the button
15: 'Get the text box
16: Dim tb As TextBox = wpfApp.MainWindow.Find.ByName(Of TextBox)("textBlock1")
18: 'Verify the text
19: Assert.IsTrue(tb.Text.Equals("Hello World!"))
21: End Sub
Let's take a closer look at the code above line by line:
This is basic code that launches the application from a location on the local machine and initializes
it into a variable for us. As soon as you have an instance of a Wpf App,you now have full access to the
application including the entire Logical Tree.
We use the standard Assert class to check the name of the MainWindow. The MainWindow is a member of the generic WpfApplication class since every application has one and it holds its entire content.
The MainWindow offers a short-cut member 'Find' which contains search-related methods. In the above sample, given that the Button has a name associated with it, we can use Find.ByName to locate it. The MainWindow class offers two versions of Find.ByName:
- The first is a generic method that can return any Framework element.
- The second version allows you to specify the specific type of Framework Element you're looking for and will only return element of the specified type.
We'll be using the specific version.
We are using the 'User' object to click the button. The User object is present on all elements that inherit from FrameworkElement and offers real user interactions with WPF elements. So a click using the 'User' object will actually move the mouse over that button and click it. That is exactly what this line does.
Similarly to 9-12 we initialize the Texbox from the app into a variable after locating it by name.
Now that the button is clicked, we need to verify that the message is correct.
Now that we understand this sample, let's dig into some of the key objects available in the WPF Extension.