翻譯|使用教程|編輯:龔雪|2019-12-04 09:23:43.943|閱讀 1492 次
概述:DevExpress Winforms Controls 內(nèi)置140多個(gè)UI控件和庫,完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,本教程主要為大家介紹如何實(shí)現(xiàn)UI自動化。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress Winforms Controls 內(nèi)置140多個(gè)UI控件和庫,完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序。無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),DevExpress WinForms都能輕松勝任。DevExpress廣泛應(yīng)用于ECM企業(yè)內(nèi)容管理、 成本管控、進(jìn)程監(jiān)督、生產(chǎn)調(diào)度,在企業(yè)/政務(wù)信息化管理中占據(jù)一席重要之地。
【適用范圍】:各種桌面、Web應(yīng)用程序開發(fā),尤其是WinForms應(yīng)用程序開發(fā)。
點(diǎn)擊獲取DevExpress v19.2完整版試用下載
在針對Visual Studio 2019的發(fā)行說明中,Microsoft 宣布Coded UI測試的生命周期終止。
Microsoft建議將 一起用于測試桌面和UWP應(yīng)用,此消息引起廣大用戶的興趣:DevExpress控件是否與Appium兼容?經(jīng)過DevExpress團(tuán)隊(duì)的反復(fù)測試,答案是肯定的!使用Appium創(chuàng)建自動UI測試的方法如下。
1. 跳轉(zhuǎn)到 然后下載兩個(gè)APP,
2. 在Windows中打開。
3. 以管理員身份運(yùn)行WinAppDriver.exe并使其運(yùn)行,請注意應(yīng)用程序正在偵聽的地址,稍后您將需要它。

4. 打開您要測試的Visual Studio解決方案,或創(chuàng)建一個(gè)新的示例解決方案。
5. 將新的單元測試項(xiàng)目添加到解決方案。

6. 在Solution Explorer中右鍵單擊Unit Test project,然后選擇“Manage NuGet Packages…”,安裝最新的穩(wěn)定Appium.WebDriver程序包。
7. 打開Unit Test項(xiàng)目的UnitTest1.cs文件,并添加兩個(gè)類:MainDemoSession(定義開始和結(jié)束測試會話的方法)和Helper(包含查找被測試的UI元素的方法),將步驟3中的地址用作WindowsApplicationDriverUrl值。
public class MainDemoSession{protected const string WindowsApplicationDriverUrl = "//127.0.0.1:4723";private const string ApplicationPath = @"C:\Users\...\AppiumTest.exe";
protected static WindowsDriver<WindowsElement> desktopSession;
public static void Setup(TestContext context)
  {
  // Launch a new instance of the tested application
  if (desktopSession == null)
  {
  // Create a new session to launch the tested application
AppiumOptions options = new AppiumOptions();
  options.AddAdditionalCapability("app", ApplicationPath);
  desktopSession = new WindowsDriver<WindowsElement>(
  new Uri(WindowsApplicationDriverUrl), options);
  Assert.IsNotNull(desktopSession);
  Assert.IsNotNull(desktopSession.SessionId);
// Set implicit timeout to 1.5 seconds
  //to make element search to retry every 500 ms
  //for at most three times
  desktopSession.Manage().Timeouts().ImplicitWait =
  TimeSpan.FromSeconds(1.5);
  }
  }
public static void TearDown()
  {
  // Close the application and delete the session
  if (desktopSession != null)
  {
  desktopSession.Close();
  desktopSession.Quit();
  desktopSession = null;
  }
  }
  }
public static class Helper
  {
  public static WindowsElement FindElementByAbsoluteXPath(
  this WindowsDriver<WindowsElement> desktopSession,
  string xPath,
  int nTryCount = 3)
  {
  WindowsElement uiTarget = null;
  while (nTryCount-- > 0) {
  try {
  uiTarget = desktopSession.FindElementByXPath(xPath);
  }
  catch {
  }
  if (uiTarget != null) {
  break;
  }
  else {
  System.Threading.Thread.Sleep(400);
  }
  }
  return uiTarget;
  }
  }8. 修改自動生成的UnitTest1類,如下所示:
[TestClass]public class UnitTest1 : MainDemoSession{[TestMethod]public void TestMethod1(){//test start
//test finish
  }
  [ClassInitialize]
  public static void ClassInitialize(TestContext context)
  {
  Setup(context);
  }
[ClassCleanup]
  public static void ClassCleanup()
  {
  TearDown();
  }
  }9. 運(yùn)行您的應(yīng)用程序,并將其拖到主系統(tǒng)顯示屏上(如果您具有多屏幕設(shè)置)。
10. 啟動WinAppDriver UI Recorder然后點(diǎn)擊“Record”, 將鼠標(biāo)懸停在要與之交互的第一個(gè)UI元素上,然后等待它開始閃爍藍(lán)色。Recorder的狀態(tài)欄會將其文本從“Active”更改為“XPath Ready”。

11. 當(dāng)該元素閃爍時(shí),recorder已準(zhǔn)備就緒,您可以執(zhí)行UI操作:單擊此元素、將其拖動、輸入新值等。完成此元素后,將鼠標(biāo)懸停在另一個(gè)UI元素上,等待 recorder的確認(rèn)并重復(fù)該過程。
12. 記錄了要重現(xiàn)的一系列步驟后,請?jiān)趓ecorder中單擊“Pause”,您可以打開actions selector確保已記錄所有UI操作。

13. 單擊“Generate and copy C# code to Clipboard”按鈕來復(fù)制所有記錄的操作代碼,將此代碼粘貼到UnitTest1.TestMethod1方法中。 例如,下面的代碼選擇“Job”標(biāo)簽。

[TestMethod]
  public void TestMethod1()
  {
//test start
// LeftClick on TabItem "Job" at (20,31)
Console.WriteLine("LeftClick on TabItem \"Job\" at (20,31)");
string xpath_LeftClickTabItemJob_20_31 = "/Pane\[@ClassName=\"#32769\"\][@Name=\"Desktop 1\"]/Window\[starts-with(@AutomationId,\"XtraForm\")]/Pane[@Name=\"The XtraLayoutControl\"\][starts-with(@AutomationId,\"dataLayoutControl\")]/Table[@Name=\"Root\"]/Table[@Name=\"autoGeneratedGroup0\"]/Table[@Name=\"Root\"]/Table[@Name=\"Photo\"]/Table[@Name=\"FirstAndLastName\"]/Tab[@Name=\"Tabs\"]/TabItem[@Name=\"Job\"]";
var winElem_LeftClickTabItemJob_20_31 = desktopSession.FindElementByAbsoluteXPath(xpath_LeftClickTabItemJob_20_31);
if (winElem_LeftClickTabItemJob_20_31 != null)
{
winElem_LeftClickTabItemJob_20_31.Click();
}
else
{
Console.WriteLine($"Failed to find element using xpath: {xpath_LeftClickTabItemJob_20_31}");
return;
}
//test finish
}14. 在內(nèi)部測試期間,自動生成的代碼可能無法通過其完整路徑找到UI元素:
/Pane\[@ClassName=\"#32769\"\][@Name=\"Desktop 1\"]/Window[starts-with…
如果發(fā)生這種情況,請縮短所有元素路徑,使其以“ / Window”開頭。
string xpath_LeftClickTabItemJob_20_31 = "/Window[starts-with(@AutomationId...";
此外,您可以使用Assert.Fail而不是Console.WriteLine來調(diào)試測試(如果找不到UI元素,則可以)。
Assert.Fail($"Failed to find element...");
15. 在Visual Studio中右鍵單擊Unit Test project,然后單擊“Run Tests”。測試將啟動您的應(yīng)用程序,重復(fù)所有記錄的步驟,然后關(guān)閉應(yīng)用程序。 所有測試操作都記錄在步驟3中啟動的WinAppDriver控制臺中。

您可以通過與Coded UI相同的方式啟動Appium測試,唯一的區(qū)別是您需要在測試執(zhí)行計(jì)算機(jī)上運(yùn)行WinAppDriver。
DevExpress中文網(wǎng)官網(wǎng)QQ群:540330292 歡迎一起進(jìn)群討論
DevExpress 2019年度界面大賽火熱開啟!曬圖有獎,各大視頻VIP免費(fèi)領(lǐng)取!
掃描下方二維碼,立即參與哦!

本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自:慧都網(wǎng)