前回は、テスト対象のアプリケーションを起動して UI オートメーション要素を取得するところまでを実装しました。
今回は、テキストボックスに文字列を自動入力する処理を実装したいと思います。
自動入力って響きがいいですね。
入力するテストパターンがたくさんあるときは、かなり有効です。
まずは、テスト対象の実装から始めます。
テキストボックスの作成
テスト対象のウィンドウに TextBox コントロールを追加して、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>
<TextBox Name="textBox" />
</Grid>
</Window>
コントロール パターンの取得
次にテキストボックスの UI オートメーション要素からコントロール パターンを取得する処理を実装します。コントロール パターンは複数あり、今回は ValuePattern を使用します。
public static class AutomationHelper
{
public static ValuePattern GetValuePattern(AutomationElement element)
{
if (element != null)
{
object pattern = null;
if (element.TryGetCurrentPattern(ValuePattern.Pattern, out pattern))
{
return pattern as ValuePattern;
}
}
return null;
}
}
文字列の入力
最後は取得した ValuePattern を使用して文字列を入力する処理を実装します。ValuePattern クラスの SetValue メソッドで文字列を入力できます。
[TestFixture]
public class MainWindowTest
{
[TestCase]
public void TextBoxTest()
{
var textBoxElement = AutomationHelper.FindElementById(this.targetElement, "textBox");
if (textBoxElement == null)
{
Assert.Fail();
}
var textBoxPattern = AutomationHelper.GetValuePattern(textBoxElement);
if (textBoxPattern == null)
{
Assert.Fail();
}
textBoxPattern.SetValue("自動入力文字列");
}
}
ひと通り実装が終わったので、NUnit で実行します。
テスト対象のアプリケーションが起動して、テキストボックスに文字列が入力されました。
完成です!
でも、このままだとアプリケーションが起動したままですね。
起動したアプリケーションを閉じる方法はまた次回に。
頑張りすぎず脱力系でいこうと思います。
以上。
関連記事
0 件のコメント:
コメントを投稿