翻譯|使用教程|編輯:龔雪|2024-04-10 10:57:26.967|閱讀 105 次
概述:本文將為大家展示如何使用UI自動化在Visual Studio 2022中編寫簡單/高級UI測試,歡迎下載相關(guān)組件體驗(yàn)!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForm能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
UI自動化測試?yán)锰囟ǖ墓ぞ?框架來模擬用戶與界面的交互,并幫助確保應(yīng)用程序滿足相關(guān)的最終用戶需求。當(dāng)與其他測試方法(API測試、單元測試等)結(jié)合使用時,UI自動化可以提高應(yīng)用程序的穩(wěn)定性,減少花在手工測試上的時間,當(dāng)然還可以提高用戶滿意度。在本文中,我們將向您展示如何使用UI自動化在Visual Studio 2022中編寫簡單/高級UI測試。
在開始之前,我們先看看UI測試的優(yōu)勢:
DevExpress技術(shù)交流群9:909157416 歡迎一起進(jìn)群討論
在上文中(),我們?yōu)榇蠹医榻B了UI測試自動化是如何工作的、開始創(chuàng)建UI自動化測試等,本文將繼續(xù)介紹如何創(chuàng)還能UI自動化測試。
在進(jìn)行測試之前,我想澄清幾點(diǎn):
AutomationElement logInFormElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "CRM Log In Form"));
FindFirst方法有兩個參數(shù),第一個參數(shù)(scope)指定搜索的范圍,第二個參數(shù)(條件)指定要匹配的標(biāo)準(zhǔn)(在我的例子中,這是一個AutomationName = "CRM Log In Form"的表單)。
UI控件可以根據(jù)其他設(shè)置(例如,Text)自動“計(jì)算”AutomationName。
如果需要,您可以顯式地設(shè)置AutomationName屬性或處理DXAccessible.QueryAccessibleInfo事件,來向DevExpress UI元素提供可訪問性信息。
public static class AutomationElementExtensions { public static AutomationElement FindFirstWithTimeout(this AutomationElement @this, TreeScope scope, Condition condition, int timeoutMilliseconds = 1000) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); do { var result = @this.FindFirst(scope, condition); if (result != null) return result; Thread.Sleep(100); } while (stopwatch.ElapsedMilliseconds < timeoutMilliseconds); return null; } }
下面的測試將執(zhí)行以下操作:
[Test] public void NonExistingUsernameLoginTest() { afterLogInAction = CheckErrorLabel; LogIn("TestNonExistingUser", "123456"); } void CheckErrorLabel() { AutomationElement errorLabelElement = loginForm.FindFirstByNameWithTimeout( TreeScope.Children, "Invalid User or Password", 10000); Assert.IsNotNull(errorLabelElement); } void LogIn(string username, string password) { // Finds the LogIn form and its main UI elements. loginForm = AutomationElement.RootElement.FindFirstByNameWithTimeout( TreeScope.Children, logInFormAccessbleName, 10000); AutomationElement usernameElement = loginForm.FindFirstByNameWithTimeout(TreeScope.Children, usernameAccessbleName, 10000); AutomationElement passwordElement = loginForm.FindFirstByNameWithTimeout(TreeScope.Children, passwordAccessbleName, 10000); AutomationElement logInButtonElement = loginForm.FindFirstByNameWithTimeout(TreeScope.Children, logInButtonAccessbleName, 10000); // Gets automation patterns to fill "UserName" and "Password" inputs (editors). ValuePattern usernameValuePattern = (ValuePattern)usernameElement.GetCurrentPattern(ValuePattern.Pattern); ValuePattern passwordValuePattern = (ValuePattern)passwordElement.GetCurrentPattern(ValuePattern.Pattern); InvokePattern invokePattern = (InvokePattern)logInButtonElement.GetCurrentPattern(InvokePattern.Pattern); // Sets editor values. Fills in username and password input fields. usernameValuePattern.SetValue(username); passwordValuePattern.SetValue(password); invokePattern.Invoke(); // Performs an action after a log in attempt. afterLogInAction?.Invoke(); }
正如您所看到的,編寫測試可以歸結(jié)為獲取一個AutomationElement并調(diào)用它的模式方法。
讓我們考慮一個更復(fù)雜的情況,并在DevExpress WinForm數(shù)據(jù)網(wǎng)格(GridControl)中測試數(shù)據(jù)編輯。DevExpress數(shù)據(jù)網(wǎng)格包含一個帶有布爾值的“Is Modified”未綁定列,該列的值表示用戶是否修改了“Name”列的值。
我使用TablePattern與網(wǎng)格控件一起工作,下面的例子展示了如何編寫一個測試來修改我們的WinForms Grid中的客戶名稱,并檢查“Is Modified”列中的值是否從false變?yōu)閠rue:
[Test] public void ModifiedCustomerTest() { LogIn(testExistingUserLogin, testExistingUserPassword); // Finds the GridControl and gets its TablePattern. customersForm = AutomationElement.RootElement.FindFirstByNameWithTimeout( TreeScope.Children, customersFormAccessbleName, 10000); AutomationElement customersGrid = customersForm.FindFirstByIdWithTimeout( TreeScope.Children, customersGridAutomationID, 10000); TablePattern customersTablePattern = (TablePattern)customersGrid.GetCurrentPattern(TablePattern.Pattern); // Activates a cell within the GridControl. AutomationElement cellToUpdate = customersTablePattern.GetItem(1, 1); InvokePattern testCellInvokePattern = (InvokePattern)cellToUpdate.GetCurrentPattern(InvokePattern.Pattern); testCellInvokePattern.Invoke(); // Modifies the cell's value. AutomationElement editingControl = customersGrid.FindFirstByNameWithTimeout(TreeScope.Descendants, "Editing control", 1000); ValuePattern editedCellValuePattern = (ValuePattern)editingControl.GetCurrentPattern(ValuePattern.Pattern); editedCellValuePattern.SetValue("Value updated!"); Thread.Sleep(1000); // Sets a delay for demonstration purposes. // Selects the next data row. AutomationElement nextRowCell = customersTablePattern.GetItem(2, 1); SelectionItemPattern selectionItemPattern = (SelectionItemPattern)TreeWalker.ControlViewWalker.GetParent(nextRowCell).GetCurrentPattern(SelectionItemPattern.Pattern); selectionItemPattern.Select(); Thread.Sleep(1000); // Checks if the value in the "Is Modified" column has changed. int isModiedColumnIndex = customersTablePattern.Current.GetColumnHeaders().ToList().FindIndex(h => h.Current.Name == "Is Modified"); AutomationElement isModifiedCell = customersTablePattern.GetItem(1, isModiedColumnIndex); ValuePattern isModifiedCellValuePattern = (ValuePattern)isModifiedCell.GetCurrentPattern(ValuePattern.Pattern); Assert.AreEqual(isModifiedCellValuePattern.Current.Value, "Checked"); }
要運(yùn)行我剛剛創(chuàng)建的測試,將用tests展開項(xiàng)目(“TestRunner”),右鍵單擊*.cs文件來調(diào)用上下文菜單,然后單擊"Run Tests"。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)