Windows 10 および Visual Studio 2015 で Appium を使用して Windows アプリをテストする

Connect(); で// 2016 年、Scott Hanselman の基調講演には、Appium と呼ばれるツール (Stacey Doerr によって提示された) の短い説明が含まれています。このツールを使用すると、Windows アプリの UI テストを作成および自動化できます。これは、UWP アプリだけでなく、基本的に Windows マシンで実行されるすべてのアプリです。自動化された UI テストは、Web 開発から UWP 開発に移行するときに見逃していたものであるため、このギャップを埋めるのに役立つプロジェクトがあることを知り、非常に興奮しました.

よくあることですが、新しいことを始めるのは難しいものです。Microsoft からの現在の指示に従っていると、いくつかのエラーが発生していることがわかりました。これは私の開発マシンのセットアップが原因である可能性がありますが、同じ問題に遭遇する可能性があります。この投稿では、Appium を機能させるためにたどったプロセスについて説明し、途中で見つけたエラー メッセージも記録します。

Appium のインストールとトラブルシューティング

ステップ 1 – Node.js をインストール

ここから Node.js をインストールします。

ステップ 2 – 管理者として PowerShell プロンプトを開き、Appium をインストールします

管理者特権の PowerShell プロンプトから、次のコマンドを実行します:

npm install –g appium

このコマンドを実行すると、次の警告が画面に出力されましたが、心配する必要はないと思います:

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.12(node_modules\appium\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

ステップ 3 – 昇格した PowerShell プロンプトから、Appium を実行します

管理者特権の PowerShell プロンプトから、次のコマンドを実行します:

appium

数秒後、次のテキストが画面に出力されます。

Welcome to Appium v1.6.0
Appium REST http interface listener started on 0.0.0.0:4723

この時点で、Appium が GitHub で提供している Sample Calculator App でテストを実行しようとしました。 Visual Studio を使用してこれらのテストを実行しましたが、5 つのテストすべてが失敗し、次のエラーが PowerShell プロンプトに出力されました。

[Appium] Creating new WindowsDriver session
[Appium] Capabilities:
[Appium]   app: 'Microsoft.WindowsCalculator_8wekyb3d8bbwe!App'
[Appium]   platformName: 'Windows'
[Appium]   deviceName: 'WindowsPC'
[BaseDriver] The following capabilities were provided, but are not recognized by appium: app.
[BaseDriver] Session created with session id: dcfce8e7-9615-4da1-afc5-9fa2097673ed
[WinAppDriver] Verifying WinAppDriver is installed with correct checksum
[debug] [WinAppDriver] Deleting WinAppDriver session
[MJSONWP] Encountered internal error running command: 
	Error: Could not verify WinAppDriver install; re-run install
    at WinAppDriver.start$ (lib/winappdriver.js:35:13)
    at tryCatch (C:\Users\Jeremy\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\Jeremy\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22)
    at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (C:\Users\Jeremy\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21)
    at GeneratorFunctionPrototype.invoke (C:\Users\Jeremy\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)

私のマシンでは、何らかの理由で、Appium のインストール中に WinAppDriver が正しくインストールされませんでした。

ステップ 4 – WinAppDriver の v0.5-beta を手動でインストール

これは非常に簡単に修正できます。GitHub サイトから WinAppDriver インストーラーを取得するだけです。しかし、Appium のバージョン 1.6.0 では、WinAppDriver の正しいバージョン、特に v0.5-beta を選択することが重要であることがわかりました。 、2016 年 9 月 16 日にリリースされました。上位バージョンは、Appium v​​1.6.0 では機能しませんでした。

ステップ 5 – 昇格した PowerShell プロンプトから Appium を再起動します

WinAppDriver v0.5-beta のインストールは非常に簡単なプロセスでした。ファイルをダブルクリックして、すべてのデフォルト オプションを選択するだけでした。次に、手順 3 を繰り返し、昇格した PowerShell プロンプトから Appium を再起動しました。再び、数秒後に同じメッセージが表示されました.

Welcome to Appium v1.6.0
Appium REST http interface listener started on 0.0.0.0:4723

今回、GitHub から Sample Calculator App のテストを実行したところ、すべて合格しました。また、PowerShell プロンプトにはエラーは表示されませんでした。WinAppDriver のインストールを確認できなかったという代わりに、以下のメッセージが表示されました。

[WinAppDriver] Verifying WinAppDriver is installed with correct checksum
[debug] [WinAppDriver] WinAppDriver changed state to 'starting'
[WinAppDriver] Killing any old WinAppDrivers, running: FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"4823 "`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I winappdriver.exe`) do (IF NOT %b=="" TASKKILL /F /PID %a))
[WinAppDriver] No old WinAppDrivers seemed to exist
[WinAppDriver] Spawning winappdriver with: undefined 4823/wd/hub
[WinAppDriver] [STDOUT] Windows Application Driver Beta listening for requests at: http://127.0.0.1:4823/wd/hub
[debug] [WinAppDriver] WinAppDriver changed state to 'online'

標準の Windows Calculator が表示され、一連の自動化された UI テストがアプリで実行されるのを見ることができました。

これらのアプリの自動化情報を取得するにはどうすればよいですか?

サンプル電卓アプリとテスト用の基本的なシナリオを見ると、以下のスニペットのように、いくつかの奇妙な定数値を持つコードが表示されます。

DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
appCapabilities.SetCapability("platformName", "Windows");
appCapabilities.SetCapability("deviceName", "WindowsPC");
CalculatorSession = new RemoteWebDriver(new Uri(WindowsApplicationDriverUrl), appCapabilities);
Assert.IsNotNull(CalculatorSession);
CalculatorSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));
 
// Make sure we're in standard mode
CalculatorSession.FindElementByXPath("//Button[starts-with(@Name, \"Menu\")]").Click();
OriginalCalculatorMode = CalculatorSession.FindElementByXPath("//List[@AutomationId=\"FlyoutNav\"]//ListItem[@IsSelected=\"True\"]").Text;
CalculatorSession.FindElementByXPath("//ListItem[@Name=\"Standard Calculator\"]").Click();

上記のコードは、テストが識別子を持つアプリを探すことを示しています:

「Microsoft.WindowsCalculator_8wekyb3d8bbwe!アプリ」

これが Microsoft Windows の電卓アプリであることは明らかですが、ほとんどの人は、この文字列の末尾に追加された奇妙に見えるコードに気付かないでしょう。これは、アプリケーションの自動化識別子です。

この識別子を見つけるには、Windows 内から標準の電卓アプリケーションを起動します (実行プロンプトを開いて「Calc」と入力します)。

「Inspect」と呼ばれる Visual Studio 2015 に同梱されているツールがあります。通常、次の場所で利用できます。

C:\Program Files (x86)\Windows Kits\10\bin\x86

上記で指定したディレクトリから Inspect.exe を起動します。 Inspect アプリケーションを実行すると、現在 Windows 10 によって管理されているオブジェクトに関する膨大な量の情報が得られます。画面左側のツリー ビューにドリルダウンして実行中のアプリケーションを表示すると、「電卓」を選択できます。 」、右側に「AutomationId」の値が表示されます – 以下で赤で強調表示しています。

メニュー、ボタン、表示要素などの他のアイテムも、対応するメニュー、ボタン、または表示要素を選択すると、このビューから取得できます。FindElementByXPath メソッドを使用して要素を識別する場合に特に役立つプロパティは、「Legacy|Accessible:Name」です。 .

結論

この投稿が、Windows アプリの UI テストの自動化に関心のあるすべての人、特に Appium の動作に問題がある場合に役立つことを願っています。 Appium の GitHub には、非常に便利なサンプル アプリがいくつかあります。Windows アプリのコーディングは、最初は少し混乱することがわかりましたが、Inspect ツールの使用など、いくつかの重要な情報があれば、サンプル アプリが作成され、どのように機能するかを説明します。これにより、独自の自動化された UI テスト コードを使用できるようになります。このツールがアプリケーションの品質を向上させる機会を与えてくれることを楽しみにしています。この投稿が皆さんの開始にも役立つことを願っています。