Exceptions can be generated when you reuse code from a recorded code UI test

I recently assisted a customer who was having a problem with VS 2010's coded UI tests. The customer had recorded the web test using the recorder and then modified the generated code to complete his tests. In this particular instance, the customer copied and pasted the code to call certain portions of his test to simulate multiple entries. When he ran the coded UI test, he was greated with an exception:

Error Message:

Test method My.SmokeTest_Part1.SLA_New threw exception:

Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotAvailableException: The control is not available or not valid. Additional Details:

...

ClassName: 'Internet Explorer_TridentDlgFrame'

ControlType: 'Window'

---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.

What had happened was that the first use of the input button closed the window that contained all of the controls. Then the user created a new window and tried to reuse the same controls that were mapped to the now closed window. Since that window was closed, the exception was thrown. The workaround was to create a new instance of the UIMap's ShareResourcesPage class. Without this new mapping, the exception will be thrown every time.