2014-09-22 44 views
1

我在等待一个控制这样启用:WaitForControlEnabled()抛出UITestControlNotFoundException

control.WaitForControlEnabled(60000); // timeout in 60 seconds 

然而,有时这种方法大约十秒钟就会抛出一个UITestControlNotFoundException

Test method MyTest threw exception: 
Automation Playback Engine was not able to find the button &Next > in - window &Next >. Additional Details: TechnologyName: 'MSAA' Name: 'Next >' ControlType: 'Button' 
Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException 
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapControlNotFoundException(COMException ex, IPlaybackContext context) 
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowComException(COMException innerException, IPlaybackContext context) 
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(Exception exception, IPlaybackContext context) 
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(Exception exception, String queryId) 
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId, Boolean expandUIElementWhileSearching, Int32 searchTime) 
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetElement(Boolean useCache, ISearchArgument searchArg) 
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.Search(ISearchArgument searchArg) 
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal() 
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindControlIfNecessary() 
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.WaitForControlReadyPrivate(Int32 millisecondsTimeout, Boolean doLogging) 
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.WaitForControlEnabledPrivate(Int32 millisecondsTimeout) 
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl+<>c__DisplayClass4a.<WaitForControlEnabled>b__49() 
    at Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod(Func`1 function, UITestControl control, Boolean firePlaybackErrorEvent, Boolean logAsAction) 
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.WaitForControlEnabled(Int32 millisecondsTimeout) 

    Error HRESULT E_FAIL has been returned from a call to a COM component. 
    System.Runtime.InteropServices.COMException 
     at Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.IScreenElement.FindAllDescendants(String bstrQueryId, Object& pvarResKeys, Int32 cResKeys, Int32 nMaxDepth) 
     at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindAllScreenElement(String queryId, Int32 depth, Boolean singleQueryId, Boolean throwException, Boolean resetSkipStep) 
     at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindScreenElement(String queryId, Int32 depth, Boolean resetSkipStep) 
     at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindFirstDescendant(String queryId, Boolean expandUIElementWhileSearching, Int32 searchTime) 

我问题是:

  • 为什么WaitForControlEnabled(60000)等待整个60秒?
  • 即使MSDN doc中没有提及UITestControlNotFoundException,它为什么会丢掉?

编辑:我用WaitForControlExist()直接WaitForControlEnabled()之前,但WaitForControlEnabled()仍然抛出同样的异常,这是没有意义的我:

control.WaitForControlExist(60000); 
control.WaitForControlEnabled(60000); 

如果WaitForControlExist()回报,那么这意味着UI元素被找到。但那么怎么能不能立刻发现?

回答

0

要等到您在WaitForControlEnabled()方法中指定的时间,控件的顶层必须存在或者实际控件必须存在或处于禁用状态,否则智能搜索引擎无法找到等待控件并抛出异常。如果控件不存在,可以尝试使用WaitForControlNotExist()方法等待控件存在,因为我的测试用例中也发现了相同的问题,这里是我等待控件的解决方案,即使是一小时的时间,直到它出现为止 退出或启用。

 bool iscontrolexist= uIwaitingButton.WaitForControlEnabled(); 

     while (!iscontrolexist) 
     { 
      try 
      { 
      uIwaitingButton.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch); 
      uIwaitingButton.SetFocus(); // setting focus for the on button 
      iscontrolexist= uIwaitingButton.WaitForControlExist(100); 
      } 
      catch (Exception ex) 
      { 
       // possibly exception for setfocus() method call 
       // handle exception, to write a log message     
      } 

     } 

     // perform the button click after find the control 
     // Click 'Save' button 
     Mouse.Click(uIwaitingButton, new Point(100,100)); 
相关问题