Share via


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

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

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

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

赤い矢印を使用した Driver to Application to Driver to UIATestLibrary のフローを示す図。

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

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

XML ログ

XML ログは通常、Visual UI オートメーション 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);

        }
    }
}