Freigeben über


Test Script Generator Sample

This sample demonstrates how to use Microsoft UI Automation control patterns and events to generate test scripts for automated testing scenarios. The test script generator tracks user interaction with the user interface (UI) of a target application and stores the elements and events of interest.

Information about an element and any associated events is saved to a general record class (UIAEventRecord.cs). Both the general information and the script can be dumped as plain text for subsequent analysis or modification by a tester, and, in the case of the generated script, used for later playback.

An event-based application such as this has the inherent limitation of not being able to distinguish between changes in the user interface (UI) that are the result of direct or indirect user input. For example, the target application for this sample contains three interdependent controls where user interaction with one control causes changes to each of the other controls.

Caution noteCaution:

UI Automation and the automated test scenario addressed in this sample require a contract between the application, or control developer, and a tester. In other words, the ability to consistently execute a generated script is dependent on a static, predictable target UI to operate on. In this regard, UI Automation should not be considered a solution for scenarios where the recording and subsequent playback of interaction with random applications and controls is required.

Common issues that make "blind" recording and playback impractical arise when elements do not have unique identifier, such as an AutomationId property, or the UI is dynamic and unpredictable, such as the Windows Task Manager dialog where items listed on the "Processes" or "Applications" tabs are dependent on system state. Locating specific elements during playback is virtually impossible in these instances.

This sample demonstrates a specific feature of the Windows Presentation Foundation (WPF) and, consequently, does not follow application development best practices. For comprehensive coverage of Windows Presentation Foundation (WPF) and Microsoft .NET Framework application development best practices, refer to the following as appropriate:

Accessibility - Accessibility Best Practices

Security - WPF Security

Localization - WPF Globalization and Localization Overview

Download sample

Building the Sample

  • This sample is made up of the following three projects.

    • ScriptGeneratorClient provides the script generating functionality for the sample; the target application is started from the client UI.

    • ScriptGeneratorTarget provides the target UI for the client applications.

    • ExecuteScript provides the script execution functionality for the sample; the target application is started from the client UI. This project is run manually after a script has been generated and copied to the script.cs file.

  • Install the Windows Software Development Kit (SDK) and open its build environment command window. On the Start menu, point to All Programs, Microsoft Windows SDK, and then click CMD Shell.

  • Download the sample, usually from the software development kit (SDK) documentation, to your hard disk drive.

  • To build the sample from the build environment command window, go to the source directory of the sample. At the command prompt, type MSBUILD.

  • To build the sample in Microsoft Visual Studio 2005, load the sample solution or project file and then press CTRL+SHIFT+B.

Generating a Script

  • To run the compiled sample from the build environment command window, execute the .exe file in the Bin\Debug or Bin\Release folder contained under the sample source code folder.

  • To run the compiled sample with debugging in Visual Studio 2005, press F5.

NoteNote:

This will start the ScriptGenerator client application. The target application is started from the client.

Executing a Script

  • Close the client and target applications.

  • Open the Script.cs file in the ExecuteScript project.

  • Select the existing code and paste over it with the generated script.

  • Build and run the ExecuteScript project.

NoteNote:

This will start the ExecuteScript client application. The target application is started from the client.

See Also