UI Recorderという、テスト自動化のためのツールが公開されたらしい。UI Recorderを使うと、操作手順をキャプチャしてC#のコードを生成してくれるみたい。試してみる。
Microsoft、オープンソースの自動UIテストスクリプトツール「WinAppDriver UI Recorder」を公開:自動UIテストのスクリプトを簡単に作成できる – @IT
http://www.atmarkit.co.jp/ait/articles/1806/22/news036.html
WinAppDriver/Tools/UIRecorder at master · Microsoft/WinAppDriver · GitHub
https://github.com/Microsoft/WinAppDriver/tree/master/Tools/UIRecorder
UI Recorderのビルド
1. リポジトリのクローニング
1 |
$ git clone https://github.com/Microsoft/WinAppDriver |
2. VS2017でソリューションファイルを開く
WinAppDriver\Tools\UIRecorder\WinAppDriverUIRecorder.sln
3. ビルド
ビルドしたらエラーが出た。
1 2 |
MSB8036: The Windows SDK version 10.0.16299.0 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". |
プロジェクトにアサインされているバージョンのWindows SDKがこのマシン上には存在しないので、ソリューションを右クリックして[Retarget solution]を選択し、存在するバージョンに変更する。
1 2 3 4 |
Retarget Projects Select Windows SDK version for the following projects. Windows SDK Version: 10.0.17134.0 ..\UIXPathLib\UIXPathLib.vcxproj |
再ビルドして成功。
UI操作を記録する
Windowsの電卓を操作して記録してみる。
1. UI Recorderを実行する。
2. [Record]ボタンをクリックして電卓アプリを操作する。
3. 操作完了したら[Stop Recording]ボタンをクリックする。
4. [C# Code]タブに操作内容のC#コードが生成される。
自動化アプリを作成する
前処理 (WinAppDriverの起動やセッションの確立処理など)部分は参考サイトのコードをそのままコピペさせてもらって、それに続けてUIRecorderが生成したC#のコードを貼りつける。
1. プロジェクト作成 Console App (.NET Framework)
2. NuGetでAppium.WebDriver をインストールする
3. コーディング
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
using System; using OpenQA.Selenium.Appium.Windows; using OpenQA.Selenium.Remote; namespace CalcTest { class Program { private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723"; private const string CalculatorAppId = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"; protected static WindowsDriver<WindowsElement> session; static void Main(string[] args) { string serverPath = System.IO.Path.Combine( System.Environment.GetFolderPath( System.Environment.SpecialFolder.ProgramFilesX86 ), @"Windows Application Driver", "WinAppDriver.exe" ); System.Diagnostics.Process.Start(serverPath); DesiredCapabilities appCapabilities = new DesiredCapabilities(); appCapabilities.SetCapability("app", CalculatorAppId); session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities); session.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1.5)); // LeftClick on "One" at (50,48) Console.WriteLine("LeftClick on \"One\" at (50,48)"); string xp1 = "//*[@ClassName=\"ApplicationFrameWindow\"]/Window[@Name=\"Calculator\"][@ClassName=\"Windows.UI.Core.CoreWindow\"]/Group[@ClassName=\"LandmarkTarget\"]/Group[@AutomationId=\"NumberPad\"][@Name=\"Number pad\"]/Button[@AutomationId=\"num1Button\"][@Name=\"One\"]"; var winElem1 = session.FindElementByXPath(xp1); if (winElem1 != null) { winElem1.Click(); } else { Console.WriteLine($"Failed to find element {xp1}"); return; } // LeftClick on "Two" at (43,35) Console.WriteLine("LeftClick on \"Two\" at (43,35)"); string xp2 = "//*[@ClassName=\"ApplicationFrameWindow\"]/Window[@Name=\"Calculator\"][@ClassName=\"Windows.UI.Core.CoreWindow\"]/Group[@ClassName=\"LandmarkTarget\"]/Group[@AutomationId=\"NumberPad\"][@Name=\"Number pad\"]/Button[@AutomationId=\"num2Button\"][@Name=\"Two\"]"; var winElem2 = session.FindElementByXPath(xp2); if (winElem2 != null) { winElem2.Click(); } else { Console.WriteLine($"Failed to find element {xp2}"); return; } // LeftClick on "Three" at (37,43) Console.WriteLine("LeftClick on \"Three\" at (37,43)"); string xp3 = "//*[@ClassName=\"ApplicationFrameWindow\"]/Window[@Name=\"Calculator\"][@ClassName=\"Windows.UI.Core.CoreWindow\"]/Group[@ClassName=\"LandmarkTarget\"]/Group[@AutomationId=\"NumberPad\"][@Name=\"Number pad\"]/Button[@AutomationId=\"num3Button\"][@Name=\"Three\"]"; var winElem3 = session.FindElementByXPath(xp3); if (winElem3 != null) { winElem3.Click(); } else { Console.WriteLine($"Failed to find element {xp3}"); return; } // LeftClick on "Plus" at (46,45) Console.WriteLine("LeftClick on \"Plus\" at (46,45)"); string xp4 = "//*[@ClassName=\"ApplicationFrameWindow\"]/Window[@Name=\"Calculator\"][@ClassName=\"Windows.UI.Core.CoreWindow\"]/Group[@ClassName=\"LandmarkTarget\"]/Group[@AutomationId=\"StandardOperators\"][@Name=\"Standard operators\"]/Button[@AutomationId=\"plusButton\"][@Name=\"Plus\"]"; var winElem4 = session.FindElementByXPath(xp4); if (winElem4 != null) { winElem4.Click(); } else { Console.WriteLine($"Failed to find element {xp4}"); return; } // LeftClick on "Six" at (40,40) Console.WriteLine("LeftClick on \"Six\" at (40,40)"); string xp5 = "//*[@ClassName=\"ApplicationFrameWindow\"]/Window[@Name=\"Calculator\"][@ClassName=\"Windows.UI.Core.CoreWindow\"]/Group[@ClassName=\"LandmarkTarget\"]/Group[@AutomationId=\"NumberPad\"][@Name=\"Number pad\"]/Button[@AutomationId=\"num6Button\"][@Name=\"Six\"]"; var winElem5 = session.FindElementByXPath(xp5); if (winElem5 != null) { winElem5.Click(); } else { Console.WriteLine($"Failed to find element {xp5}"); return; } // LeftClick on "Equals" at (39,17) Console.WriteLine("LeftClick on \"Equals\" at (39,17)"); string xp6 = "//*[@ClassName=\"ApplicationFrameWindow\"]/Window[@Name=\"Calculator\"][@ClassName=\"Windows.UI.Core.CoreWindow\"]/Group[@ClassName=\"LandmarkTarget\"]/Group[@AutomationId=\"StandardOperators\"][@Name=\"Standard operators\"]/Button[@AutomationId=\"equalButton\"][@Name=\"Equals\"]"; var winElem6 = session.FindElementByXPath(xp6); if (winElem6 != null) { winElem6.Click(); } else { Console.WriteLine($"Failed to find element {xp6}"); return; } } } } |
XPathはそのままだとNot Foundになるので、以下のように編集した。
編集前
1 |
string xp1 = "/Pane[@Name=\"Desktop 1\"][@ClassName=\"#32769\"]/Window[@Name=\"Calculator - Calculator\"][@ClassName=\"ApplicationFrameWindow\"]/Window[@Name=\"Calculator\"][@ClassName=\"Windows.UI.Core.CoreWindow\"]/Group[@ClassName=\"LandmarkTarget\"]/Group[@AutomationId=\"NumberPad\"][@Name=\"Number pad\"]/Button[@AutomationId=\"num1Button\"][@Name=\"One\"]"; |
編集後
1 |
string xp1 = "//*[@ClassName=\"ApplicationFrameWindow\"]/Window[@Name=\"Calculator\"][@ClassName=\"Windows.UI.Core.CoreWindow\"]/Group[@ClassName=\"LandmarkTarget\"]/Group[@AutomationId=\"NumberPad\"][@Name=\"Number pad\"]/Button[@AutomationId=\"num1Button\"][@Name=\"One\"]"; |
4. ビルドして実行
アプリが自動実行された。
参考サイト
WinAppDriver UI Recorderを試してみました。 | 初心者備忘録
https://www.ka-net.org/blog/?p=10504