原創(chuàng)|使用教程|編輯:龔雪|2021-04-13 10:29:11.893|閱讀 205 次
概述:在v20.2版本中,技術(shù)團隊增強了對WPF產(chǎn)品線UI測試自動化的支持,UI自動化現(xiàn)在包括更全面的自動化測試功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
下載DevExpress v20.2完整版 DevExpress v20.2漢化資源獲取
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)在包括更全面的自動化測試功能:
您可以使用UIAutomationClient庫API創(chuàng)建自動測試,也可以使用基于UI自動化技術(shù)的任何UI測試庫。
DevExpress WPF控件包含UI測試模式選項,使用時會對應(yīng)用程序進行以下更改:
注意:我們使用測試了控件。
請按照以下步驟創(chuàng)建一個新的測試項目:
1. 打開Windows命令提示符,創(chuàng)建項目文件夾或?qū)Ш降浆F(xiàn)有文件夾,然后使用以下命令:
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復制并粘貼到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í)行以下操作:
將DevExpress WPF控件切換到UI測試模式,為此請將被測應(yīng)用程序(在應(yīng)用程序啟動時)將DX.UITESTINGENABLED環(huán)境變量設(shè)置為1或?qū)?屬性設(shè)置為true。此模式產(chǎn)生以下變化:
請按照以下步驟記錄測試:
1. 以管理員身份運行WinAppDriver UI記錄器。
2. 在Test1方法中設(shè)置一個斷點。
3. 調(diào)試Test1測試,這將運行OutlookInspired演示應(yīng)用程序并啟用UI測試模式。
4. 單擊WinAppDriver UI記錄器窗口中的Record按鈕。
將鼠標懸停在New Employee按鈕上,然后等待,直到記錄器在按鈕周圍顯示藍色邊框為止。 這意味著記錄器已準備好捕獲輸入,點擊按鈕。
將鼠標懸停在First Name文本字段上,然后等待,直到記錄儀準備好捕獲輸入,輸入一個值。
對Last Name、Title、Mobile Phone和Email文本字段重復上一步。
記錄Save & Close按鈕的點擊。
5. 在Recorder窗口中,單擊Pause and copy按鈕將生成的代碼復制到剪貼板。
上面概述的方法有一些缺點:
我們可以重寫測試以解決上述問題(并加快測試速度)。 您可以分析記錄的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技術(shù)交流群3:700924826 歡迎一起進群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)