UI 自动化测试库
UI 自动化测试库(UIA 测试库)是自动化测试方案中由 驱动程序 应用程序调用的 API。 驱动程序是从需要验证的控件获取自动化元素(IUIAutomationElement 对象),并将其提供给 UI 自动化测试库的应用程序。 然后,测试库会检查并验证 UI 自动化实现。 若要了解有关 UI 自动化和自动化元素的详细信息,请参阅 UI 自动化基础知识。
- UI 自动化测试库工作流
- 使用 UI 自动化测试库 日志记录
UI 自动化测试库工作流
下图显示了一个使用控制台应用程序作为驱动程序合并 UI 自动化测试库的测试工作流。 在这种情况下,驱动程序启动 Notepad.exe 实例,并从编辑控件获取自动化元素(即 IUIAutomationElement 对象)。 接下来,驱动程序基于要测试的 UI 平台创建 UI 自动化测试库对象,然后将自动化元素作为参数传入。 UI 自动化测试库确定自动化元素是 文档 控件类型,然后执行文档控件测试、文本 和 Scroll 控件模式测试和泛型自动化元素测试。
使用 UI 自动化测试库进行日志记录
UI 自动化测试库使用外部 DLL 记录测试运行的结果。 它支持将日志记录为 XML 并日志记录到控制台。
XML 日志记录
XML 日志记录通常由 Visual UI 自动化验证使用,但它也可以合并到命令行工作流中。
如果指定了 XML 日志记录,驱动程序应用程序必须通过创建 XmlWriter 对象并将其传递给 XmlLog.GetTestRunXml 方法来序列化输出。 然后,驱动程序可以在内部使用序列化的 XML,或将其写入文件。
XML 日志记录需要以下 DLL。
- WUIALogging.dll
- WUIALoggerXml.dll
控制台日志记录
默认情况下,UI 自动化测试库使用控制台日志记录,其中所有日志记录输出都以纯文本的形式传递给控制台窗口。 控制台日志记录需要 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);
}
}
}