Bagikan melalui


Pustaka Pengujian Automasi UI

UI Automation Test Library (UIA Test Library) adalah API yang dipanggil oleh aplikasi driver dalam skenario pengujian otomatis. Driver adalah aplikasi yang mendapatkan elemen otomatisasi (objek IUIAutomationElement) dari kontrol yang memerlukan verifikasi, dan menyediakannya ke Pustaka Pengujian Automasi UI. Kemudian, pustaka pengujian memeriksa dan memvalidasi implementasi Automation UI. Untuk mempelajari selengkapnya tentang elemen Automasi dan otomatisasi UI, lihat Dasar-Dasar Automasi UI.

Alur Kerja Pustaka Uji Automasi UI

Diagram berikut menunjukkan alur kerja pengujian yang menggabungkan Pustaka Pengujian Automasi UI dengan menggunakan aplikasi konsol sebagai driver. Dalam hal ini, driver memulai instans Notepad.exe dan mendapatkan elemen otomatisasi (yaitu, objekIUIAutomationElement) dari kontrol edit. Selanjutnya, driver membuat objek Pustaka Uji Automasi UI berdasarkan platform UI yang sedang diuji, lalu meneruskan elemen otomatisasi sebagai parameter. Pustaka Pengujian Automasi UI menentukan bahwa elemen otomatisasi adalah jenis kontrol Dokumen, lalu menjalankan pengujian kontrol Dokumen, Teks dan Gulir pengujian pola kontrol, dan pengujian elemen otomatisasi generik.

Diagram yang menunjukkan alur Driver ke Aplikasi ke Driver ke UIATestLibrary menggunakan panah merah.

Pengelogan dengan Pustaka Pengujian Automasi UI

Pustaka Pengujian Automasi UI menggunakan DLL eksternal untuk mencatat hasil dari uji coba. Ini mendukung pengelogan sebagai XML dan pengelogan ke konsol.

Pengelogan XML

Pengelogan XML biasanya digunakan oleh Visual UI Automation Verify, tetapi juga dapat dimasukkan ke dalam alur kerja baris perintah.

Jika pengelogan XML ditentukan, aplikasi driver harus membuat serial output dengan membuat objek XmlWriter dan meneruskannya ke metode XmlLog.GetTestRunXml. Driver kemudian dapat menggunakan XML berseri secara internal atau menulisnya ke file.

DLL berikut diperlukan untuk pengelogan XML.

  • WUIALogging.dll
  • WUIALoggerXml.dll

Pengelogan Konsol

Secara default, Pustaka Pengujian Automasi UI menggunakan pengelogan konsol, di mana semua output pengelogan disalurkan sebagai teks biasa ke jendela konsol. WUIALogging.dll diperlukan untuk pengelogan konsol.

Persyaratan Kode untuk Pengelogan

Aplikasi driver harus menyertakan cuplikan kode berikut untuk menggunakan pengelogan Pustaka Pengujian Automasi 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("...");

Contoh Pengelogan

Contoh berikut menunjukkan fungsionalitas pengelogan dasar dan menunjukkan cara menggunakan UI Automation Test Library API dalam aplikasi driver otomatisasi pengujian dasar.

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

        }
    }
}