无法对阻止控制异常执行操作?使用Coded UI?

Sur*_*har 12 coded-ui-tests

我正在自动化WPF应用程序,当我记录"WpfComboBox"控件并对该控件执行选择索引时,它会抛出错误,如"无法对阻塞控件异常执行操作".请帮我解决这个问题.

WpfControl customContr = new WpfControl(subDvnMap.SubDvsnItemCustom.SubDvsnItemTabList.SubDvsnPIPrismPrismExtensioTabPage);
customContr.SearchProperties.Add(WpfControl.PropertyNames.AutomationId, "legalFormatsControl");

WpfComboBox combLegal = new WpfComboBox(customContr);       
combLegal.SearchProperties.Add(WpfComboBox.PropertyNames.AutomationId, "legalFormats");
combLegal.Find();
combLegal.SelectedIndex = 2; 
Run Code Online (Sandbox Code Playgroud)

以上是我的代码,它在combLegal.selectedIndex = 2中抛出错误

在此输入图像描述

小智 3

此问题的原因:

该控件放置在一个不可见的控件内,该控件放置在父控件内。正如您的代码中一样, combLegal组合框位于customContrWpfcontrol 内部。但还有另一个控件会阻止您访问组合框。设计者一定在调试时将其用于其他目的,并且在完成后忘记将其删除。

可能的解决方案:


1. 尝试通过其父级访问不可见控件。

WpfControl customContr = new WpfControl(subDvnMap.SubDvsnItemCustom......);
customContr.SearchProperties.Add(....AutomationId, "legalFormatsControl");
foreach(WpfControl TempContr in customContr.GetChildren())
{
  WpfControl ChildContr = TempContr.GetChildren().ElementAt(0);
  if(ChildContr is WpfComboBox)
    {
     combLegal.SelectedIndex = 2;
     break;
    }
}
Run Code Online (Sandbox Code Playgroud)



2. 尝试通过检查其宽度来访问控件。

WpfControl customContr = new WpfControl(subDvnMap.SubDvsnItemCustom......);
customContr.SearchProperties.Add(....AutomationId, "legalFormatsControl");
foreach(WpfControl TempContr in customContr.GetChildren())
{
   if(TempContr.BoundingRectangle.Width>0)
      {
        combLegal.SelectedIndex = 2;
        break;
      }
}
Run Code Online (Sandbox Code Playgroud)



3. 尝试通过检查其父项的宽度来访问控件。

  WpfControl customContr = new WpfControl(subDvnMap.SubDvsnItemCustom......);
    customContr.SearchProperties.Add(....AutomationId, "legalFormatsControl");
    foreach(WpfControl TempContr in customContr.GetChildren())
    {
       if(TempContr.BoundingRectangle.Width>0)
         {
           WpfControlCollection Collection = TempContr.GetChildren();
           foreach(WpfControl combLegal in Collection)
             {
               if(combLegal is WpfComboBox)
                 {
                  combLegal.SelectedIndex = 2;
                   break;
                 }
             }
         }
    }
Run Code Online (Sandbox Code Playgroud)


其中之一应该可以解决您的问题。如果没有,请在下面评论,我们将进行更多尝试。祝你好运..!!