简体   繁体   中英

Microsoft UI Automation for Web Application

Has anyone tried "Microsoft UI Automation" for web application?

I have a WPF application which has a embedded wpfbrowser. Since this is basically a desktop app, I cant use Selenium Webdriver.

I tried CodedUI but i am facing a issue, Which i have asked here: Coded UI - Unable to identify a html controls on a Wpfbrowser

I am planning to use UIAutomation, But again itseems that i am unable to identify a control using id property

Ex:

<button id="but1">Click Me</button>

For this i have:

PropertyCondition ps = new PropertyCondition(AutomationElement.AutomationIdProperty, "but1");
AutomationElement Clickme = elementMainWindow.FindFirst(TreeScope.Descendants, ps);

But this is not working. "Clickme" is null.

How to do this is UIAutomation??

EDIT: Attaching a screeshot: 在此处输入图片说明

I would try actually navigating the tree view down to the control you are looking for instead of doing it based on decedents. Also another thing you could try is doing a retry loop if it is null. Here is an example of a generic Retry for FlaUI. So your code would look something like this.

 PropertyCondition ps = new PropertyCondition(AutomationElement.AutomationIdProperty, "but1");

 Func<AutomationElement> func = () => elementMainWindow.FindFirst(TreeScope.Descendants, ps);
 Predicate<AutomationElement> retry = element => element == null;

 AutomationElement clickMe = Retry.While<AutomationElement>(func, retry, TimeSpan.FromSeconds(1));

So this code will retry finding the element for 1 second and will retry finding it if the element comes back null or it exceptions. If either of those happens it waits 200 milliseconds and tries again. This will tell me if the elements are just not rendered when you try to find them or if their is a difference between how inspect finds them and how System.Windows.Automation is finding them.

If this doesn't work I will post a solution using the tree walker but I suggest using this solution over the tree walker because if this was an application others would want to write automation against they would expect these functions to work the way you are attempting to use them.

Not sure if <button id="but1"> equals with automationId. You can set automation id using AutomationProperties.AutomationId="but1" if you can use that namespace in the code where you define your UI (XAML), which is probaly only for WPF applications.

In your case if your UI defined in HTML I think you can use the button's caption. So something like this.

var ps = new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
                new PropertyCondition(AutomationElement.NameProperty, "Click Me"));

AutomationElement Clickme = elementMainWindow.FindFirst(TreeScope.Descendants, ps);

ControlTypeProperty can help in filtering results by type. Not mandatory, but it can help if you have automation elements with different type, but with same name property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM