COMException being thrown randomly

Apr 9, 2008 at 7:20 PM
Hi,
We're having a problem with a COM exception being thrown from time to time in several of our tests. Reading through the stack trace, it appears to be that the exception is thrown when trying to write something in the Debug logger.
This is happening while trying to get a hold of a RadioButton (in this particular stacktrace at least) that should be there by doing window.Get<RadioButton>(radioName)

Does this ring any bell on what the problem might be or how to solve it?

This is the stack trace:
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at MS.Internal.Automation.UiaCoreApi.CheckError(Int32 hr)
   at System.Windows.Automation.AutomationElement.GetCurrentPropertyValue(AutomationProperty property, Boolean ignoreDefaultValue)
   at System.Windows.Automation.AutomationElement.AutomationElementInformation.get_AutomationId()
   at Core.Debug.WriteDetail(StringBuilder stringBuilder, AutomationElement automationElement, String displayPadding)
   at Core.Debug.Details(StringBuilder stringBuilder, AutomationElement automationElement, String displayPadding)
   at Core.Debug.Details(AutomationElement automationElement)
   at Core.UIItems.UIItemContainer.Get(SearchCriteria searchCriteria)
   at Core.UIItems.UIItemContainer.Get[T](SearchCriteria searchCriteria)
   at Core.UIItems.UIItemContainer.Get[T](String primaryIdentification)
   at StockTraderRI.AcceptanceTests.AutomatedTests.ModuleFixtures.BuySellModuleFixture.PopulateBuySellPanelWithData(Order model) in c:\PRISM\TestBuilds\WorkingDir\Source\StockTraderRI\StockRI.Tests.AcceptanceTests\AutomatedTests\ModuleFixtures\BuySellModuleFixture\BuySellModuleFixtureCommon.cs:line 246
   at StockTraderRI.AcceptanceTests.AutomatedTests.ModuleFixtures.BuySellModuleFixture.SellMultipleStocksFromPositionTableByClickingSubmitAll() in c:\PRISM\TestBuilds\WorkingDir\Source\StockTraderRI\StockRI.Tests.AcceptanceTests\AutomatedTests\ModuleFixtures\BuySellModuleFixture\BuySellModuleFixtureFromPositionTable.cs:line 322
Coordinator
Apr 10, 2008 at 2:59 AM
When trying to get the radio button, you are getting error. White is trying to show all the UIItems which are present in that window. Unfortunately while doing that it is throwing another exception. This bit needs to be fixed.
If you have source code you can probably comment this line
throw new WhiteException(Debug.Details(automationElement), e);
in UIItemContainer class.
Then you would be able to see the real error, which most likely is that it couldn't find the RadioButton.

You have sent me the stacktrace, the message part of the exception is missing. Can you please send it to me.
Apr 10, 2008 at 4:47 PM
Hi, thanks for the reply.
Sorry, I guess I missed copying the message. Here it is:
Test method StockTraderRI.AcceptanceTests.AutomatedTests.ModuleFixtures.BuySellModuleFixture.SellStockFromPositionTableByClickingSubmit threw exception: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x80042002.

The thing is that this is happening randomly (and not only in this case with the radio button). Another thing to note is that the radio button should be there in the first place, so I'm not sure if it's a timing issue, or maybe the tree is broken somehow and that's why UI Automation is throwing an exception while White is trying to write debug info, or...

Thanks,
Julian
Coordinator
Apr 12, 2008 at 5:42 PM
Can you try using the source code and catch exception when doing Debug.Details to find real exception. The actual exception is getting hidden because of this
Apr 24, 2008 at 4:43 PM

viveksingh wrote:
Can you try using the source code and catch exception when doing Debug.Details to find real exception. The actual exception is getting hidden because of this


Sorry it's being so long, but I never really looked into this, because I cannot repro the problem anymore... apparently the machine that had this problem (the build server) had other problems with UI Automation, so they took it out.
Anyway, it's good to be aware that the Debug.Details could throw an exception, so it may be good to wrap it in a try-catch block.

Thanks for the help
Jun 11, 2008 at 11:32 PM

Hi,

As discussed earlier, I've modified the following line in UIItemContainer.cs file in and now I am logging the exception generated to the System event log. Here is what I've modified.

public virtual IUIItem Get(SearchCriteria searchCriteria)
{
    try
    {
        IUIItem uiItem = currentContainerItemFactory.Find(searchCriteria, windowSession);
        CustomUIItem customUIItem = uiItem as CustomUIItem;
        if (customUIItem != null) SetContainer(customUIItem);
        return uiItem;
    }
    catch (Exception e)
    {
        // throw new WhiteException(Debug.Details(automationElement), e); 
        System.Diagnostics.EventLog.WriteEntry("DebugDetails", Debug.Details(automationElement));
        System.Diagnostics.EventLog.WriteEntry("Level1Msg", e.Message); 
        if (e.InnerException != null)
        {
               System.Diagnostics.EventLog.WriteEntry("Level2Msg", e.InnerException.Message);
        }
        System.Diagnostics.EventLog.WriteEntry("Level1StackTrace", e.StackTrace.ToString());
        throw;
    }
}

Interestingly, even for successful test cases also, exceptions are generated but it doesn't impact the test cases. At one point of time, the test case is failing with a COM Exception and the stack trace shows that it generates from UIA. 

Stack Trace details
---------------------
  at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at MS.Internal.Automation.UiaCoreApi.CheckError(Int32 hr)
   at MS.Internal.Automation.UiaCoreApi.UiaFind(SafeNodeHandle hnode, UiaFindParams findParams, Condition findCondition, UiaCacheRequest request)
   at System.Windows.Automation.AutomationElement.Find(TreeScope scope, Condition condition, UiaCacheRequest request, Boolean findFirst, BackgroundWorker worker)
   at System.Windows.Automation.AutomationElement.FindFirst(TreeScope scope, Condition condition)
   at Core.AutomationElementSearch.AutomationElementFinder.Descendant(Condition condition)
   at Core.Factory.PrimaryUIItemFactory.Create(SearchCriteria searchCriteria, ActionListener actionListener)
   at Core.UIItems.Container.NonCachedContainerItemFactory.Find(SearchCriteria searchCriteria)
   at Core.UIItems.Container.ContainerItemFactory.Get(SearchCriteria searchCriteria, ActionListener uiItemActionListener)
   at Core.Sessions.NullWindowSession.Get(ContainerItemFactory containerItemFactory, SearchCriteria searchCriteria, ActionListener actionListener)
   at Core.UIItems.Container.CurrentContainerItemFactory.Find(SearchCriteria searchCriteria, WindowSession windowSession)
   at Core.UIItems.UIItemContainer.Get(SearchCriteria searchCriteria)

Also, there is no inner exception and Debug details generated at this level, that could throw some light.

What might be the root cause for this issue?

Thanks,
Arun

 

 

Coordinator
Jun 12, 2008 at 3:16 PM
Thanks for the information.
Can you please send the exception message as well? (there is only stacktrace here).
Jun 12, 2008 at 5:26 PM
 Sorry, I didn't give the exception details. Here it is

Exception from HRESULT: 0x80042002

Thanks,
Arun 

Jun 13, 2008 at 8:25 PM

We still have the COM Exception in our tests and when we log the exception details from UIItemContainer.cs to Event Viewer,
we found that the WHITE is throwing exception even for the successfull test methods.Looks like WHITE is hiding these exceptions.

Is this by Design?

Exception Message:
Index was out of range. Must be non-negative and less than the size of the collection.

Stack Trace:
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.ThrowHelper.ThrowArgumentOutOfRangeException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Core.UIItems.Finders.IndexCondition.Filter(UIItemCollection collection)
   at Core.UIItems.Finders.SearchCriteria.IndexedItem(UIItemCollection collection)
   at Core.Factory.PrimaryUIItemFactory.Create(SearchCriteria searchCriteria, ActionListener actionListener)
   at Core.UIItems.Container.NonCachedContainerItemFactory.Find(SearchCriteria searchCriteria)
   at Core.UIItems.Container.ContainerItemFactory.Get(SearchCriteria searchCriteria, ActionListener uiItemActionListener)
   at Core.Sessions.NullWindowSession.Get(ContainerItemFactory containerItemFactory, SearchCriteria searchCriteria, ActionListener actionListener)
   at Core.UIItems.Container.CurrentContainerItemFactory.Find(SearchCriteria searchCriteria, WindowSession windowSession)
   at Core.UIItems.UIItemContainer.Get(SearchCriteria searchCriteria)

Coordinator
Jun 13, 2008 at 9:11 PM
Prasad,
The exception that you mentioned would not allow your test to pass (also it is not ComException). This is happening because you are trying to search do Indexed search by specifying it in the search criteria. This is a bug and should be fixed in white to return null instead of exception.
Coordinator
Jun 13, 2008 at 9:16 PM
Arun,
Seems like problem in UIAutomation. But I would need to investigate your problem further.
Jun 13, 2008 at 10:22 PM

Vivek,
I agree that the exception I mentioned above is not related to COM Exception but we uncovered this exception when we were investigating the COM Exception.

To answer to your question, the exceptions doesn't bubble up to the test methods and the test method do pass.
The exception was captured in place of throwing WhiteException

copying the code again:

public virtual IUIItem Get(SearchCriteria searchCriteria)
{
    try
    {
        ...
    }
    catch (Exception e)
    {
        // throw new WhiteException(Debug.Details(automationElement), e); 
        System.Diagnostics.EventLog.WriteEntry("DebugDetails", Debug.Details(automationElement));
        System.Diagnostics.EventLog.WriteEntry("Level1Msg", e.Message); 
        if (e.InnerException != null)
        {
               System.Diagnostics.EventLog.WriteEntry("Level2Msg", e.InnerException.Message);
        }
        System.Diagnostics.EventLog.WriteEntry("Level1StackTrace", e.StackTrace.ToString());
        throw;
    }
Jun 30, 2009 at 6:11 AM

I'm getting this exception too (intermittently) 

There is some discussion here...

http://social.msdn.microsoft.com/Forums/en-US/windowsaccessibilityandautomation/thread/5ac8c19f-d2a4-4f9b-abe3-5003ba5f975e

 

In my case the exception is as follows...

{"Exception from HRESULT: 0x80042002"}

at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)

   at MS.Internal.Automation.UiaCoreApi.CheckError(Int32 hr)

   at MS.Internal.Automation.UiaCoreApi.UiaFind(SafeNodeHandle hnode, UiaFindParams findParams, Condition findCondition, UiaCacheRequest request)

   at System.Windows.Automation.AutomationElement.Find(TreeScope scope, Condition condition, UiaCacheRequest request, Boolean findFirst, BackgroundWorker worker)

   at System.Windows.Automation.AutomationElement.FindAll(TreeScope scope, Condition condition)

   at Core.AutomationElementSearch.AutomationElementFinder.Descendants(Condition condition) in C:\Projects\White\Core\AutomationElementSearch\AutomationElementFinder.cs:line 61

   at Core.Factory.PrimaryUIItemFactory.CreateAll(SearchCriteria searchCriteria, ActionListener actionListener) in C:\Projects\White\Core\Factory\PrimaryUIItemFactory.cs:line 101

   at Core.UIItems.Container.NonCachedContainerItemFactory.GetAll(SearchCriteria searchCriteria) in C:\Projects\White\Core\UIItems\Container\NonCachedContainerItemFactory.cs:line 30

   at Core.UIItems.Container.CurrentContainerItemFactory.FindAll(SearchCriteria criteria) in C:\Projects\White\Core\UIItems\Container\CurrentContainerItemFactory.cs:line 73

   at Core.UIItems.UIItemContainer.GetMultiple(SearchCriteria criteria) in C:\Projects\White\Core\UIItems\UIItemContainer.cs:line 140

...

I'm hoping it's a timing issue and all I have to do is sprinkle some "wait's" around the place to solve it. (I'm not too hopeful)

 Let me know if you get any insights.

 

 

Jul 2, 2009 at 7:11 AM

My simplest example of this is

 

[Test]
public void COMExceptionTest()
{
   var window = AutomationElement.RootElement.FindFirst(TreeScope.Children,
                                                                  new PropertyCondition(AutomationElement.NameProperty, "Window1"));

   for (int i = 0; i < 1000; i++)
   {
      var count = window.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, "complexControls")).Count;
      Assert.AreEqual(1, count);
   }
}

where there is a WPF app running with a title Window1 and a listview on it called complexControls.

I get:

{"Exception from HRESULT: 0x80042002"} 

 

   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
   at MS.Internal.Automation.UiaCoreApi.CheckError(Int32 hr)
   at MS.Internal.Automation.UiaCoreApi.UiaFind(SafeNodeHandle hnode, UiaFindParams findParams, Condition findCondition, UiaCacheRequest request)
   at System.Windows.Automation.AutomationElement.Find(TreeScope scope, Condition condition, UiaCacheRequest request, Boolean findFirst, BackgroundWorker worker)
   at System.Windows.Automation.AutomationElement.FindAll(TreeScope scope, Condition condition)
   at Executor.UnitTests.ControlPathTests.test_name() in C:\Projects\Executor\src\UnitTests\ControlPathTests.cs:line 87

Source : "mscorlib"
Anyone able to help with this? 

 

Coordinator
Jul 4, 2009 at 10:06 AM

I am not sure whether it would help, because I don't remember seeing this before.

Please check point 3 here http://white.codeplex.com/Wiki/View.aspx?title=Other%20questions

Jul 25, 2011 at 4:05 PM
Edited Jul 25, 2011 at 4:58 PM

I'm having this issue as well.   It happens after a new window is launched containing a DataGrid with search results.    I thought ReIniatializing would fix the problem, however it did not.   

 System.Runtime.InteropServices.COMException was unhandled
  Message=Exception from HRESULT: 0x80042002
  Source=mscorlib
  ErrorCode=-2147213310
  StackTrace:
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
       at MS.Internal.Automation.UiaCoreApi.CheckError(Int32 hr)
       at MS.Internal.Automation.UiaCoreApi.UiaFind(SafeNodeHandle hnode, UiaFindParams findParams, Condition findCondition, UiaCacheRequest request)
       at System.Windows.Automation.AutomationElement.Find(TreeScope scope, Condition condition, UiaCacheRequest request, Boolean findFirst, BackgroundWorker worker)
       at System.Windows.Automation.AutomationElement.FindAll(TreeScope scope, Condition condition)
       at White.Core.AutomationElementSearch.DescendantFinder.Descendants(AutomationSearchCondition automationSearchCondition) in c:\white\Core\AutomationElementSearch\DescendantFinder.cs:line 28
       at White.Core.AutomationElementSearch.AutomationElementFinder.Descendants(AutomationSearchCondition automationSearchCondition) in c:\white\Core\AutomationElementSearch\AutomationElementFinder.cs:line 62
       at White.Core.Factory.PrimaryUIItemFactory.CreateAll(SearchCriteria searchCriteria, ActionListener actionListener) in c:\white\Core\Factory\PrimaryUIItemFactory.cs:line 83
       at White.Core.UIItems.Container.NonCachedContainerItemFactory.GetAll(SearchCriteria searchCriteria) in c:\white\Core\UIItems\Container\NonCachedContainerItemFactory.cs:line 30
       at White.Core.UIItems.Container.CurrentContainerItemFactory.FindAll(SearchCriteria criteria) in c:\white\Core\UIItems\Container\CurrentContainerItemFactory.cs:line 73
       at White.Core.UIItems.UIItemContainer.GetMultiple(SearchCriteria criteria) in c:\white\Core\UIItems\UIItemContainer.cs:line 180
       at FunctionalTest.UI.UI01_Auto(Config CF, SilverlightDocument sl) in C:\Ellipsis_Functional_Test\FunctionalTest\UI.cs:line 65
       at FunctionalTest.UI.executeui(Config CF, IE watin) in C:\Ellipsis_Functional_Test\FunctionalTest\UI.cs:line 100
       at FunctionalTest.SmokeTestController.Main(String[] args) in C:\Ellipsis_Functional_Test\FunctionalTest\SmokeTestController.cs:line 275
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: