UIAutomation | パークのソフトウエア開発者ブログ|ICT技術(Java・Android・iPhone・C・Ruby)なら株式会社パークにお任せください

パークのソフトウエア開発者ブログ|ICT技術(Java・Android・iPhone・C・Ruby)なら株式会社パークにお任せください

開発の解決方法や新しい手法の情報を、パークのエンジニアが提供します。パークのエンジニアが必要な場合は、ぜひお気軽にお問い合わせ下さい。 株式会社パーク:http://www.pa-rk.co.jp/

NUnitを使用してGUIのテストを行うことができる。
GUIのテストをするために、今回はUIAutomationを使用する。

今回は、テキスト、ボタン、コンボボックスを使用して簡単なプログラムを作成した。

テスト対象のプログラム
下記はテスト対象のプログラムである。

動作は、テキストに設定されている文字列をボタン押下でコンボボックスに追加するたけのプログラムである。

using System;
using System.Collection.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Sample.UIApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button_Click(object sender, EventArgs e)
{
comboBox.Item.Add(textBox.Text);
textBox.Text = "";
}
}
}

テストプログラム
テストのプロジェクトに下記の参照設定を行う。
UIAutomationClient
UIAutomationTypes

テストは、テキストに文字列を設定してボタンを押下すうとコンボボックスにテキストの文字列が追加されたことを確認するものである。
下記がテストプログラムである。

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;
using NUint.Famework;

namespace Sample.UITest
{
[TestFixture]
public class UITest
{
pivate AutomationElement from;

///
/// テストの初期化
///

[TestFixttureSetUp]
public void TestFixttureSetUp()
{
// テストするUIAppを起動させる
Process process = Process.Start(@".\UIApp.exe");
      
      // UIAppが立ちあがらないと、from取得に失敗すうのでスリープさせる
Thread.Sleep(1000);

// 起動したUIAppを参照
from = AutomationElement.FromHandle(process.MainWindowHandle);
}

///
/// テストの終了処理
///

[TestFixttureTearDown]
public void TestFixttureTearDown()
{
// UIAppを終了させる
WindowPattern patternFrom = (WindowPattern)from.GetCurrentPattern(WindowPattern.Pattern);
patternFrom.Close();
}

///
/// テストの処理
    /// TextBoxに文字列を設定
/// Buttonを押下するとTextBoxに設定した文字列をComboBoxに追加
/// ComboBoxに正常に追加されたかを確認。
///

[Test]
public void Test()
{
// UIApp画面のtextBoxを取得
AutomationElement elementTextbox = this.GetFromControl("textBox");
// textBoxのパターンを取得
ValuePattern valueTextbox = (ValuePattern)elementTextbox.GetCurrentPattern(ValuePattern.Pattern);
// textBoxに文字列を設定
valueTextbox.SetValue("park");

// UIApp画面のbuttonを取得
AutomationElement elementTextbox = this.GetFromControl("button");
// buttonのパターンを取得
InvokePattern patternButton = (InvokePattern)elementTextbox.GetCurrentPattern(InvokePattern.Pattern);
// buttonを押して
// textBoxに設定していう文字をcomboBoxに追加
patternButton.Invoke();

// UIApp画面のcomboBoxを取得
AutomationElement elementComboBox = this.GetFromControl("comboBox");
// comboBoxからアイテムリストを取得
AutomationElementCollection collection = elementComboBox.FindAll(TreeScope.Subtree,
new PropertyCondition(AutomationElement.ControlTypeProperty.ControlType.ListItem));
// アイテムリストの先頭を選択する
SelectionItemPattern patternItem = (SelectionItemPattern)collection[0].GetCurrentPattern(SelectionItemPattern.Pattern);
patternItem.Select();

// UIApp画面のcomboBoxの文字を取得して確認する。
    Assert.AreEqual("park", elementComboBox.Current.Name, "失敗");
}


///
/// テスト対象のAppから指定したコントロールを取得
///

private AutomationElement GetFromControl(String controlName)
{
PropertyCondition condition = new PropertyCondition(AutomationElement.AutomationIdProperty, controlName);

AutomationElement control = null;
// テスト対象のAppからcontrolNameのコントロールを取得
AutomationElementCollection collection = from.FindAll(TreeScope.Descendants, condition);
foreach(AutomationElement element in collection)
{
if(element.Current.ControlType == ControlType.Edit ||
element.Current.ControlType == ControlType.Button ||
element.Current.ControlType == ControlType.ComboBox)
{
control = element;
break;
}
}
return control;
}
}
}

@some