libreria di test Automazione interfaccia utente

La libreria di test Automazione interfaccia utente (libreria di test UIA) è un'API chiamata dall'applicazione driver in uno scenario di test automatizzato. Il driver è l'applicazione che ottiene un elemento di automazione (oggetto IUIAutomationElement) da un controllo che richiede la verifica e lo fornisce alla libreria di test Automazione interfaccia utente. Quindi, la libreria di test controlla e convalida l'implementazione del Automazione interfaccia utente. Per altre informazioni sugli elementi di Automazione interfaccia utente e automazione, vedere Automazione interfaccia utente Fundamentals.To learn more about Automazione interfaccia utente and automation elements, see Automazione interfaccia utente Fundamentals.To learn more about Automazione interfaccia utente and automation elements, see Automazione interfaccia utente Fundamentals.

Flusso di lavoro della libreria di test Automazione interfaccia utente

Il diagramma seguente mostra un flusso di lavoro di test che incorpora la libreria di test Automazione interfaccia utente usando un'applicazione console come driver. In questo caso, il driver avvia un'istanza di Notepad.exe e ottiene un elemento di automazione (ovvero un oggetto IUIAutomationElement ) dal controllo di modifica. Successivamente, il driver crea un oggetto libreria di test Automazione interfaccia utente in base alla piattaforma dell'interfaccia utente sottoposta a test e quindi passa l'elemento di automazione come parametro. La libreria di test Automazione interfaccia utente determina che l'elemento di automazione è un tipo di controllo Document e quindi esegue i test del controllo Document, i test del pattern di controllo Text e Scroll e i test generici degli elementi di automazione.

Diagramma che mostra il flusso del driver da applicazione a driver a UIATestLibrary usando frecce rosse.

Registrazione con la libreria di test di Automazione interfaccia utente

La libreria di test Automazione interfaccia utente usa DLL esterne per registrare i risultati delle esecuzioni di test. Supporta la registrazione come XML e la registrazione nella console.

Registrazione XML

La registrazione XML viene in genere usata da Visual Automazione interfaccia utente Verify, ma può anche essere incorporata in un flusso di lavoro della riga di comando.

Se viene specificata la registrazione XML, l'applicazione driver deve serializzare l'output creando un oggetto XmlWriter e passandolo al metodo XmlLog.GetTestRunXml . Il driver può quindi usare il codice XML serializzato internamente o scriverlo in un file.

Per la registrazione XML sono necessarie le DLL seguenti.

  • WUIALogging.dll
  • WUIALoggerXml.dll

Registrazione della console

Per impostazione predefinita, la libreria di test di Automazione interfaccia utente usa la registrazione della console, in cui tutto l'output di registrazione viene inviato tramite pipe come testo normale alla finestra della console. WUIALogging.dll è necessario per la registrazione della console.

Requisiti del codice per la registrazione

Un'applicazione driver deve includere i frammenti di codice seguenti per usare Automazione interfaccia utente registrazione della libreria di test.

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

Esempi di registrazione

Gli esempi seguenti illustrano la funzionalità di registrazione di base e illustrano come usare l'API libreria di test Automazione interfaccia utente in un'applicazione di base del driver di automazione dei test.

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

        }
    }
}