c# – 当应用程序通过UI自动化测试启动时,ContentControl不可见,但在用户启动应用程序时可以看到ContentControl

c# – 当应用程序通过UI自动化测试启动时,ContentControl不可见,但在用户启动应用程序时可以看到ContentControl,第1张

概述我们正在使用棱镜和 WPF来构建应用程序.最近我们开始使用UI自动化(UIA)来测试我们的应用程序.但是当我们运行UIA测试时,会发生一些奇怪的行为.这是简化的shell: <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> < 我们正在使用棱镜和 WPF来构建应用程序.最近我们开始使用UI自动化(UIA)来测试我们的应用程序.但是当我们运行UIA测试时,会发生一些奇怪的行为.这是简化的shell:
<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,以拒绝对控件的访问.要显示d出窗口,我们使用下一个方法:

//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自动化测试启动它时,ErrorPopup从控件树中消失.我们试图启动这样的应用程序:

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))有一些奇怪的行为.如果我们停止使用它,这将解决我们的问题.现在我们可以在PopupContentControl中看到ErrorVIEw和任何类型的视图,并且应用程序更新UIA元素树结构.但这不是一个答案 – “停止使用这个功能”!

在MainVIEwArea中,我们有一个ContentControl,它根据用户 *** 作更新内容,我们只能看到第一个加载的ContentControl到ContentControl.Content属性.这样执行:

IRegionManager regionManager = Container.Resolve<IRegionManager>();regionManager.RequestNavigate(Regionnames.MainContentRegion,this.Uri);

如果我们更改视图,则在UI自动化树中不会执行任何更新 – 第一个加载的视图将被替换.但是在视觉上我们观察到另一个视图,而WPFInspector显示它正确(它的显示不是UI自动化树),但是Inspect.exe – 不是.

另外我们建议窗口使用某种缓存是错误的 – 在UI automation客户端中缓存,我们必须明确地打开,但是我们不这样做.

解决方法 对不起,我错过了一些细节,那是答案的关键.我认为这不重要.无论如何.

我们从DevExpress控件库中使用Navbar进行WPF.事实证明,当Navbar存在时,动态创建的视图不会出现在UI自动化树中.当从窗口中删除它时,可以看到所有动态加载的视图. Navbar是什么 – 对我来说还是很重要的.

这个明亮的例子,看看发生了什么,如果Navbar在窗口中出现或不存在(需要DevExpress).

MainWindow.xaml:

<Window xmlns:dxn="http://schemas.devexpress.com/winfx/2008/xaml/navbar"        x:Class="Test.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        title="MainWindow" Height="350" WIDth="525"        >    <GrID name="ContentGrID">        <GrID.ColumnDeFinitions>            <ColumnDeFinition WIDth="auto"/>            <ColumnDeFinition/>        </GrID.ColumnDeFinitions>        <GrID.RowDeFinitions>            <RowDeFinition></RowDeFinition>            <RowDeFinition></RowDeFinition>        </GrID.RowDeFinitions>        <!--Comment Navbar to see dynamic control in UI automation tree-->        <dxn:NavbarControl name="asdasd">            <dxn:NavbarControl.Groups>                <dxn:NavbarGroup header="asdasdasdasd" />            </dxn:NavbarControl.Groups>        </dxn:NavbarControl>        <TextBox GrID.Column="1" name="Statictb" Text="static is visible in ui automation tree" />        <button GrID.Row="1" Content="Create controls" Height="25"  Click="button_Click"/>    </GrID></Window>

MainWindow.xaml.cs

public partial class MainWindow : Window{    public MainWindow()    {        InitializeComponent();    }    private voID button_Click(object sender,RoutedEventArgs e)    {        TextBox tb = new TextBox();        GrID.SetRow(tb,1);        GrID.SetColumn(tb,1);        tb.Text = "dynamic is not visible,if Navbar here...";        ContentGrID.Children.Add(tb);    }}

编辑

根据DevExpress answer在他们的支持网站:

After a peer is created,Listening of automation events may cause performance issues. We have decIDed to clear invocation Lists of automation events to resolve it. In your specific situation,you need to disabling clearing. To do it,please set the static DevExpress.Xpf.Core.ClearautomationEventsHelper.IsEnabled property to False in the Window constructor.

这解决了这个问题.

总结

以上是内存溢出为你收集整理的c# – 当应用程序通过UI自动化测试启动时,ContentControl不可见,但在用户启动应用程序时可以看到ContentControl全部内容,希望文章能够帮你解决c# – 当应用程序通过UI自动化测试启动时,ContentControl不可见,但在用户启动应用程序时可以看到ContentControl所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1262538.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存