Tutorial

In the following tutorial a WPF-UserControl is used to create persons. After entering data in some fields the person is saved. The created persons are shown in a data-grid.
These examples are used out of a MSTest environment. So the used attributes for the methods may differ for other test-suites.

You can download a sample-project with this and some further code here.

Start a WPF UserControl directly

We want to test a User-Control without having a previously built application.

First we have to start our UserControl. For that purpose we use the class "CustomControl" of Guia. This is done in the initialize-method of our test-case:

[TestInitialize()]
public void TestInitialize()
{
    // Our DataContext for the User-Control
    viewModel = new PersonUserControlViewModel();

    // Start our User-Control
    control = CustomControl.Start<PersonUserControl>((c) => c.DataContext = viewModel);
}

As you can see, starting a User-Control with Guia is quite easy. As parameter we can pass any Action. In this case the lamda expression causes that our View-Model is set as DataContext in the User-Control. Alternatively we could also pass any parameters which are then routed to the constructor of our User-Control.

As we have started the User-Control, we also need to stop it once. This is done in our cleanup-method of our test-case:

[TestCleanup()]
public void TestCleanup()
{
    // Stop the running CustomControl
    control.Stop();

    // Make sure all objects can be cleaned up
    control = null;
    viewModel = null;
}

Test the User-Control

In the following test-case we use Guia to automate creating a person:

[TestMethod]
public void TestAddPerson()
{
    // Enter data in the textfields
    control.Get<TextBox>("txtFirstname").Value = "John";
    control.Get<TextBox>("txtName").Value = "Miller";
    control.Get<ComboBox>("cmbNationality").GetItem("Switzerland").Select();

    // Simulate a click on the "Add" button
    control.Get<Button>("btnAdd").Invoke();

    // Make assertions that the created person is shown in the data-grid
    DataGrid_WpfToolkit dataGrid = control.Get<DataGrid_WpfToolkit>("dataGrid");
    Assert.AreEqual("Miller", dataGrid[0, 0].TextValue);
    Assert.AreEqual("John", dataGrid[0, 1].TextValue);
    Assert.AreEqual("Switzerland", dataGrid[0, 2].TextValue);
}

This very simple example shows you the scope of Guia:
  • Searching for UI-Elements (through "Get" method)
  • Changing of control-specific properties
  • Executing of control-specific actions
  • Reading of control-specific properties

Start a WPF Window directly

As you can start a WPF User-Control directly, Guia enables you to start also a WPF Window directly:

[TestInitialize()]
public void TestInitialize()
{
    // Our view-model which we use in our Window
    viewModel = new PersonUserControlViewModel();

    // Start our Window
    windowControl = WindowControl.Start<PersonWindow>(viewModel);
}

Starting of a WPF Window is done with the class "WindowControl" of Guia. In this case, we pass our view-model as a parameter for the constructor of our particular Window. We also need to stop the WindowControl:

[TestCleanup()]
public void MyTestCleanup()
{
    // Stop the running WindowControl
    windowControl.Stop();

    // Make sure all objects can be cleaned up
    windowControl = null;
    viewModel = null;
}

In our tests we can use here the windowControl to execute the user-actions.

Start an entire application

If you don't want to test on the component-level but to test an entire application (e.g. for an acceptance-test), you may use the "Application" class:

[TestInitialize()]
public void MyTestInitialize()
{
    // Start the application
    application = Application.Start("PersonDemoApplication.exe");

    // Get the desired window of the application
    window = application.GetWindowByName("PersonWindow");
}

As you can see, we pass the path of our application to the start-method. After creating the application, we have to catch our window to be tested. Like the other test-methods, we also need to stop the application:

[TestCleanup()]
public void MyTestCleanup()
{
    // Stop the application
    application.Stop();

    // Make sure all objects can be cleaned up
    window = null;
}

The user-actions can be done with the window-variable.

Last edited Jul 1, 2010 at 8:55 PM by Ghostinus, version 7

Comments

No comments yet.