How To: Get UITesting methods working outside the TestMethod of Coded UI Test

This is another questions that I have got asked multiple times –

How do I use the methods in Microsoft.VisualStudio.TeamTest.UITesting namespace outside the TestMethod of Coded UI Test?  For example, I want to do a one time initialization of my application settings in ClassInitialize method.  How can I do that?

The answer is to wrap the code with Playback.Initialize() and Playback.Cleanup() calls something like below –

public static void MyClassInitialize(TestContext context)
        // Your one time per class initialization code goes here

Note that Playback.Initialize() and Playback.Cleanup() calls are NOT needed for TestInitialize and TestCleanup methods where this is done implicitly by the Coded UI Test framework.

Comments (16)

  1. Nilesh says:

    i have got all the UI elements using automaton namespace. It gives me the error if i am doing the below mentioned thing.


                           Mouse.Click(new System.Drawing.Point((int)UIelement.Current.BoundingRectangle.X,



    It gives me error at "Playback" line.

    The error is mentioned below:

    Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.Extension, Version=,

    Please let me know how to correct this error.

  2. Please share the complete code.  The above line means that the framework is not able to load *.UITest.Extension.dll.  If the project is of type Coded UI Test, then it knows how to load.  Else you need to use tricks like AppDomain.AssemblyResolve (…/system.appdomain.assemblyresolve.aspx) to resolve the assembly.

  3. Nilesh says:

    I am doing the things outside Coded UI.

    //GetAllControls function get all the childs of window or application under test and create list of type AutomationElement

    ListOfControls = GetAllControls(allChildsOfApp);

    foreach (AutomationElement element in ListOfControls)


    //trying to click on the buttons found on the UI using Mouse.Click

                   if(element.Current.LocalizedControlType.Equals("button") && (element.Current.IsControlElement == true))





                           Mouse.Click(new System.Drawing.Point((int)element.Current.BoundingRectangle.X,




                       catch (UITestException ex)


                           MessageBox.Show(ex.Message + "n" + ex.Source);




    Please help in resolving the issue.

  4. It is difficult to say without looking at the complete solution.  Please post your question along with the source code at…/threads

  5. Bill says:

    My coded UI tests have been running fine. I added one more test and all of a sudden I'm getting the error you are describing with the built-in framework.

    Adding Playback.initialize with the TestMethod lets it work.

    Why has the built in framework stopped calling playback.initialize?

  6. Is the test class marked as CodedUITest (or just TestClass)?  If the class is marked as CodedUITest, the framework should be calling these methods.

  7. Rahul Aras says:

    Hi Gautam ,

    Please correct me if I am wrong but from what I understood from this post by your colleague , Siddhartha Pandey contradicts what you  mention here that Playback.Initialize and Playback.Cleanup are not needed in the TestInitialize and TestCleanup methods.

    Here's the link to the post…/d9d0b942-22b3-4444-a19d-c414755db912  

    Can u please clarify?

  8. @Rahul – Both SIddhartha and I am right.  If you are using CodedUITest as the class attribute, you don't need Playback.InitializeCleanup in TestInitialize method.  However if you are using TestClass as the class attribute, then you need to do this explicitly.

  9. Gautam,

    My code is similar to the sample you have shown above. It does work when I run it locally but does not when I use test controller on visual studio to run it on a remote machine installed with test agent. Playback.Initialize in Class Initializer throws an exception.

    <ErrorInfo> <Message>Class Initialization method Tests.Scenario1.Initialize threw exception. Microsoft.VisualStudio.TestTools.UITest.Extension.PlaybackFailureException: Microsoft.VisualStudio.TestTools.UITest.Extension.PlaybackFailureException: Cannot perform '' on the control. Additional Details: The control details were not specified. —> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component..</Message> <StackTrace> at Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.IRPFPlayback.SetSkipStepEventName(String skipStepEventName) at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.InitPlayback() at Microsoft.VisualStudio.TestTools.UITesting.Playback.Initialize() — End of inner exception stack trace — at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowComException(COMException innerException, IPlaybackContext context) at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, IPlaybackContext context) at Microsoft.VisualStudio.TestTools.UITesting.Playback.Initialize() at Tests.Scenario1.Initialize(TestContext testContext) in C:WorkspaceTestsScenario1.cs:line 101 </StackTrace> </ErrorInfo>

    Can you help please?

  10. Difficult to say what is wrong in your setup.  Please post at…/threads with details and someone should be able to help you.

  11. Satish says:


    Is it possible to run a standalone CodedUI test app with a .net 4 framework installed in a different server. When I search aorund in google, I see posts that, CodedUI has been not been de-coupled yet and will need the entire visual studio installed for this?


  12. According to the link below, it is possible to run a Coded UI test from a custom executable, for example a WPF app.…/running-codedui-test-from-another-application.aspx

    If the article is correct, is it possible to configure some execution parameters similar to what mstest.exe offers? In other words, I would like to run a Coded UI test from my own WPF application and I would like to specify the location of the results file (equivalent to mstest's "/resultsfile" parameter) and the location of the run configuration file (equivalent to mstest's "/runconfig" parameter). (please note that calling mstest.exe from my WPF app. is not an option; I need to execute the Coded UI test directly from my WPF app)

  13. No, the trx file is generated by the common runner which is a layer above Coded UI Test.  Using the above technique you can run Coded UI Test without trx.  If you need trx, you should invoke mstest.exe from your WPF app.

  14. Thomas Janik says:

    I am trying to run a threaded coded UI test … mainly because I have 200 or so objects on a web page that I want to concerrently pull the innertext data for in a parallel.foreach loop.  Sequentially it takles like 15 minutes cause each llop take 15 seconds or so. When threaded  I keep getting an error that says my apartmentstate can't be STA so I am assuming it won't let me access the browserwindow in a new thread.  Even if I try to identify the open browserwindow in the new thread and literally get everything new in the new thread, I still get the same error.  I am not trying to interact with IE, just get like object data.  Would doing the testing via the method above allow me to stay in MTA mode for threading?