前回は、起動したアプリケーションを終了する処理を実装しました。
これで連続する自動テストを実行できるようになりました。
今回は、ボタンを押す処理を実装したいと思います。
この処理はメニューアイテムを選択するときにも使えます。
ボタンの作成
テスト対象のウィンドウに Button コントロールを追加して、Name プロパティを定義します。追加したボタンの Click イベントに、テキストボックスの値を変える処理を追加します。
<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">
<StackPanel>
<TextBox Name="textBox" />
<Button Name="button"
Content="Execute"
Click="OnClick" />
</StackPanel>
</Window>
コントロールパターンの取得
前回は、WindowPattern を取得しました。今回は、InvokePattern です。public static class AutomationHelper
public static InvokePattern GetInvokePattern(AutomationElement element)
{
if (element != null)
{
object pattern = null;
if (element.TryGetCurrentPattern(InvokePattern.Pattern, out pattern))
{
return pattern as InvokePattern;
}
}
return null;
}
}
ボタンのクリック
最後は取得した InvokePattern を使用してクリック処理を実装します。InvokePattern クラスの Invoke メソッドでクリックできます。
[TestFixture]
public class MainWindowTest
[TestCase]
public void ButtonTest()
{
var buttonElement = AutomationHelper.FindElementById(this.targetElement, "button");
if (buttonElement == null)
{
Assert.Fail();
}
var buttonPattern = AutomationHelper.GetInvokePattern(buttonElement);
if (buttonPattern == null)
{
Assert.Fail();
}
buttonPattern.Invoke();
Thread.Sleep(1000);
}
}
ひと通り実装が終わったので、NUnit で実行します。
テキストボックスの文字列が変更されました。
完成です。
次回はテキストボックスの値を確認する方法を説明したいと思います。
頑張りすぎず脱力系でいこうと思います。
以上。
関連記事
0 件のコメント:
コメントを投稿