Sie sind auf Seite 1von 12

Introduction to Coded UI Test Overview

Coded UI tests are a new capability of Visual Studio 2010 which provides a way to create fully automated tests to validate the functionality and behaviour of your applications user interface. The coded UI test performs actions on the user interface controls for an application and verifies that the correct controls are displayed with the correct values. Creating a coded UI test generates a UIMap object that is specific to your test and represents the windows, controls, parameters, and assertions that are in the UI or that you created during the test recording. You can then perform actions on these UI objects to automate your user interface. For example, you can have your test method click a hyperlink in a Web application, type a value in a text box, or branch off and take different testing actions based on a value in a field. Each coded UI test is a test method in a coded UI test class. You can add multiple test methods to each coded UI test class and identify each coded UI test method by using the TestMethodAttribute. Your test method can also add validation code for a UI test control to obtain the value of a property of a UI test control. The test method can use an Assert statement to compare the actual value of the property to an expected value. The result of this comparison determines the outcome of the test. Every time that you run a coded UI test, you can analyze the test result and if the test fails, you can see or store the details of which assertion failed. When you create a coded UI test, these files are added to your test project:

File
CodedUITest1.cs UIMap.uitest UIMap.Designer.cs UIMap.cs

Description
Contains the coded UI test class, test methods and assertions. Contains the XML model for the UIMap class, including all windows, controls, properties, methods, parameters, actions, and assertions. Contains the code representation of the XML contained in the UIMap.uitest file. Do not edit this file. Contains more of the code for the UIMap class. You can put any customizations for the UI map in this file.

These assemblies are added as references to your test project:


Microsoft.VisualStudio.QualityTools.CodedUITestFramework Microsoft.VisualStudio.QualityTools.UnitTestFramework Microsoft.VisualStudio.TestTools.UITest.Common Microsoft.VisualStudio.TestTools.UITest.Extension Microsoft.VisualStudio.TestTools.UITesting

Architecture Overview

Introduction to Code Generation from Action Recordings


1. Open Microsoft Visual Studio from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010. 2. Start a new testing project (File | New | Project). 3. In the New Project window, select the Test Project template from Visual C# | Test and select the OK button to create the test project.

4. Close the window showing UnitTest1.cs. 5. In Solution Explorer, right-click on TestProject1 and select Add | Coded UI Test from the context menu.

6. There are two ways to generate code for this new coded UI test. The first and default option is to use the Coded UI Test Builder, which allows you to generate test code by manually walking through a test scenario. The second option is to use an existing action recording. Select the first option to click the OK button to continue.

7. Coded UI Test Builder tool will pop at the right hand bottom corner.

8. Click on Record and Play button the red button. 9. Now go to the Start -> IE Browser - > type the complete url of the site you want to navigate. 10. Let the page load and then perform all the action which you want record. 11. Once done click on the Pause button

12. Click on Generate Code control, the last control in the tool. Give some name to the method and click on Generate

Code is being generated now.

Once the code is generated you are good to run the testcase and see the result.

13. Right-click somewhere within the CodedUITest1.cs source file and select Run Tests. Important: do not touch the mouse or keyboard during the tests.

14. As the tests run, an instance of Internet Explorer will be opened and actions automatically taken as they are defined in the coded UI test. 15. Verify that the test passed by viewing the Test Results window. In this case, however, we are not performing any validation after any of the steps.

Steps to add Asserts to the code


1. To add assert drag and drop the crosshair control onto the textbox whose value need to be evaluated.
For example open the site http://glo.fr.msn.com once the page load drag the crosshair pin on to the bing search box highlighted in below screenshot

2. Select the Text property and click the Add Assertion button. This will load a dialog to finalize the assertion
options to use.

3. Verify that the Comparator to use is AreEqual and that the comparison value is Rechercher dans Glo. Select the OK button to continue.

Selecting the comparator type and value 4. Verify that a checkbox has been added to the Text property row in the Coded UI Test Builder window.

Text property showing assertion checkbox

5. Click on the Generate Code button in the Coded UI Test Builder tool window.

Generate Code button location 6. In the Generate Code window, use ValueEqualsSearch for the Method Name and select the Add and Generate
button to generate the validation code.

Generating assertion code


7. Close the Coded UI Test Builder. 8. Close the Internet Explorer window and return to Visual Studio 9. Similarly, multiple asserts can be added.

Kinds of Asserts
the Microsoft.VisualStudio.TestTools.UnitTesting namespace provides several kinds of Assert classes: Assert In your test method, you can call any number of methods of the Assert class, such as Assert.AreEqual(). The Assert class has many methods to choose from, and many of those methods have several overloads. CollectionAssert Use the CollectionAssert class to compare collections of objects, and to verify the state of one or more collections. StringAssert Use the StringAssert class to compare strings. This class contains a variety of useful methods such as StringAssert.Contains, StringAssert.Matches, and StringAssert.StartsWith. AssertFailedException The AssertFailedException exception is thrown whenever a test fails. A test fails if it times out, throws an unexpected exception, or contains an Assert statement that produces a Failed result. AssertInconclusiveException The AssertInconclusiveException is thrown whenever a test produces a result of Inconclusive. Typically, you add an Assert.Inconclusive statement to a test that you are still working on to indicate it is not yet ready to be run. UnitTestAssertException If you write a new Assert exception class, having that class inherit from the base class UnitTestAssertException makes it easier to identify the exception as an assertion failure instead of an unexpected exception thrown from your test or production code. ExpectedExceptionAttribute Decorate a test method with the ExpectedExceptionAttribute attribute when you want the test method to verify that an exception you expect to be thrown by a method in your development code is indeed being thrown in that method.

The Assert type exposes the following members.


Methods
Name
AreEqual(Object, Object)

Description
Verifies that two specified objects are equal. The assertion fails if the objects are not equal.

AreEqual(Double, Double, Double)

Verifies that two specified doubles are equal, or within the specified accuracy of each other. The assertion fails if they are not within the specified accuracy of each other.

AreEqual(Object, Object, String)

Verifies that two specified objects are equal. The assertion fails if the objects are not equal. Displays a message if the assertion fails.

AreEqual(Single, Single, Single)

Verifies that two specified singles are equal, or within the specified accuracy of each other. The assertion fails if they are not within the specified accuracy of each other.

AreEqual(String, String, Boolean)

Verifies that two specified strings are equal, ignoring case or not as specified. The assertion fails if they are not equal.

AreEqual(Double, Double, Double, String)

Verifies that two specified doubles are equal, or within the specified accuracy of each other. The assertion fails if they are not within the specified accuracy of each other. Displays a message if the assertion fails.

AreEqual(Object, Object, String, Object())

Verifies that two specified objects are equal. The assertion fails if the objects are not equal. Displays a message if the assertion fails, and applies the specified formatting to it.

AreEqual(Single, Single, Single, String)

Verifies that two specified singles are equal, or within the specified accuracy of each other. The assertion fails if they are not within the specified accuracy of each other. Displays a message if the assertion fails.

AreEqual(String, String, Boolean, CultureInfo)

Verifies that two specified strings are equal, ignoring case or not as specified, and using the culture info specified. The assertion fails if they are not equal.

AreEqual(String, String, Boolean, String)

Verifies that two specified strings are equal, ignoring case or not as specified. The assertion fails if they are not equal. Displays a message if the assertion fails.

AreEqual(Double, Double, Double, String, Object())

Verifies that two specified doubles are equal, or within the specified accuracy of each other. The assertion fails if they are not within the specified accuracy of each other. Displays a message if the assertion fails, and applies the specified formatting to it.

AreEqual(Single, Single, Single,

Verifies that two specified singles are equal, or within the specified

String, Object())

accuracy of each other. The assertion fails if they are not within the specified accuracy of each other. Displays a message if the assertion fails, and applies the specified formatting to it.

AreEqual(String, String, Boolean, CultureInfo, String)

Verifies that two specified strings are equal, ignoring case or not as specified, and using the culture info specified. The assertion fails if they are not equal. Displays a message if the assertion fails.

AreEqual(String, String, Boolean, String, Object())

Verifies that two specified strings are equal, ignoring case or not as specified. The assertion fails if they are not equal. Displays a message if the assertion fails, and applies the specified formatting to it.

AreEqual(String, String, Boolean, CultureInfo, String, Object())

Verifies that two specified strings are equal, ignoring case or not as specified, and using the culture info specified. The assertion fails if they are not equal. Displays a message if the assertion fails, and applies the specified formatting to it.

AreEqual(Of T)(T, T)

Verifies that two specified generic type data are equal by using the equality operator. The assertion fails if they are not equal.

AreEqual(Of T)(T, T, String)

Verifies that two specified generic type data are equal by using the equality operator. The assertion fails if they are not equal. Displays a message if the assertion fails.

AreEqual(Of T)(T, T, String, Object()) Verifies that two specified generic type data are equal by using the equality operator. The assertion fails if they are not equal. Displays a message if the assertion fails, and applies the specified formatting to it. AreNotEqual(Object, Object) Verifies that two specified objects are not equal. The assertion fails if the objects are equal. AreNotEqual(Double, Double, Double) Verifies that two specified doubles are not equal, and not within the specified accuracy of each other. The assertion fails if they are equal or within the specified accuracy of each other. AreNotEqual(Object, Object, String) Verifies that two specified objects are not equal. The assertion fails if the objects are equal. Displays a message if the assertion fails. AreNotEqual(Single, Single, Single) Verifies that two specified singles are not equal, and not within the specified accuracy of each other. The assertion fails if they are equal or within the specified accuracy of each other. AreNotEqual(String, String, Boolean) Verifies that two specified strings are not equal, ignoring case or not as specified. The assertion fails if they are equal. AreNotEqual(Double, Double, Double, String) Verifies that two specified doubles are not equal, and not within the specified accuracy of each other. The assertion fails if they are equal or within the specified accuracy of each other. Displays a message if the assertion fails.

AreNotEqual(Object, Object, String, Object())

Verifies that two specified objects are not equal. The assertion fails if the objects are equal. Displays a message if the assertion fails, and applies the specified formatting to it.

AreNotEqual(Single, Single, Single, String)

Verifies that two specified singles are not equal, and not within the specified accuracy of each other. The assertion fails if they are equal or within the specified accuracy of each other. Displays a message if the assertion fails.

AreNotEqual(String, String, Boolean, CultureInfo)

Verifies that two specified strings are not equal, ignoring case or not as specified, and using the culture info specified. The assertion fails if they are equal.

AreNotEqual(String, String, Boolean, String)

Verifies that two specified strings are not equal, ignoring case or not as specified. The assertion fails if they are equal. Displays a message if the assertion fails.

AreNotEqual(Double, Double, Double, String, Object())

Verifies that two specified doubles are not equal, and not within the specified accuracy of each other. The assertion fails if they are equal or within the specified accuracy of each other. Displays a message if the assertion fails, and applies the specified formatting to it.

AreNotEqual(Single, Single, Single, String, Object())

Verifies that two specified singles are not equal, and not within the specified accuracy of each other. The assertion fails if they are equal or within the specified accuracy of each other. Displays a message if the assertion fails, and applies the specified formatting to it.

AreNotEqual(String, String, Boolean, CultureInfo, String)

Verifies that two specified strings are not equal, ignoring case or not as specified, and using the culture info specified. The assertion fails if they are equal. Displays a message if the assertion fails.

AreNotEqual(String, String, Boolean, String, Object())

Verifies that two specified strings are not equal, ignoring case or not as specified. The assertion fails if they are equal. Displays a message if the assertion fails, and applies the specified formatting to it.

AreNotEqual(String, String, Boolean, CultureInfo, String, Object())

Verifies that two specified strings are not equal, ignoring case or not as specified, and using the culture info specified. The assertion fails if they are equal. Displays a message if the assertion fails, and applies the specified formatting to it.

AreNotEqual(Of T)(T, T)

Verifies that two specified generic type data are not equal. The assertion fails if they are equal.

AreNotEqual(Of T)(T, T, String)

Verifies that two specified generic type data are not equal. The assertion fails if they are equal. Displays a message if the assertion fails.

AreNotEqual(Of T)(T, T, String, Object())

Verifies that two specified generic type data are not equal. The assertion fails if they are equal. Displays a message if the assertion fails, and applies the specified formatting to it.

AreNotSame(Object, Object)

Verifies that two specified object variables refer to different objects. The assertion fails if they refer to the same object.

AreNotSame(Object, Object, String)

Verifies that two specified object variables refer to different objects. The assertion fails if they refer to the same object. Displays a message if the assertion fails.

AreNotSame(Object, Object, String, Object())

Verifies that two specified object variables refer to different objects. The assertion fails if they refer to the same object. Displays a message if the assertion fails, and applies the specified formatting to it.

AreSame(Object, Object)

Verifies that two specified object variables refer to the same object. The assertion fails if they refer to different objects.

AreSame(Object, Object, String)

Verifies that two specified object variables refer to the same object. The assertion fails if they refer to different objects. Displays a message if the assertion fails.

AreSame(Object, Object, String, Object())

Verifies that two specified object variables refer to the same object. The assertion fails if they refer to different objects. Displays a message if the assertion fails, and applies the specified formatting to it.

Equals Fail Fail(String) Fail(String, Object())

Determines whether two objects are equal. Fails the assertion without checking any conditions. Fails the assertion without checking any conditions. Displays a message. Fails the assertion without checking any conditions. Displays a message, and applies the specified formatting to it.

Inconclusive Inconclusive(String) Inconclusive(String, Object())

Indicates that the assertion cannot be verified. Indicates that the assertion cannot be verified. Displays a message. Indicates that an assertion cannot be verified. Displays a message, and applies the specified formatting to it.

IsFalse(Boolean)

Verifies that the specified condition is false. The assertion fails if the condition is true.

IsFalse(Boolean, String)

Verifies that the specified condition is false. The assertion fails if the condition is true. Displays a message if the assertion fails.

IsFalse(Boolean, String, Object())

Verifies that the specified condition is false. The assertion fails if the condition is true. Displays a message if the assertion fails, and applies the specified formatting to it.

IsInstanceOfType(Object, Type)

Verifies that the specified object is an instance of the specified type. The assertion fails if the type is not found in the inheritance hierarchy of the object.

IsInstanceOfType(Object, Type, String)

Verifies that the specified object is an instance of the specified type. The assertion fails if the type is not found in the inheritance hierarchy of the object. Displays a message if the assertion fails.

IsInstanceOfType(Object, Type, String, Object())

Verifies that the specified object is an instance of the specified type. The assertion fails if the type is not found in the inheritance hierarchy of the object. Displays a message if the assertion fails, and applies the specified formatting to it.

IsNotInstanceOfType(Object, Type)

Verifies that the specified object is not an instance of the specified type. The assertion fails if the type is found in the inheritance hierarchy of the object.

IsNotInstanceOfType(Object, Type, String)

Verifies that the specified object is not an instance of the specified type. The assertion fails if the type is found in the inheritance hierarchy of the object. Displays a message if the assertion fails.

IsNotInstanceOfType(Object, Type, String, Object())

Verifies that the specified object is not an instance of the specified type. The assertion fails if the type is found in the inheritance hierarchy of the object. Displays a message if the assertion fails, and applies the specified formatting to it.

IsNotNull(Object)

Verifies that the specified object is not Nothing. The assertion fails if it is Nothing.

IsNotNull(Object, String)

Verifies that the specified object is not Nothing. The assertion fails if it is Nothing. Displays a message if the assertion fails.

IsNotNull(Object, String, Object())

Verifies that the specified object is not Nothing. The assertion fails if it is Nothing. Displays a message if the assertion fails, and applies the specified formatting to it.

IsNull(Object)

Verifies that the specified object is Nothing. The assertion fails if it is not Nothing.

IsNull(Object, String)

Verifies that the specified object is Nothing. The assertion fails if it is not Nothing. Displays a message if the assertion fails.

IsNull(Object, String, Object())

Verifies that the specified object is Nothing. The assertion fails if it is not Nothing. Displays a message if the assertion fails, and applies the specified formatting to it.

IsTrue(Boolean)

Verifies that the specified condition is true. The assertion fails if the condition is false.

IsTrue(Boolean, String)

Verifies that the specified condition is true. The assertion fails if the condition is false. Displays a message if the assertion fails.

IsTrue(Boolean, String, Object())

Verifies that the specified condition is true. The assertion fails if the condition is false. Displays a message if the assertion fails, and applies

the specified formatting to it. ReplaceNullChars In a string, replaces null characters ('\0') with "\\0".

Best Practices for Coded UI Automation.


Use the following guidelines to create a flexible coded UI test.

Use the Coded UI Test Builder whenever possible. Do not modify the UIMap.designer.cs file directly. If you do this, the changes to the file will be overwritten. Create your test as a sequence of recorded methods. For more information about how to record a method, see How to: Create a Coded UI Test. Each recorded method should act on a single page, form, or dialog box. Create a new test method for each new page, form, or dialog box. When you create a method, use a meaningful method name instead of the default name. A meaningful name helps identify the purpose of the method. When possible, limit each recorded method to fewer than 10 actions. This modular approach makes it easier to replace a method if the UI changes. Create each assertion using the Coded UI Test Builder, which automatically adds an assertion method to the UIMap.Designer.cs file. If the user interface (UI) changes, re-record the test methods, or the assertion methods, or re-record the affected sections of an existing test method. Create a separate UIMap file for each module in your application under test. For more information, see Testing a Large Application with Multiple UI Maps. In the application under test, use meaningful names when you create the UI controls. This gives more meaning and usability to the automatically generated control names. If you are creating assertions by coding with the API, create a method for each assertion in the part of the UIMap class that is in the UIMap.cs file. Call this method from your test method to execute the assertion. If you are directly coding with the API, use the properties and methods in the classes generated in the UIMap.Designer.cs file in your code as much as you can. These classes will make your work easier, more reliable, and will help you be more productive.

Coded UI tests automatically adapt to many changes in the user interface. If, for example, a UI element has changed position or color, most of the time the coded UI test will still find the correct element. During a test run, the UI controls are located by the testing framework by using a set of search properties which are applied to each control class in the definitions created by the Coded UI Test Builder in the UIMap.Designer.cs file. The search properties contain name-value pairs of property names and property values that can be used to identify the control, such as the FriendlyName, Name, and ControlType properties of the control. If the search properties are unchanged, the coded UI test will successfully find the control in the UI. If the search properties are changed, coded UI tests have a smart match algorithm which applies heuristics to find controls and windows in the UI. When the UI has changed, you might be able to modify the search properties of previously identified elements to make sure that they are found

Das könnte Ihnen auch gefallen