Share via


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 -

 [ClassInitialize]
public static void MyClassInitialize(TestContext context)
{
    Playback.Initialize(); 
    try
    {
        // Your one time per class initialization code goes here
    }
    finally
    {
        Playback.Cleanup(); 
    }
}

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

  • Anonymous
    March 03, 2011
    i have got all the UI elements using automaton namespace. It gives me the error if i am doing the below mentioned thing. Playback.Initialize();                        Mouse.Click(new System.Drawing.Point((int)UIelement.Current.BoundingRectangle.X,                                                              (int)UIelement.Current.BoundingRectangle.Y));                        Playback.Cleanup(); It gives me error at "Playback" line. The error is mentioned below: Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Please let me know how to correct this error.

  • Anonymous
    March 06, 2011
    The comment has been removed

  • Anonymous
    March 07, 2011
    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))                {                    try                    {                        Playback.Initialize();                        Mouse.Click(new System.Drawing.Point((int)element.Current.BoundingRectangle.X,                                                              (int)element.Current.BoundingRectangle.Y));                        Playback.Cleanup();                    }                    catch (UITestException ex)                    {                        MessageBox.Show(ex.Message + "n" + ex.Source);                    }                }            } Please help in resolving the issue.

  • Anonymous
    March 17, 2011
    It is difficult to say without looking at the complete solution.  Please post your question along with the source code at social.msdn.microsoft.com/.../threads

  • Anonymous
    May 05, 2011
    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?

  • Anonymous
    May 09, 2011
    Is the test class marked as CodedUITest (or just TestClass)?  If the class is marked as CodedUITest, the framework should be calling these methods.

  • Anonymous
    August 31, 2011
    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 social.msdn.microsoft.com/.../d9d0b942-22b3-4444-a19d-c414755db912   Can u please clarify?

  • Anonymous
    September 04, 2011
    @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.

  • Anonymous
    September 04, 2011
    The comment has been removed

  • Anonymous
    September 04, 2011
    Difficult to say what is wrong in your setup.  Please post at social.msdn.microsoft.com/.../threads with details and someone should be able to help you.

  • Anonymous
    October 04, 2011
    Hi, 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? thanks.

  • Anonymous
    October 04, 2011
    Refer my blog - blogs.msdn.com/.../faq-running-coded-ui-test-on-a-machine-without-vs.aspx

  • Anonymous
    November 28, 2011
    The comment has been removed

  • Anonymous
    November 29, 2011
    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.

  • Anonymous
    March 08, 2012
    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?

  • Anonymous
    March 11, 2012
    See if you can run your test as MTA using - msdn.microsoft.com/.../ms404663.aspx