繁体   English   中英

Web应用程序的Microsoft UI自动化

[英]Microsoft UI Automation for Web Application

有没有人尝试过针对Web应用程序的“ Microsoft UI Automation”?

我有一个WPF应用程序,它具有嵌入式wpfbrowser。 由于这基本上是一个桌面应用程序,因此我无法使用Selenium Webdriver。

我尝试了CodedUI,但遇到了一个问题,在这里我已经问过: 编码UI-无法在Wpfbrowser上识别html控件

我打算使用UIAutomation,但再次似乎无法使用id属性标识控件

例如:

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

为此,我有:

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

但这是行不通的。 “ Clickme”为空。

UIAutomation该怎么做?

编辑:附加一个screeshot: 在此处输入图片说明

我会尝试实际将树形视图导航到您要查找的控件,而不是基于后代。 另外,您可以尝试做的另一件事是如果它为null,则执行重试循环。 这是FlaUI的通用重试示例。 因此,您的代码将如下所示。

 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));

因此,此代码将重试查找元素1秒钟,如果元素返回null或异常,则将重试查找该元素。 如果发生任何一种情况,它将等待200毫秒,然后重试。 这将告诉我在尝试查找元素时是否仅不呈现元素,或者它们是否是检修如何找到它们与System.Windows.Automation如何找到它们之间的区别。

如果这不起作用,我将发布一个使用Tree Walker的解决方案,但是我建议在Tree Walker上使用该解决方案,因为如果这是一个应用程序,其他人可能想针对其编写自动化程序,他们希望这些功能能够按照您尝试的方式工作使用它们。

不确定<button id="but1">等于automationId。 如果可以在定义UI(XAML)的代码中使用该名称空间,则可以使用AutomationProperties.AutomationId="but1"来设置自动化ID,这仅适用于WPF应用程序。

在您的情况下,如果您的UI是用HTML定义的,那么我认为您可以使用按钮的标题。 像这样

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

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

ControlTypeProperty可帮助按类型过滤结果。 不是强制性的,但是如果您具有类型不同但名称属性相同的自动化元素,则可以提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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