Skip to content

Instantly share code, notes, and snippets.

@munyabe
Created July 26, 2013 02:29
Show Gist options
  • Save munyabe/6085611 to your computer and use it in GitHub Desktop.
Save munyabe/6085611 to your computer and use it in GitHub Desktop.
Automatic GUI test sample by Microsoft's UIAutomation library.
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace GUITest
{
[TestClass]
public class CalcTest
{
[TestMethod]
public void AddTest()
{
using (var app = Process.Start(@"calc.exe"))
{
Thread.Sleep(500);
var aeForm = AutomationElement.FromHandle(app.MainWindowHandle);
ClickButton(aeForm, "131");
ClickButton(aeForm, "93");
ClickButton(aeForm, "132");
ClickButton(aeForm, "121");
var result = GetText(aeForm, "150");
Assert.AreEqual("3", result);
app.CloseMainWindow();
}
}
public static void ClickButton(AutomationElement rootElement, string automationId)
{
var button = FindElement(rootElement, automationId);
var pattern = GetPattern<InvokePattern>(button);
if (pattern != null)
{
pattern.Invoke();
// MEMO : 目視で動作していることを判断するため
Thread.Sleep(500);
}
else
{
throw new InvalidOperationException(string.Format("This element [{0}] is not InvokePattern.", button));
}
}
public static AutomationElement FindElement(AutomationElement rootElement, string automationId)
{
return rootElement.FindFirst(
TreeScope.Element | TreeScope.Descendants,
new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
}
public static T GetPattern<T>(AutomationElement element) where T : BasePattern
{
var pattern = typeof(T).GetField("Pattern").GetValue(null) as AutomationPattern;
return element.GetCurrentPattern(pattern) as T;
}
public static string GetText(AutomationElement rootElement, string automationId)
{
var text = FindElement(rootElement, automationId);
return text.Current.Name;
}
//AutomationId : 122, Name : メモリのクリア
//AutomationId : 83, Name : バックスペース
//AutomationId : 137, Name : 7
//AutomationId : 134, Name : 4
//AutomationId : 131, Name : 1
//AutomationId : 130, Name : 0
//AutomationId : 123, Name : メモリ呼び出し
//AutomationId : 82, Name : 入力のクリア
//AutomationId : 138, Name : 8
//AutomationId : 135, Name : 5
//AutomationId : 132, Name : 2
//AutomationId : 124, Name : メモリ保存
//AutomationId : 81, Name : クリア
//AutomationId : 139, Name : 9
//AutomationId : 136, Name : 6
//AutomationId : 133, Name : 3
//AutomationId : 84, Name : 小数点
//AutomationId : 125, Name : メモリ加算
//AutomationId : 80, Name : 符号切り替え
//AutomationId : 91, Name : 除算
//AutomationId : 92, Name : 乗算
//AutomationId : 94, Name : 減算
//AutomationId : 93, Name : 加算
//AutomationId : 126, Name : メモリ減算
//AutomationId : 110, Name : 平方根
//AutomationId : 118, Name : パーセント
//AutomationId : 114, Name : 逆数
//AutomationId : 121, Name : 等号
//AutomationId : Minimize, Name : 最小化
//AutomationId : Maximize, Name : 最大化
//AutomationId : Close, Name : 閉じる
//AutomationId : 404, Name : 実行履歴
//AutomationId : 401, Name : メモリ
//AutomationId : 150, Name : 結果?
//AutomationId : 158, Name : 結果?
//System.Windows.Automation.Peers
//AutomationInteropProvider
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment