Testbibliothek für die Benutzeroberflächenautomatisierung
Die Benutzeroberflächenautomatisierungs-Testbibliothek (UIA Test Library) ist eine API, die vom Treiber Anwendung in einem automatisierten Testszenario aufgerufen wird. Der Treiber ist die Anwendung, die ein Automatisierungselement (IUIAutomationElement -Objekt) aus einem Steuerelement abruft, das eine Überprüfung erfordert, und stellt es der Benutzeroberflächenautomatisierungs-Testbibliothek bereit. Anschließend überprüft und überprüft die Testbibliothek die Implementierung der Benutzeroberflächenautomatisierung. Weitere Informationen zu Benutzeroberflächenautomatisierungs- und Automatisierungselementen finden Sie unter Grundlagen der Benutzeroberflächenautomatisierung.
- Testbibliotheksworkflow für die Benutzeroberflächenautomatisierung
- Protokollierung mit der Testbibliothek für die Benutzeroberflächenautomatisierung
Testbibliotheksworkflow für die Benutzeroberflächenautomatisierung
Das folgende Diagramm zeigt einen Testworkflow, der die Testbibliothek für die Benutzeroberflächenautomatisierung mithilfe einer Konsolenanwendung als Treiber enthält. In diesem Fall startet der Treiber eine Instanz von Notepad.exe und ruft ein Automatisierungselement (d. h. ein IUIAutomationElement -Objekt) aus dem Bearbeitungssteuerelement ab. Als Nächstes erstellt der Treiber ein Testbibliotheksobjekt für die Benutzeroberflächenautomatisierung, das auf der getesteten UI-Plattform basiert, und übergibt dann das Automatisierungselement als Parameter. Die Benutzeroberflächenautomatisierungs-Testbibliothek bestimmt, dass das Automatisierungselement ein Document Steuerelementtyp ist, und führt dann die Tests des Dokumentsteuerelements, die Text- und Scroll Steuerelementmustertests und allgemeine Automatisierungselementtests aus.
Protokollierung mit der Testbibliothek für die Benutzeroberflächenautomatisierung
Die Testbibliothek für die Benutzeroberflächenautomatisierung verwendet externe DLLs, um Ergebnisse aus Testläufen zu protokollieren. Sie unterstützt die Protokollierung als XML und protokollierung in der Konsole.
XML-Protokollierung
Die XML-Protokollierung wird in der Regel von Visual UI Automation Verify verwendet, kann aber auch in einen Befehlszeilenworkflow integriert werden.
Wenn die XML-Protokollierung angegeben ist, muss die Treiberanwendung die Ausgabe serialisieren, indem ein XmlWriter--Objekt erstellt und an die XmlLog.GetTestRunXml-Methode übergeben wird. Der Treiber kann dann die serialisierte XML intern verwenden oder in eine Datei schreiben.
Die folgenden DLLs sind für die XML-Protokollierung erforderlich.
- WUIALogging.dll
- WUIALoggerXml.dll
Konsolenprotokollierung
Standardmäßig verwendet die Testbibliothek für die Benutzeroberflächenautomatisierung die Konsolenprotokollierung, in der alle Protokollierungsausgaben als Nur-Text an das Konsolenfenster weitergeleitet werden. WUIALogging.dll ist für die Konsolenprotokollierung erforderlich.
Codeanforderungen für die Protokollierung
Eine Treiberanwendung muss die folgenden Codeausschnitte enthalten, um die Protokollierung der Benutzeroberflächenautomatisierungs-Testbibliothek zu verwenden.
\\ 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("...");
Protokollierungsbeispiele
Die folgenden Beispiele veranschaulichen grundlegende Protokollierungsfunktionen und zeigen, wie sie die Testbibliotheks-API für die Benutzeroberflächenautomatisierung in einer einfachen Testautomatisierungs-Treiberanwendung verwenden.
//---------------------------------------------------------------------------
//
// 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);
}
}
}