[UPDATED] Adding Timers to Coded UI tests running in Load Tests

 

I have uploaded a sample App and Coded UI test project for the app with the below code in it. This sample also shows a couple of examples of how to wait for events and how to randomly pick values from screen items. Enjoy and please send feedback.

By default, Visual Studio does not expose timers for collecting times for individual steps inside Coded UI tests. However, you can use the following trick to capture step timers for Coded UI tests WHEN they are executed as part of a load test. If you are not interested in running the Coded UI test under load, then create the load test with the settings to only have a single user and a single iteration.

First, let me explain the code. Visual Studio Unit tests expose a ContextTimer that can be used when the unit test is executed as a part of a load test. In Visual Studio 2010 and earlier, you could also use this when running the unit test by itself. However, the product group removed that feature in 2012 and above. The workaround is to check to see if the unit test is running in the context of a load test, and if so, then use the timers. For a Coded UI test, create a new instance of the context in the UIMap class, set the timers appropriately, then pass in a reference to the context from the CodedUI TestMethod.

Step 1

Add a TestContext object to the Coded UI’s main UIMap.Designer.cs file at the root of the “UIIMap” class:

        // ----- ADDED BY GEOFFGR ----

        publicTestContext TestContext

        {

            get

            {

                return testContextInstance;

            }

            set

            {

                testContextInstance = value;

            }

        }

        privateTestContext testContextInstance;

 

clip_image002

 

Step 2

Add a BeginTimer prior to the call you want to measure. Add an EndTimer after the call. If the call is asynchronous, you also need to add an appropriate “ControlReady” event.

            if (TestContext.Properties.Contains("$LoadTestUserContext")) //running as load test

                TestContext.BeginTimer("TestTimerButtonWithWait");

 

             //<CodedUI Call HERE>

 

            if (TestContext.Properties.Contains("$LoadTestUserContext")) //running as load test

                TestContext.EndTimer("TestTimerButtonWithWait");

 

clip_image004

 

Step 3

Add a line in the TestMethod to initialize the TestContext object you added in step 1.

    this.UIMap.TestContext = TestContext;

 

clip_image006

 

Step 4

Create a load test and set the appropriate properties to execute it only one time:

clip_image008

 

clip_image010

 

 

ForTestingCodedUi.zip