共用方式為


UI 自動化測試連結庫

UI 自動化測試連結庫 (UIA 測試連結庫) 是自動化測試案例中 驅動程式 應用程式所呼叫的 API。 驅動程式是應用程式,可從需要驗證的控件取得自動化專案(IUIAutomationElement 物件),並將其提供給 UI 自動化測試連結庫。 然後,測試連結庫會檢查並驗證UI自動化實作。 若要深入瞭解使用者介面自動化和自動化元素,請參閱 UI 自動化基本概念

UI 自動化測試連結庫工作流程

下圖顯示使用主控台應用程式做為驅動程式,併入UI自動化測試連結庫的測試工作流程。 在此情況下,驅動程式會啟動 Notepad.exe 實例,並從編輯控件取得自動化專案(也就是 IUIAutomationElement 物件)。 接下來,驅動程式會根據所測試的UI平臺建立UI自動化測試連結庫物件,然後將自動化專案當做參數傳入。 使用者介面自動化測試連結庫會判斷自動化元素是 Document 控件類型,然後執行檔控件測試、文字Scroll 控件模式測試,以及泛型自動化元素測試。

圖表,其中顯示使用紅色箭號將驅動程式到應用程式到驅動程式到 UIATestLibrary 的流程。

使用UI自動化測試連結庫進行記錄

使用者介面自動化測試連結庫會使用外部 DLL 來記錄測試回合的結果。 它支援以 XML 身分記錄和記錄至主控台。

XML 記錄

XML 記錄通常是由 Visual UI Automation Verify 使用,但也可以併入命令行工作流程。

如果指定 XML 記錄,驅動程式應用程式必須藉由建立 XmlWriter 物件,並將其傳遞至 XmlLog.GetTestRunXml 方法,以串行化輸出。 驅動程式接著可以在內部使用串行化 XML,或將它寫入檔案。

XML 記錄需要下列 DLL。

  • WUIALogging.dll
  • WUIALoggerXml.dll

主控台記錄

根據預設,使用者介面自動化測試連結庫會使用主控台記錄,其中所有記錄輸出都會以純文本傳送至控制台視窗。 主控台記錄需要 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);

        }
    }
}