それってスゴイ便利じゃん!
ってことで調べてみました。
UI オートメーションには、4つのコンポーネントがあるみたいです。
- UIAutomationClient
- UIAutomationClientsideProviders
- UIAutomationProvider
- UIAutomationTypes
今回は、UIAutomationClient と UIAutomationTypes を参照設定に追加して始めます。
テストターゲットプロジェクトの作成
初めにテスト対象の WPF アプリケーション プロジェクトを作成します。MainWindow に Name プロパティを追加しておきます。
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Name="mainWindow">
<Grid>
</Grid>
</Window>
テストプロジェクトの作成
次にテストを行う クラス ライブラリ プロジェクトを作成します。ここでは、MainWindow のテストクラスを作成します。
起動するアプリケーションの名前、MainWindow の名前、今回のキモとなるテスト対象の UI オートメーション要素を定義しておきます。
単体テストは、NUnit を使用します。
[TestFixture]
public class MainWindowTest
{
private string applicationName;
private string windowName;
private AutomationElement targetElement;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
this.applicationName = Properties.Resources.ApplicationName;
this.windowName = Properties.Resources.WindowName;
}
}
UI オートメーション要素の取得
テスト対象のアプリケーションを操作するために、対象の UI オートメーション要素を取得します。今回は UI オートメーション要素を Name プロパティの値から取得するヘルパー関数を作成しました。
public static class AutomationHelper
{
public static AutomationElement FindElementById(AutomationElement element, string automationId, TreeScope treeScope = TreeScope.Element | TreeScope.Descendants)
{
var target = element.FindFirst(
treeScope,
new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
return target;
}
}
テストの準備
最後にアプリケーションを起動して、UI オートメーション要素を取得する処理を実装します。今回は、アプリケーションを起動して、10秒経っても UI オートメーション要素が取得できないときはテスト失敗にしました。
ここはテスト対象のアプリケーションによって修正すればいいと思います。
[TestFixture]
public class MainWindowTest
{
[SetUp]
public void SetUp()
{
var p = Process.Start(this.applicationName);
AutomationElement desktopElement = null;
desktopElement = AutomationElement.RootElement;
if (desktopElement == null)
{
Assert.Fail();
}
this.targetElement = null;
var waitCount = 0;
do
{
this.targetElement = AutomationHelper.FindElementById(desktopElement, this.windowName, TreeScope.Children);
waitCount++;
Thread.Sleep(200);
}
while (this.targetElement == null && waitCount < 50);
if (this.targetElement == null)
{
Assert.Fail();
}
}
}
まだテストの準備段階ですが、今回はここまで。
頑張りすぎず脱力系でいこうと思います。
以上。
0 件のコメント:
コメントを投稿