Windowsアプリの受け入れテストを自動化しよう特集:受け入れ検査の自動化手法の考察(4/5 ページ)

» 2013年04月10日 07時41分 公開
[石川達也,株式会社Codeer]

UIオートメーションを使ってAPIを実装する

 UIオートメーションも、WCFと同じくマイクロソフトがサポートするUI操作手段の1つである。UIオートメーションの詳細に関しては「UIオートメーションによる自動UIテストの実践」などを参照願いたい。

 この手法のメリットは、テスト対象プロセスに変更が必要ないということである。そのため、レガシー・コードにも比較的適用しやすい。また、WCFは.NETアプリを対象にしたものであるが、この手法はネイティブ・アプリにも適用可能である。

 しかし、可能なことはGUI操作を使ったことに限定される。また、操作によっては実装の難易度も高い。さらに、GUIとビジネス・ロジックが自然と分離されるようなメリットには寄与しない、などのデメリットも挙げられる。

●APIの実装

 次のコードは、UIオートメーションを使ってManagementSystemクラスを操作するためのAPI(=EmployeeManagementAppクラス)の実装例である(先ほどのWCFの場合と同様にクラス・ライブラリのプロジェクト内に実装する)。

using System;
using EmployeeManagement;
using System.Diagnostics;
using System.IO;
using System.Windows.Automation;
using System.Threading;

namespace EmployeeManagementAPI
{
  public class EmployeeManagementApp : IDisposable
  {
    // 開始。
    public static EmployeeManagementApp Start()
    {
#if DEBUG
      string path = Path.GetFullPath("../../bin/Debug/EmployeeManagement.exe");
#else
      string path = Path.GetFullPath("../../bin/Release/EmployeeManagement.exe");
#endif
      return new EmployeeManagementApp(Process.Start(path));
    }

    AutomationElement mainForm;
    int processId;
    ValuePattern textBoxName;
    ValuePattern textBoxAge;
    ValuePattern textBoxPosition;
    InvokePattern buttonAdd;
    InvokePattern buttonFind;

    // コンストラクタ。
    public EmployeeManagementApp(Process process)
    {
      while (process.MainWindowHandle == IntPtr.Zero)
      {
        Thread.Sleep(10);
      }
      processId = process.Id;

      // GUIマッピング。
      mainForm = AutomationElement.FromHandle(process.MainWindowHandle);
      textBoxName = (ValuePattern)GetPattern("textBoxName", ValuePattern.Pattern);
      textBoxAge = (ValuePattern)GetPattern("textBoxAge", ValuePattern.Pattern);
      textBoxPosition = (ValuePattern)GetPattern("textBoxPosition", ValuePattern.Pattern);
      buttonAdd = (InvokePattern)GetPattern("buttonAdd", InvokePattern.Pattern);
      buttonFind = (InvokePattern)GetPattern("buttonFind", InvokePattern.Pattern);
    }

    // 終了。
    public void Dispose()
    {
      Process.GetProcessById(processId).CloseMainWindow();
    }

    // 登録。
    public bool Add(string name, string age, string post)
    {
      textBoxName.SetValue(name);
      textBoxAge.SetValue(age);
      textBoxPosition.SetValue(post);
      buttonAdd.Invoke();
      return string.IsNullOrEmpty(textBoxName.Current.Value);
    }

    // 検索。
    public EmployeeData Find(string name)
    {
      // 検索。
      textBoxName.SetValue(name);
      buttonFind.Invoke();

      // データ作成。
      if (string.IsNullOrEmpty(textBoxName.Current.Value))
      {
        return null;
      }
      return new EmployeeData(textBoxName.Current.Value,
        int.Parse(textBoxAge.Current.Value),
        textBoxPosition.Current.Value);
    }

    // パターンの取得。
    private object GetPattern(string name, AutomationPattern pattern)
    {
      AutomationElement textElement = mainForm.FindFirst(
        TreeScope.Element | TreeScope.Descendants,
        new PropertyCondition(
          AutomationElement.AutomationIdProperty,
          name));
      return textElement.GetCurrentPattern(pattern);
    }
  }
}

リスト5 UIオートメーションでAPIを実装する(EmployeeManagementApp.cs)
EmployeeManagementプロジェクト/UIAutomationClientアセンブリ/UIAutomationTypesアセンブリへの参照を追加する必要がある。

 あとは、WCFの場合と全く同じ単体テスト・プロジェクトを使ってテストを実行すると、受け入れテストが実施される。

 次にCodeer.Friendlyを使う方法を説明しよう。

Copyright© Digital Advantage Corp. All Rights Reserved.

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。