Aracılığıyla paylaş


UI Otomasyonu Test Kitaplığı

UI Otomasyonu Test Kitaplığı (UIA Test Kitaplığı), otomatikleştirilmiş test senaryosunda sürücü uygulaması tarafından çağrılan bir API'dir. Sürücü, doğrulama gerektiren bir denetimden bir otomasyon öğesi (IUIAutomationElement nesnesi) alan ve UI Otomasyonu Test Kitaplığı'na sağlayan uygulamadır. Ardından, test kitaplığı UI Otomasyonu uygulamasını denetler ve doğrular. UI Otomasyonu ve otomasyon öğeleri hakkında daha fazla bilgi edinmek için bkz. UI Otomasyonu TemelLeri.

  • UI Otomasyonu Test Kitaplığı İş Akışı
  • UI Otomasyonu Test Kitaplığı ile günlüğe kaydetmeyi

UI Otomasyonu Test Kitaplığı İş Akışı

Aşağıdaki diyagramda, sürücü olarak bir konsol uygulaması kullanarak UI Otomasyonu Test Kitaplığı'nı bir arada bulunduran bir test iş akışı gösterilmektedir. Bu durumda, sürücü Notepad.exe örneğini başlatır ve düzenleme denetiminden bir otomasyon öğesi (IUIAutomationElement nesnesi) alır. Ardından, sürücü test edilen UI platformunu temel alan bir UI Otomasyonu Test Kitaplığı nesnesi oluşturur ve otomasyon öğesini parametre olarak geçirir. UI Otomasyonu Test Kitaplığı otomasyon öğesinin bir Belge denetim türü olduğunu belirler ve ardından Belge denetim testlerini, Metin ve Kaydırma denetim düzeni testlerini ve genel otomasyon öğesi testlerini yürütür.

Kırmızı oklar kullanarak Driver to Application to Driver to UIATestLibrary akışını gösteren Diyagramı.

UI Otomasyonu Test Kitaplığı ile günlüğe kaydetme

UI Otomasyonu Test Kitaplığı, test çalıştırmalarının sonuçlarını günlüğe kaydetmek için dış DLL'ler kullanır. XML olarak günlüğe kaydetmeyi ve konsolda günlüğe kaydetmeyi destekler.

XML Günlüğü

XML günlüğü genellikle Visual UI Otomasyonu Doğrulama tarafından kullanılır, ancak komut satırı iş akışına da eklenebilir.

XML günlüğü belirtilirse, sürücü uygulamasının bir XmlWriter nesnesi oluşturup XmlLog.GetTestRunXml yöntemine geçirerek çıktıyı seri hale getirmesi gerekir. Sürücü daha sonra serileştirilmiş XML'yi dahili olarak kullanabilir veya bir dosyaya yazabilir.

XML günlüğü için aşağıdaki DLL'ler gereklidir.

  • WUIALogging.dll
  • WUIALoggerXml.dll

Konsol Günlüğü

Varsayılan olarak, UI Otomasyonu Test Kitaplığı tüm günlük çıkışının konsol penceresine düz metin olarak yöneltildiği konsol günlüğünü kullanır. konsol günlüğü için WUIALogging.dll gereklidir.

Günlük için Kod Gereksinimleri

Bir sürücü uygulaması, UI Otomasyonu Test Kitaplığı günlüğünü kullanmak için aşağıdaki kod parçacıklarını içermelidir.

\\ 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("...");

Günlük Örnekleri

Aşağıdaki örneklerde temel günlüğe kaydetme işlevi gösterilmektedir ve UI Otomasyonu Test Kitaplığı API'sinin temel bir test otomasyonu sürücü uygulamasında nasıl kullanılacağı gösterilmektedir.

//---------------------------------------------------------------------------
//
// 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);

        }
    }
}