问题
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.
Specifically, I have a DataGrid (from the WPFToolkit), which is databound to a List<MyBusinessObject>
. Each row therefore has a DataContext
of a MyBusinessObject
I can get the grid using white by finding it with it's automation ID.
I can then do this:
var row = Grid.Rows[0]
row.AutomationElement.Current.Name
which returns the string "MyCompany.Namespace.MyBusinessObject"
, so I'm almost there, but I can't actually get the actual business object itself.
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.
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?
回答1:
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.
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.
回答2:
ItemStatus (Attached Property)
This property enables a client to ascertain whether an element is conveying status about an item. For example, an item associated with a contact in a messaging application might be "Busy" or "Connected".
You cannot retrieve the underlying class, you can only get UI stuff. But, the Automation Peer for your business class can expose an Item Status. You could put a simple string in there or even XML.
来源:https://stackoverflow.com/questions/1309315/how-can-i-get-a-controls-datacontext-from-an-external-application-using-ui-auto