如何使用UI Automation(和/或White)从外部应用程序中获取控件的DataContext [英] How can I get a control's DataContext from an external application using UI Automation (and/or White)

查看:121
本文介绍了如何使用UI Automation(和/或White)从外部应用程序中获取控件的DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用白色来驱动WPF应用程序的用户界面,到目前为止效果很好

I'm using White to drive the UI of a WPF app, and it's worked well so far.

但是我到了要进入测试中的应用程序内部状态以检查某些条件的位置.

I'm at the point however, where I need to poke into the internal state of the app under test to check some conditions.

具体来说,我有一个DataGrid(来自 WPFToolkit ) ,该数据绑定到List<MyBusinessObject>.因此,每一行都有一个MyBusinessObject

Specifically, I have a DataGrid (from the WPFToolkit), which is databound to a List<MyBusinessObject>. Each row therefore has a DataContext of a MyBusinessObject

我可以通过找到带有自动化ID的白色来获得网格.

I can get the grid using white by finding it with it's automation ID.

然后我可以这样做:

var row = Grid.Rows[0]
row.AutomationElement.Current.Name

返回字符串"MyCompany.Namespace.MyBusinessObject",所以我几乎在这里 ,但是我实际上无法获得实际的业务对象本身.

which returns the string "MyCompany.Namespace.MyBusinessObject", so I'm almost there, but I can't actually get the actual business object itself.

我已经研究了WPF UIAutomation AutomationPeers(从AutomationPeer您可以获取Owner属性,这是实际的业务对象),但是似乎您需要在过程中使用它们,因为它们都有构造函数,需要您提供WPF控件.

I've looked into WPF UIAutomation AutomationPeers (from an AutomationPeer, you can get the Owner property, which is the actual business object), but it seems that you need to be in-process to use these, as they all have constructors which require you to supply the WPF control.

是否有任何方法可以从外部流程构造AutomationPeer,或者以其他方式深入了解WPF控件的实际DataContext?

Is there any way I can construct an AutomationPeer from an external process, or otherwise drill down into the actual DataContext of a WPF control?

推荐答案

我认为您可以.使用UI自动化交换信息的唯一方法是通过预定义的提供程序,它们集中在UI及其显示内容上,而不是在它后面的数据上:您永远不会在应用程序和测试应用程序之间交换实际的对象.

I dont think you can. The only way you can exchange information using UI Automation is through the predefined Providers, and they are focused on the UI and what it displays, not the data behind it: you never exchange actual objects between your app and your test app.

我认为使用UIA可以获得的最接近的方法是将BusinessObject或至少相关数据编码为字符串,然后使用ValuePattern检索该字符串.

I think the closest you can get, using UIA, is to encode your BusinessObject, or at least the relevant data, in a string and retrieve that string using the ValuePattern.

这篇关于如何使用UI Automation(和/或White)从外部应用程序中获取控件的DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆