文章目录
- 关于
- 1 环境准备
- 2 集成单元测试
- 3 新增基本测试代码
- 4 测试 WPF 程序
- 5 启动测试
关于
参考链接:Get Your WPF Apps Automated With Appium
Appium官网:http://appium.io/docs/en/2.0/quickstart
1 环境准备
一、下载 Windows Application Driver,该驱动可以用于测试 UWP、WPF、WinForm、Win32 程序
二、下载 WinAppDriver 源码,具体的一些内容可以参考源码中的示例
如果需要自己编译 UIRecorder(后面使用到的元素选择器),可以打开 WinAppDriver 源码进行编译,或者你可以用我已经编译好的 exe 文件
UIRecorder:点此下载
编译时间:2023-05-09
编译平台:Visual Studio 2022
git branch: master
git commit: 9c561a5
三、打开系统的 开发者模式
打开 Window 设置,搜索 developer
,启用开发者模式
2 集成单元测试
新建一个单元测试项目 MyAPP
给该项目添加 nuget 包 Appium.WebDriver
3 新增基本测试代码
新建类 TestsBase
,新增方法 StartWinAppDriver 和 Initialize
public class TestsBase
{
private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
private const string ApplicationPath = @"..\..\..\..\MyAPP\bin\Debug\\MyAPP.exe";
private const string DeviceName = "WindowsPC";
private const int WaitForAppLaunch = 5;
private const string WinAppDriverPath = @"C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe";
private static Process winAppDriverProcess;
public WindowsDriver<WindowsElement> AppSession { get; private set; }
public WindowsDriver<WindowsElement> DesktopSession { get; private set; }
private static void StartWinAppDriver()
{
ProcessStartInfo psi = new ProcessStartInfo(WinAppDriverPath);
psi.UseShellExecute = true;
psi.Verb = "runas"; // run as administrator
winAppDriverProcess = Process.Start(psi);
}
public void Initialize()
{
StartWinAppDriver();
var appiumOptions = new AppiumOptions();
appiumOptions.AddAdditionalCapability("app", ApplicationPath);
appiumOptions.AddAdditionalCapability("deviceName", DeviceName);
appiumOptions.AddAdditionalCapability("ms:waitForAppLaunch", WaitForAppLaunch);
this.AppSession =
new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
Assert.IsNotNull(AppSession);
Assert.IsNotNull(AppSession.SessionId);
AppSession.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);
AppiumOptions optionsDesktop = new AppiumOptions();
optionsDesktop.AddAdditionalCapability("app", "Root");
optionsDesktop.AddAdditionalCapability("deviceName", DeviceName);
DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), optionsDesktop);
}
}
新增方法
public void Cleanup()
{
// Close the session
if (AppSession != null)
{
AppSession.Close();
AppSession.Quit();
}
// Close the desktopSession
if (DesktopSession != null)
{
DesktopSession.Close();
DesktopSession.Quit();
}
}
public static void StopWinappDriver()
{
// Stop the WinAppDriverProcess
if (winAppDriverProcess != null)
{
foreach (var process in Process.GetProcessesByName("WinAppDriver"))
{
process.Kill();
}
}
}
新增一些操作元素的基本方法
protected void SelectAllText()
{
Actions action = new Actions(AppSession);
action.KeyDown(Keys.Control).SendKeys("a");
action.KeyUp(Keys.Control);
action.Perform();
}
protected void PerformDelete()
{
Actions action = new Actions(AppSession);
action.SendKeys(Keys.Delete);
action.Perform();
}
protected void PerformEnter()
{
Actions action = new Actions(AppSession);
action.SendKeys(Keys.Enter);
action.Perform();
}
protected void WriteText(string text)
{
Actions action = new Actions(AppSession);
action.SendKeys(text);
action.Perform();
}
public WindowsElement FindByName(string name)
{
return this.AppSession.FindElementByName(name);
}
public WindowsElement FindById(string id)
{
return this.AppSession.FindElementByAccessibilityId(id);
}
4 测试 WPF 程序
新建类 UnitTest
,继承自 TestBase
[TestClass]
public class UnitTest : TestBase
{
private const string BUTTON_NEW_DATA_BASE = "newDataBaseButton";
[TestInitialize]
public void TestInitialize()
{
this.Initialize();
}
[TestCleanup]
public void TestCleanup()
{
this.Cleanup();
}
[ClassCleanup]
public static void ClassCleanusp()
{
StopWinappDriver();
}
[TestMethod]
[DataRow("我的库")]
public void CreateDataBase(string dbName)
{
WindowsElement btn = this.FindById(BUTTON_NEW_DATA_BASE);
Assert.IsNotNull(btn);
btn.Click();
Thread.Sleep(1000);
this.WriteText(dbName);
this.PerformEnter();
}
}
然后打开我们的 WPF 主窗体的 xaml 代码
新增一个按钮,添加属性 AutomationProperties.AutomationId
,和上面的 BUTTON_NEW_DATA_BASE 相对应
<Button
MinWidth="100"
AutomationProperties.AutomationId="newDataBaseButton" />
5 启动测试
在 Visual Studio 2022 中,点击视图-测试资源管理器
找到我们新增的 UnitTest 类,右键运行,结果如下