我们使用prism和WPF来构建应用程序。最近我们开始使用UI自动化(UIA)来测试我们的应用程序。但是当我们运行UIA测试时发生了一些奇怪的行为。这里的简化壳:当应用程序通过UI自动化测试启动时ContentControl不可见,但当应用程序由用户启动时它是可见的
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0" Grid.Column="0"
Name="loadingProgressText"
VerticalAlignment="Center" HorizontalAlignment="Center"
Text="Loading, please wait..."/>
<Border
Grid.Row="0"
x:Name="MainViewArea">
<Grid>
...
</Grid>
</Border>
<!-- Popup -->
<ContentControl
x:Name="PopupContentControl"
Grid.Row="0"
prism:RegionManager.RegionName="PopupRegion"
Focusable="False">
</ContentControl>
<!-- ErrorPopup -->
<ContentControl
x:Name="ErrorContentControl"
Grid.Row="0"
prism:RegionManager.RegionName="ErrorRegion"
Focusable="False">
</ContentControl>
</Grid>
在我们的应用程序,我们使用图层(Popup
和ErrorPopup
)隐藏MainViewArea,拒绝访问的控制。为了显示Popup
,我们用下一个方法:
//In constructor of current ViewModel we store _popupRegion instance to the local variable:
_popupRegion = _regionManager.Regions["PopupRegion"];
//---
private readonly Stack<UserControl> _popups = new Stack<UserControl>();
public void ShowPopup(UserControl popup)
{
_popups.Push(popup);
_popupRegion.Add(PopupView);
_popupRegion.Activate(PopupView);
}
public UserControl PopupView
{
get
{
if (_popups.Any())
return _popups.Peek();
return null;
}
}
与此类似,我们展示了ErrorPopup
我们的应用程序中的所有元素:
// In constructor we store _errorRegion:
_errorRegion = _regionManager.Regions["ErrorRegion"]
// ---
private UserControl _error_popup;
public void ShowError(UserControl popup)
{
if (_error_popup == null)
{
_error_popup = popup;
_errorRegion.Add(_error_popup);
_errorRegion.Activate(_error_popup);
}
}
Mistics ...
当我们运行就像用户这样做(双击应用程序图标),我们可以看到两个自定义控件(使用AutomationElement.FindFirst
方法或通过Visual UI Automation Verify)。但是,当我们使用UI自动化测试启动它时,从控件的树中消失。我们试图启动这样的应用程序:
System.Diagnostics.Process.Start(pathToExeFile);
我认为我们错过了一些东西。但是什么?
编辑#1
正如@chrismead说,我们试图与UseShellExecute
标志设置为true运行我们的应用程序,但是这并没有帮助。但是,如果我们从cmd行启动应用程序,并且手动单击该按钮,Popup
和ErrorPopup
在自动化控件树中可见。
Thread appThread = new Thread(delegate()
{
_userAppProcess = new Process();
_userAppProcess.StartInfo.FileName = pathToExeFile;
_userAppProcess.StartInfo.WorkingDirectory = System.IO.Directory.GetCurrentDirectory();
_userAppProcess.StartInfo.UseShellExecute = true;
_userAppProcess.Start();
});
appThread.SetApartmentState(ApartmentState.STA);
appThread.Start();
我们的一个建议是,当我们使用方法FindAll
或FindFirst
搜索按钮,点击,窗口莫名其妙缓存的UI自动化状态,并且不更新它。
编辑#2 我们已经发现,棱镜库IRegionManager.RegisterViewWithRegion(RegionNames.OurRegion, typeof(Views.OurView))
的是扩展方法有一些奇怪的行为。如果我们停止使用它,这将特别解决我们的问题。现在我们可以看到ErrorView和PopupContentControl
中的任何一种视图,以及应用程序更新UIA元素树的结构。但这不是答案 - “请停止使用此功能”!
在MainViewArea
我们有一个ContentControl
,根据用户的操作,其更新它里面的内容,我们只能够看到第一个装UserControl
到ContentControl.Content
财产。这是这样进行的:
IRegionManager regionManager = Container.Resolve<IRegionManager>();
regionManager.RequestNavigate(RegionNames.MainContentRegion, this.Uri);
如果我们改变了看法,没有更新将在UI Automation树进行 - 第一加载视图将在它来代替。但在视觉上,我们观察到另一个View
,并且WPFInspector正确显示它(它不显示UI自动化树),但Inspect.exe - 不是。
另外我们的建议是,窗口使用某种缓存是错误的 - 在UI自动化客户端缓存,我们必须明确打开,但我们不这样做。
因此,说一个简单的双击启动应用程序会导致控件在树中,但是Process.Start启动不会呢? – chrismead
是的,它是正确的。但我们尝试了3种方式从代码启动应用程序 - 没有人让我们找到正确的解决方案... – stukselbax
您是否尝试从cmd窗口启动应用程序?如果可行,那么使用ProcessStartInfo.UseShellExecute标志可能会起作用。 – chrismead