Ben
Ben

Reputation: 3912

Unit Tests in Visual Studio 2010

i am trying to create a Unit test for a WinForm in a Visual Studio 2010 project. I add a new "Coded UI Test" to my project, open up the code file, then right click and select "Generate Code for Coded UI Test" -> "Use Coded UI Test builder". I then start my application up, select "Record" on the UI Map control. I run my tests (in this case simply select a textbox, type in a random value, them click a button). I then select "Generate Code" from the UI Map control which generates the code which the test will use. When running this test, i get the error:

Test method HelloWorldTest.CodedUITest1.CodedUITestMethod1 threw exception: Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: The playback failed to find the control with the given search properties. Additional Details: TechnologyName: 'MSAA' ControlType: 'Window' Name: 'Form1' ClassName: 'WindowsForms10.Window' ---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.

Does anyone know where i am going wrong?

Thanks

Upvotes: 2

Views: 2433

Answers (1)

ShuManChu
ShuManChu

Reputation: 13

I believe your problem is with the window title. You see that Visual Studio will give a window title to each new window generated in your application. When you recorded your test, your window's title was "Form1". It says so in your error message:

ControlType: 'Window' Name: 'Form1'

So what you need to determine is if your application renames the window each time a new window is opened (most likely it does). For instance, if you open a second window, it might be called "Form2".

For the coded ui test to work properly, it needs to know where to look for certain controls to perform its actions on. So, it will need to know the window title to be able to "select a textbox, type in a random value, them click a button."

The only way I have found to fix this problem is to set the window title manually if i know it has changed, or will change.

this.UIMap.UIHighCapitalFranchiseWindow.UIHighCapitalFranchiseDocument.UICtl00ContentPlaceHolComboBox.WindowTitles.Clear();
this.UIMap.UIHighCapitalFranchiseWindow.UIHighCapitalFranchiseDocument.UICtl00ContentPlaceHolComboBox.WindowTitles.Add("Franchises for Sale");

As you can see, I am just clearing all the values in the WindowTitles property of the control, then adding the correct title, the one I know it should be. In the above example it is organized as follows:

this.(name of your uimap file).(name of your window).(name of the document in the window).(name of the certain control (combo box, radio button, text box etc)).WindowTitles.(clear or add)();

This code can go right in the coded ui test file, or if you want to use the partial class UIMap.cs (the one that is not auto generated, UIMap.Designer.cs is the auto generated one) to make changes to the UIMap. Just remember to remove the ".UIMap" if you choose the latter.

Upvotes: 1

Related Questions