所需文件
- WinAppDriver服务器软件阿里云盘链接
- 官方的组件元素识别工具,inspect.exe 集成于 Windows SDK如果本地不存在该文件,可以通过链接进行安装阿里云盘链接
- UIRecorder UI录制器 CSDN下载地址
WinAppDriver UI测试自动化
启动服务
- 打开
C:\Program Files\Windows Application Driver
在文件路径输入cmd按回车键 - 在cmd框输入:WinAppDriver.exe 4727,然后不要关闭窗口 ,这样WinAppDriver 服务就算器启动好了。
编写代码
- 可以参考微软提供的示例 Girhub链接
添加截图,记录测试过程中重要步骤
1)添加WinDriverUtil.cs文件,捕获当前session点击NotepadTest->右键Add->New Item->WinDriverUtil.cs
using NLog;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotepadTest
{
public class WinDriverUtil
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
private static WindowsDriver<WindowsElement> _desktopSession = null;
/// <summary>
/// Url to communicate with Windows Application Driver
/// </summary>
public const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
private static uint _remainCount = 0;
private const uint _refreshCount = 7;
private static readonly TimeSpan _commandTimeOut = new TimeSpan(0, 1, 30);
/// <summary>
/// Obtain current desktop session
/// </summary>
public static WindowsDriver<WindowsElement> DesktopSession
{
get
{
if (_remainCount == 0)
{
// Dispose the old session
_desktopSession?.Dispose();
_remainCount = _refreshCount;
var desktopDesiredCapabilities = new DesiredCapabilities();
desktopDesiredCapabilities.SetCapability("app", "Root");
// Assign new session
_desktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl),
desktopDesiredCapabilities, _commandTimeOut);
}
--_remainCount;
return _desktopSession;
}
}
/// <summary>
/// Shot the session part screen and save it with full file name
/// </summary>
/// <param name="session">Session you want to shot</param>
/// <param name="fullFileName">File name for image</param>
public static void ShotScreen(WindowsDriver<WindowsElement> session, string fullFileName)
{
session.GetScreenshot().SaveAsFile(fullFileName, ScreenshotImageFormat.Png);
}
}
}
2)打开ScenarioEditor.cs,在测试方法EditorEnterText()中添加:
//地址可自行修改
WinDriverUtil.ShotScreen(WinDriverUtil.DesktopSession, @"D:\Improvement\PrepareForTestReportTeacher\NotepadTest\TestResults\screen.png");
关于自动化视频录制截图
为该项目添加视频录制功能,记录测试视频
(1)添加ScreenRecorder.cs
点击NotepadTest->右键Add->New Item->ScreenRecorder.cs
见github:
在添加该文件时有可能会有错误,缺少依赖
解决办法:
点击Reference—》右键Manage Nuget Package ->搜索ScreenRecorderLib
选择合适的版本安装
(2)打开ScenarioEditor.cs,在测试方法:EditorEnterText()中,添加:
//地址可自行修改
var screenRecorder = ScreenRecorder.CreateRecorder(@"D:\Improvement\PrepareForTestReportTeacher\NotepadTest\TestResults\screenRecorder.mp4");
screenRecorder.StartRecording(screenRecorder, 2);
…
_logger.Info("end recordering");
screenRecorder.EndRecording();
版权归原作者 ~空中楼阁 所有, 如有侵权,请联系我们删除。