Chromium Chronicle #21:ChromeOS 端對端使用者介面自動化

第 21 集:作者:加州山景城的 Brendan Hansknecht (2021 年 5 月)
上一集

以往,在所有裝置的端對端 (E2E) 測試中自動執行 ChromeOS UI 一直都很困難。在 Tast 中,已建立新的 UI 程式庫,並使用 Chrome a11y (無障礙) 樹狀結構控制 ChromeOS UI。這個程式庫可讓開發人員輕鬆針對任何可見的 UI 介面 (ChromeOS 電腦 UI、原生應用程式、網頁應用程式、Chrome 瀏覽器 UI) 建立 E2E 測試。

程式庫提供簡單的鏈結方式,說明如何尋找 UI 元素。舉例來說,「檔案」應用程式中的「下載」資料夾可以定義如下:

filesWindow := nodewith.NameStartingWith("Files")
  .ClassName("RootView").Role(role.Window)
downloadsButton := nodewith.Name("Downloads")
  .Role(role.TreeItem).Ancestor(filesWindow)

定義節點尋找工具後,您就能透過多種方式與節點互動。從簡單的點擊到等待聚焦,UI 程式庫可為許多作業提供穩定的存取機制。例如,如要在「Downloads」資料夾上按一下滑鼠右鍵,然後點選複製按鈕,您可以寫入:

ui := uiauto.New(tconn)
if err := uiauto.Combine("copy downloads",
  ui.RightClick(downloadsButton),
  ui.LeftClick(nodewith.Name("Copy").Role(role.MenuItem)),
)(ctx); err != nil { /* do error handling */ }

現有常見 UI 區域的包裝函式 (設定、啟動器、檔案應用程式等)。

上述使用的 uiauto.Run 函式會取得動作清單。在這個情境中,動作只是 func(context.Context) error。只要使用這類簡單的 API,其他種類的動作可與 UI 動作中混用。舉例來說,鍵盤的使用方式如下所示:

if err := uiauto.Combine("do some random stuff",
  ui.FocusAndWait( /* some text field */ ),
  kb.TypeAction("Hello, world!"),
  kb.AccelAction("Enter"),
  func(ctx context.Context) error {
    // My custom action!
  },
)(ctx); err != nil { /* do error handling */ }

如需更詳盡的指南,請參閱「Tast Codelab:Chrome UI Automation」一文

編寫這些測試時,傾印 Chrome a11y 樹狀結構以進行偵錯。只要加入下列程式碼即可達到此目的:

defer faillog.DumpUITreeOnError(ctx, s.OutDir(), s.HasError, tconn)
s.Fatal("I would like to see the ui tree here")

a11y 樹狀結構現在會與其他 Tast 記錄儲存 faillog/ui_tree.txt

如有任何問題,請與tast-users 群組聯絡。