溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

界面控件DevExpress WPF有哪些優(yōu)點

發(fā)布時間:2021-10-13 16:09:45 來源:億速云 閱讀:221 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“界面控件DevExpress WPF有哪些優(yōu)點”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

DevExpress WPF 擁有120+個控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能創(chuàng)建有著強大互動功能的XAML基礎(chǔ)應(yīng)用程序,這些應(yīng)用程序?qū)W⒂诋敶蛻舻男枨蠛蜆?gòu)建未來新一代支持觸摸的解決方案。

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

  • DevExpress WPF控件形成與主題無關(guān)的AutomationPeer層次結(jié)構(gòu)。

  • 您可以在自動化樹中搜索AutomationPeer屬性,生成和分配的XAML/代碼AutomationPeer屬性均可用于搜索。

  • AutomationPeers包括各種自動化模式,例如Invoke, ExpandCollapse, Selection, Scroll等。

您可以使用UIAutomationClient庫API創(chuàng)建自動測試,也可以使用基于UI自動化技術(shù)的任何UI測試庫。

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

  • 動畫被禁用。

  • 上下文菜單僅在單擊鼠標時激活,并且當鼠標指針懸停菜單項時不會打開。

  • 修改了UI自動化樹,以產(chǎn)生更穩(wěn)定和可靠的測試。

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

準備環(huán)境

  1. 啟用Windows Developer Mode。

  2. 安裝WinAppDriver。

  3. 下載WinAppDriver UI Recorder。

創(chuàng)建測試

請按照以下步驟創(chuàng)建一個新的測試項目:

1. 打開Windows命令提示符,創(chuàng)建項目文件夾或?qū)Ш降浆F(xiàn)有文件夾,然后使用以下命令:

  • dotnet new nunit --framework netcoreapp3.1 - 創(chuàng)建一個空的nunit測試項目。

  • dotnet add package Appium.WebDriver - 在您的項目中引用Appium.WebDriver包。

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

3. 創(chuàng)建一個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復(fù)制并粘貼到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方法執(zhí)行以下操作:

  • 調(diào)用StartWAD方法,該方法啟動WinAppDriver.exe并啟用UI測試模式。

  • 創(chuàng)建一個新的Appium測試會話。

  • 調(diào)用WaitSplashScreen方法并在應(yīng)用程序加載操作期間掛起測試。

UI測試模式

將DevExpress WPF控件切換到UI測試模式,為此請將被測應(yīng)用程序(在應(yīng)用程序啟動時)將DX.UITESTINGENABLED環(huán)境變量設(shè)置為1或?qū)learAutomationEventsHelper.UITestingEnabled 屬性設(shè)置為true。此模式產(chǎn)生以下變化:

  • 動畫被禁用。

  • 上下文菜單僅在單擊鼠標時激活,并且當鼠標指針位于菜單上方時不會打開。

  • 修改了UI自動化樹,以產(chǎn)生更穩(wěn)定和可靠的UI測試。

記錄測試

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

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

2. 在Test1方法中設(shè)置一個斷點。

3. 調(diào)試Test1測試,這將運行OutlookInspired演示應(yīng)用程序并啟用UI測試模式。

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

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

界面控件DevExpress WPF有哪些優(yōu)點

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

對Last Name、Title、Mobile Phone和Email文本字段重復(fù)上一步。

記錄Save & Close按鈕的點擊。

5. 在Recorder窗口中,單擊Pause and copy按鈕將生成的代碼復(fù)制到剪貼板。

缺點

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

  • 這些測試使用FindElementByXPath方法查找元素,這種方法很慢,因為它解析了整個可視樹。 在我們的測試機上,測試耗時1分32秒。

  • 這些測試很難維護,因為它們使用絕對XPath來定位元素,對應(yīng)用布局的更改可能會破壞測試。

  • 這些測試很難閱讀。

重寫測試

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

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

[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("john.doe@dx-email.com");
newEmployeeWindow.FindElementByName("Save & Close").Click();
}

重寫后的測試僅需25秒。

“界面控件DevExpress WPF有哪些優(yōu)點”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI