次の方法で共有


UI オートメーション テスト ライブラリ

UI オートメーション テスト ライブラリ (UIA テスト ライブラリ) は、自動化されたテスト シナリオで ドライバー アプリケーションによって呼び出される API です。 ドライバーは、検証を必要とするコントロールからオートメーション要素 (IUIAutomationElement オブジェクト) を取得し、UI オートメーション テスト ライブラリに提供するアプリケーションです。 次に、テスト ライブラリは UI オートメーションの実装を確認して検証します。 UI オートメーションとオートメーション要素の詳細については、UI オートメーションの基礎 を参照してください。

UI オートメーション のテスト ライブラリ ワークフロー

次の図は、コンソール アプリケーションをドライバーとして使用して UI オートメーション テスト ライブラリを組み込むテスト ワークフローを示しています。 この場合、ドライバーは Notepad.exe のインスタンスを起動し、編集コントロールからオートメーション要素 (つまり、IUIAutomationElement オブジェクト) を取得します。 次に、ドライバーは、テスト対象の UI プラットフォームに基づいて UI オートメーション テスト ライブラリ オブジェクトを作成し、パラメーターとしてオートメーション要素を渡します。 UI オートメーション テスト ライブラリは、オートメーション要素が Document コントロール型であると判断し、ドキュメント コントロール テスト、テキスト および スクロール コントロール パターン テスト、および汎用オートメーション要素テストを実行します。

赤い矢印を使用してドライバーから UIATestLibrary へのドライバー間のフローを示す図。

UI オートメーション テスト ライブラリを使用したログ記録

UI オートメーション テスト ライブラリでは、外部 DLL を使用してテスト実行の結果をログに記録します。 XML としてのログ記録とコンソールへのログ記録がサポートされています。

XML ログ

XML ログは通常、Visual UI Automation Verify によって使用されますが、コマンド ライン ワークフローに組み込むこともできます。

XML ログが指定されている場合、ドライバー アプリケーションは、XmlWriter オブジェクトを作成し、XmlLog.GetTestRunXml メソッドに渡すことによって、出力をシリアル化する必要があります。 その後、ドライバーは、シリアル化された XML を内部的に使用するか、ファイルに書き込むことができます。

XML ログ記録には、次の DLL が必要です。

  • WUIALogging.dll
  • WUIALoggerXml.dll

コンソールログ

既定では、UI オートメーション テスト ライブラリではコンソール ログが使用され、すべてのログ出力はプレーン テキストとしてコンソール ウィンドウにパイプされます。 コンソールのログ記録には WUIALogging.dll が必要です。

ログ記録のコード要件

UI オートメーション テスト ライブラリのログ記録を使用するには、ドライバー アプリケーションに次のコード スニペットを含める必要があります。

\\ Include logging functionality.
using Microsoft.Test.UIAutomation.Logging;

...

\\ Select a logger.
UIAVerifyLogger.SetLoggerType(LogTypes.DefaultLogger | 
                              LogTypes.ConsoleLogger | 
                              LogTypes.XmlLogger);

...

\\ Output comment to selected logger.
UIAVerifyLogger.LogComment("...");

ログ記録の例

次の例では、基本的なログ機能を示し、基本的なテスト オートメーション ドライバー アプリケーションで UI オートメーション テスト ライブラリ API を使用する方法を示します。

//---------------------------------------------------------------------------
//
// Description: Sample logger.
//
//---------------------------------------------------------------------------
using System;

namespace WUITest
{
    using Microsoft.Test.UIAutomation.Logging;

    public sealed class TestMain
    {
        private TestMain() { }

        /// -----------------------------------------------------------------
        /// <summary>
        /// Entry point
        /// </summary>
        /// -----------------------------------------------------------------
        [STAThread]
        static void Main(string[] args)
        {
            // Call SetLogger() if you don't want to use the default logger.
            // To set the logger type, call SetLogger(<string>).
            // <string> can be specified from the command line or from the 
            // the UI Automation Test Library enumeration:
            //  
            //     Logger.SetLogger(LogTypes.ConsoleLogger);
            //     Logger.SetLogger(LogTypes.DefaultLogger);

            Logger.SetLogger(LogTypes.DefaultLogger);

            Logger.StartTest("Test 1");
            Logger.LogComment("This is a comment");
            Logger.LogError(new Exception("My error"), false);
            Logger.EndTest();

            Logger.StartTest("Test 2");
            Logger.LogComment("This is a second comment");
            Logger.LogPass();
            Logger.EndTest();

            Logger.ReportResults();

        }
    }
}
//---------------------------------------------------------------------------
//
// Description: Sample test automation.
//
//---------------------------------------------------------------------------

using System;
using System.Windows;

namespace WUITest
{
    using System.Diagnostics;
    using System.Threading;
    using System.Windows.Automation;
    using Microsoft.Test.UIAutomation;
    using Microsoft.Test.UIAutomation.Core;
    using Microsoft.Test.UIAutomation.TestManager;
    using Microsoft.Test.UIAutomation.Tests.Controls;
    using Microsoft.Test.UIAutomation.Tests.Patterns;
    using Microsoft.Test.UIAutomation.Tests.Scenarios;
    using Microsoft.Test.UIAutomation.Logging;

    public sealed class TestMain
    {

        // Time in milliseconds to wait for the application to start.
        static int MAXTIME = 5000;
        // Time in milliseconds to wait before trying to find the application.
        static int TIMEWAIT = 100; 

        /// -------------------------------------------------------------------
        /// <summary>
        /// Start Notepad, obtain an AutomationElement object, and run tests.
        /// </summary>
        /// -------------------------------------------------------------------
        [STAThread]
        static void Main(string[] args)
        {
            // Dump the information to the console window.  
            // Use a different LogTypes value if you need to dump to another logger, 
            // or create your own logger that complies with the interface.
            UIAVerifyLogger.SetLoggerType(LogTypes.ConsoleLogger);

            // Get the automation element.
            AutomationElement element = StartApplication("NOTEPAD.EXE", null);

            // Call the UI Automation Test Library tests.
            TestRuns.RunAllTests(element, true, TestPriorities.Pri0, 
                    TestCaseType.Generic, false, true, null);

            // Clean up.
            ((WindowPattern)element.GetCurrentPattern(WindowPattern.Pattern)).Close();

            // Dump the summary of results.
            UIAVerifyLogger.ReportResults();
        }

        /// -------------------------------------------------------------------------
        /// <summary>
        /// Start the application and retrieve its AutomationElement. 
        /// </summary>
        /// -------------------------------------------------------------------------
        static public AutomationElement StartApplication(string appPath, 
                string arguments)
        {
            Process process;

            Library.ValidateArgumentNonNull(appPath, "appPath");

            ProcessStartInfo psi = new ProcessStartInfo();

            process = new Process();
            psi.FileName = appPath;

            if (arguments != null)
            {
                psi.Arguments = arguments;
            }

            UIAVerifyLogger.LogComment("Starting({0})", appPath);
            process.StartInfo = psi;

            process.Start();

            int runningTime = 0;
            while (process.MainWindowHandle.Equals(IntPtr.Zero))
            {
                if (runningTime > MAXTIME)
                    throw new Exception("Could not find " + appPath);

                Thread.Sleep(TIMEWAIT);
                runningTime += TIMEWAIT;

                process.Refresh();
            }

            UIAVerifyLogger.LogComment("{0} started", appPath);

            UIAVerifyLogger.LogComment("Obtained an AutomationElement for {0}", appPath);
            return AutomationElement.FromHandle(process.MainWindowHandle);

        }
    }
}