界面控件DevExpress WPF——支持UI自動化和Appium測試

下載DevExpress v20.2完整版 

DevExpress技術交流羣3:700924826      歡迎一起進羣討論

DevExpress WPF 擁有120+個控件和庫,將幫助您交付滿足甚至超出企業需求的高性能業務應用程序。通過DevExpress WPF能創建有着強大互動功能的XAML基礎應用程序,這些應用程序專注於當代客戶的需求和構建未來新一代支持觸摸的解決方案。

在v20.2版本中,技術團隊增強了對WPF產品線UI測試自動化的支持,UI自動化現在包括更全面的自動化測試功能:

  • DevExpress WPF控件形成與主題無關的AutomationPeer層次結構。
  • 您可以在自動化樹中搜索AutomationPeer屬性,生成和分配的XAML/代碼AutomationPeer屬性均可用於搜索。
  • AutomationPeers包括各種自動化模式,例如Invoke, ExpandCollapse, Selection, Scroll等。

您可以使用UIAutomationClient庫API創建自動測試,也可以使用基於UI自動化技術的任何UI測試庫。

DevExpress WPF控件包含UI測試模式選項,使用時會對應用程序進行以下更改:

  • 動畫被禁用。
  • 上下文菜單僅在單擊鼠標時激活,並且當鼠標指針懸停菜單項時不會打開。
  • 修改了UI自動化樹,以產生更穩定和可靠的測試。

注意:我們使用Appium WinAppDriver API測試了控件。

準備環境

  1. 啓用Windows Developer Mode
  2. 安裝WinAppDriver
  3. 下載WinAppDriver UI Recorder

創建測試

請按照以下步驟創建一個新的測試項目:

1. 打開Windows命令提示符,創建項目文件夾或導航到現有文件夾,然後使用以下命令:

  • dotnet new nunit --framework netcoreapp3.1 - 創建一個空的nunit測試項目。
  • dotnet add package Appium.WebDriver - 在您的項目中引用Appium.WebDriver包。

2. 在Visual Studio中打開nunit測試項目。

3. 創建一個DesktopSession類,該類使您可以使用WinAppDriver UI記錄器將生成的代碼。

您可以在下面看到是如何實施的:

 

public class DesktopSession {
const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723/";
WindowsDriver < WindowsElement > desktopSession;
public DesktopSession(WindowsDriver < WindowsElement > source) {
desktopSession = source;
}
public WindowsDriver < WindowsElement > DesktopSessionElement {
get {
return desktopSession;
}
}
public WindowsElement FindElementByAbsoluteXPath(string xPath, int nTryCount = 10) {
WindowsElement uiTarget = null;
var index = xPath.IndexOf(value: '/', startIndex: 1);
xPath = xPath.Substring(startIndex: index);
while (nTryCount-->0) {
try {
uiTarget = desktopSession.FindElementByXPath(xpath: $ "/{xPath}");
}
catch {
Console.WriteLine($@"Find failed: ""{xPath}""");
}
if (uiTarget != null) break;
Thread.Sleep(millisecondsTimeout: 100);
}
return uiTarget;
}
public IOptions Manage() {
return this.desktopSession.Manage();
}
public void CloseApp() {
this.desktopSession.CloseApp();
}
}

 

4. 將以下test fixture複製並粘貼到UnitTest1.cs文件中:

 

public class Tests {
Process pWad;
const string PathToTheDemo = @"C:\Users\Public\Documents\DevExpress Demos 20.2\Components\WPF\DevExpress.OutlookInspiredApp.Wpf\bin\DevExpress.OutlookInspiredApp.Wpf.exe";
protected DesktopSession desktopSession {
get;
private set;
}

[OneTimeSetUp]
public void FixtureSetup() {
StartWAD();
var options = new AppiumOptions();
options.AddAdditionalCapability(capabilityName: "app", capabilityValue: PathToTheDemo);
options.AddAdditionalCapability(capabilityName: "deviceName", capabilityValue: "WindowsPC");
options.AddAdditionalCapability(capabilityName: "platformName", capabilityValue: "Windows");
var driver = new WindowsDriver < WindowsElement > (new Uri("http://127.0.0.1:4723"), options);
desktopSession = new DesktopSession(driver);
WaitSplashScreen(driver);
}
static void WaitSplashScreen(WindowsDriver < WindowsElement > driver) {
var cwh = driver.CurrentWindowHandle;
while (driver.WindowHandles.Contains(cwh))
Thread.Sleep(1000);
driver.SwitchTo().Window(driver.WindowHandles[0]);
}
private void StartWAD() {
var psi = new ProcessStartInfo(@"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe");
psi.EnvironmentVariables.Add("DX.UITESTINGENABLED", "1");
pWad = Process.Start(psi);
}

[OneTimeTearDown]
public void FixtureTearDown() {
desktopSession.CloseApp();
pWad.Kill();
}

[SetUp]
public void Setup() {}

[Test]
public void Test1() {
Assert.Pass();
}
}

 

FixtureSetup方法執行以下操作:

  • 調用StartWAD方法,該方法啓動WinAppDriver.exe並啓用UI測試模式。
  • 創建一個新的Appium測試會話。
  • 調用WaitSplashScreen方法並在應用程序加載操作期間掛起測試。

UI測試模式

將DevExpress WPF控件切換到UI測試模式,爲此請將被測應用程序(在應用程序啓動時)將DX.UITESTINGENABLED環境變量設置爲1或將ClearAutomationEventsHelper.UITestingEnabled 屬性設置爲true。此模式產生以下變化:

  • 動畫被禁用。
  • 上下文菜單僅在單擊鼠標時激活,並且當鼠標指針位於菜單上方時不會打開。
  • 修改了UI自動化樹,以產生更穩定和可靠的UI測試。

記錄測試

請按照以下步驟記錄測試:

1. 以管理員身份運行WinAppDriver UI記錄器。

2. 在Test1方法中設置一個斷點。

3. 調試Test1測試,這將運行OutlookInspired演示應用程序並啓用UI測試模式。

4. 單擊WinAppDriver UI記錄器窗口中的Record按鈕。

將鼠標懸停在New Employee按鈕上,然後等待,直到記錄器在按鈕周圍顯示藍色邊框爲止。 這意味着記錄器已準備好捕獲輸入,點擊按鈕。

DevExpress使用教程

將鼠標懸停在First Name文本字段上,然後等待,直到記錄儀準備好捕獲輸入,輸入一個值。

對Last Name、Title、Mobile Phone和Email文本字段重複上一步。

記錄Save & Close按鈕的點擊。

5. 在Recorder窗口中,單擊Pause and copy按鈕將生成的代碼複製到剪貼板。

缺點

上面概述的方法有一些缺點:

  • 這些測試使用FindElementByXPath方法查找元素,這種方法很慢,因爲它解析了整個可視樹。 在我們的測試機上,測試耗時1分32秒。
  • 這些測試很難維護,因爲它們使用絕對XPath來定位元素,對應用佈局的更改可能會破壞測試。
  • 這些測試很難閱讀。

重寫測試

我們可以重寫測試以解決上述問題(並加快測試速度)。 您可以分析記錄的xpath或使用檢查工具來獲取元素屬性,例如Names、ClassNames和AccessibilityIds。

使用WinAppDriver的FindElementByName、FindElementByClassName和FindElementByAccessibilityId方法查找應用程序元素,這些方法比FindElementByAbsoluteXPath方法要快。 修改應用程序的佈局時,基於這些方法的測試不會失敗。

 

[Test][Order(0)]
public void CreateEmployee() {
var desktopElement = desktopSession.DesktopSessionElement;
var bNewEmployee = desktopElement.FindElementByName("New Employee");
bNewEmployee.Click();
WindowsElement newEmployeeWindow = null;
while (newEmployeeWindow == null)
newEmployeeWindow = desktopElement.FindElementByName("Employee (New)");
newEmployeeWindow.FindElementByName("First Name").FindElementByClassName("TextEdit").SendKeys("John");
newEmployeeWindow.FindElementByName("Last Name").FindElementByClassName("TextEdit").SendKeys("Doe");
newEmployeeWindow.FindElementByName("Title").FindElementByClassName("TextEdit").SendKeys("CTO");
newEmployeeWindow.FindElementByName("Mobile Phone").FindElementByClassName("ButtonEdit").SendKeys("1111111111");
newEmployeeWindow.FindElementByName("Email").FindElementByClassName("ButtonEdit").SendKeys("[email protected]");
newEmployeeWindow.FindElementByName("Save & Close").Click();
}

 

重寫後的測試僅需25秒。


上DevExpress中文網,獲取第一手最新產品資訊!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章