1

First off, the technologies being used here include: Visual Studio, C#, Selenium WebDriver, Google Chrome, and MS Excel.

I'm in the process of building an automation framework to test multiple web applications. I'm using an Excel spreadsheet to pull variable data in so that other testers have a much easier time using the tests. During our smoke test, we run through several of the Unit Tests that I've built in order. Therefor, I figured it would be a good idea to create an Ordered Test. When running each test individually, they work just fine. However, once I start running the Ordered Tests, the first test in the list will run, but then I get this error:

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

I create the references to my Excel spreadsheet in a class called ReadFile:

static Excel.Application xlApp = new Excel.Application();
static Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"SPREADSHEET LOCATION");
static Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
static Excel.Range xlRange = xlWorksheet.UsedRange;

When my tests pass or fail, they call the CleanUp method, which consists of quitting the chromedriver and calling the ReadFile.Close() method:

xlWorkbook.Save();

xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);

xlApp.Quit();

GC.Collect();
GC.WaitForPendingFinalizers();

I've done quite a bit of research and found that there may be need to invoke Mashall.FinalReleaseComObject. I've tried using this as well, but it doesn't seem to make a difference. I have also noticed that the Excel process continues to run for a few seconds after the first test finishes. So I tried sleeping the thread to make it wait for Excel to completely close before the next test starts, but it also immediately throws the exception when Excel is removed (even if the sleep timer hasn't completed).

Anyways, at this point I'm looking for any guidance possible and it is greatly appreciated. Please keep in mind that I'm not an expert with any of these technologies.

3 Answers3

0

Are you running/testing this in debug mode? If so, the GC is overlooking the COM object and it is not being released properly. That's about as well as I understand it. I found this solution as an answer in another SE thread recently. If I can find the thread I'll link it. The solution to this is to run in release mode and see if the issue persists.

Edit: Here is the thread explaining why debug mode causes GC to overlook some things.

OsakaRhymes
  • 96
  • 1
  • 8
  • @OsakaRyhmes Thanks for the reply! I tried running the framework in release mode, but the COM issues were still persisting. By switching to the EPPlus NuGet, I was able to resolve the issues. – Majesticles Nov 13 '17 at 15:10
0

Other than that, there are now good open source libraries (nuget packages) you coud use to read excel files, almost the same way you do with excel interop, but without excel, thus avoiding all possibe COM hurdles. Take a look at EPPlus or ExcelDataReader for example.

Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • Thanks for this answer! I rewrote the read/write class in my framework to use the EPPLus NuGet and it instantly resolved the COM issues I was having. – Majesticles Nov 13 '17 at 15:09
0

This exception:

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

as well as:

System.Runtime.InteropServices.InvalidComObjectException: Attempt has been made to use a COM object that does not have a backing class factory.

Can be caused by releasing the COM object before you are done using it. So, check and double check your calls to Marshal.ReleaseComObject and make sure you are not trying to access the object after the call.

Note: I don't this was the OP's problem, but this may help others who arrive here looking for information about these exceptions.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78