[英]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該怎么做?
我會嘗試實際將樹形視圖導航到您要查找的控件,而不是基於后代。 另外,您可以嘗試做的另一件事是如果它為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.