Friday, March 25, 2016

Coded UI Tests: Error Resolution “COM object that has been separated from its underlying RCW cannot be used”.

This error may come across a coded UI developer while executing test cases. This error is occurred if you are using a same static class across two test cases.
For example in my scenario, I have created a static class called "MainCall". In this class, I have put all the UIMAP class references. So that I can call any test case of any UIMAP from the "MainCall" class.
The class is as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SaadAutomation.Maps.UIMap1;
using SaadAutomation.Maps.UIMap2;
using SaadAutomation.Maps.UIMap3;
namespace SaadAutomation
{

class
MainCall
{

private static UIMap1 _spMap;
private static UIMap2 _cMap;
private static UIMap3 _crMap;

public static UIMap1 SPMap
{
get {
if (_spMap == null)
_spMap = new UIMap1();
return _spMap;
}
}

public static UIMap2 CMap
{
get{
if (_cMap == null)
_cMap = new UIMap2();
return _cMap;
}
}

public static UIMap3 CRMap
{
get{
if (_crMap == null)
_crMap = new UIMap3();
return _crMap;
}
}
}
}


I was calling functions like this in a test method.
[TestMethod]

public void ExampleTestCase1 ()
{
MainCall.SPMap.Method1();
MainCall.SPMap.Method2();
MainCall.CMap.Method1();
MainCall.CRMap.CloseAllWindows();
}
[TestMethod]

public
void ExampleTestCase2 ()
{
MainCall.CRMap.Method1();
MainCall.CMap.Method2();
MainCall.SPMap.Method1();
}
When I run both of the above test methods together, I got this error message.
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
So I searched on the internet and found the solution. It is just a setting change in the local.testsettings file.
Resolution:
To resolve this error, open your .testsettings file in any XML editor. This file is located under solution items.
Add this line (marked bold)
<Description>These are test settings for Trace and Test Impact.</Description>
<Execution>


Don't forget to restart visual studio after that. Now you will be able to run your test cases and this error will not come inshallah.
There is one side effect of adding this line. You will not be able to debug your test. See my another post about the mentioned issue.
What other errors you encounter? Post here as comments and we will try to find the solution together.

2 comments: