WPF的UI测试-环境配置+简单案例

WinAppDriver

GitHub - microsoft/WinAppDriver: Windows Application Driver

使用 Appium 和 WinAppDriver 来测试桌面和 UWP 应用

官方GitHub

GitHub: WinAppDriver/Samples/C#/NotepadTest at master · microsoft/WinAppDriver (github.com)

介绍

Windows 应用程序驱动程序 (WinAppDriver) 是一项服务,用于在 Windows 应用程序上支持类似 Selenium 的 UI 测试自动化。此服务支持在 Windows 10 电脑上测试通用 Windows 平台 (UWP)、Windows 窗体 (WinForms)、Windows Presentation Foundation (WPF)经典 Windows (Win32) 应用。

环境搭建

初次使用WinAppDriver,借助Appium客户端编写自动化测试脚本需要经过安装WinAppDriver>>开启开发人员模式>>安装Appium客户端>>运行WinAppDriver.exe程序>>编写测试脚本,五个步骤。

安装并运行WinAppDriver

  1. Releases · microsoft/WinAppDriver · GitHub 下载 Windows 应用程序驱动程序安装程序

安装完成后,默认在系统的:C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe

这个工具的作用是你写的测试工程,通过本地 localhost(127.0.0.1)以 json 方式与 WinAppDriver 进行通信,使 WinAppDriver调用 win32 api来模拟屏幕操作,如果鼠标点击、拖拽、触屏手势等,后续据说会支持 xbox 手柄、hololens 等操作。

  1. 在安装了受测应用程序的 Windows 10 计算机上运行安装程序,并将对其进行测试
  2. 在 Windows 设置中启用开发人员模式

  1. 从安装目录运行

然后,Windows 应用程序驱动程序将在测试计算机上运行,侦听默认 IP 地址和端口 () 上的请求。然后,您可以运行我们的任何测试示例。 可以配置为侦听不同的 IP 地址和端口,如下所示:127.0.0.1:4723WinAppDriver.exe

WinAppDriver.exe 4727
WinAppDriver.exe 10.0.0.10 4725
WinAppDriver.exe 127.0.0.1 4723/wd/hub

注意:您必须以管理员身份运行才能侦听不同的 IP 地址和端口。WinAppDriver.exe

其中最后一条:WinAppDriver.exe 127.0.0.1 4723/wd/hub ,需要单独拿出来说,因为这是Appium默认的连接配置。
按这个参数启动,Appium创建Session时不需要额外的配置,否则还要改Appium的配置。

Windows SDK 下载

下载地址:windows sdk

本电脑已经下载过了

appium环境搭建

(1)appium的官方文档

Welcome - Appium Documentation

Quickstart Intro - Appium Documentation

(2)根据官方文档,采用Appium的桌面方式进行安装,而且是一键安装

下载地址:Releases · appium/appium-desktop · GitHub

使用Appium连接WinAppDriver

(1)方式一:可以设置的端口号不能与WinAppDriver冲突,然后启动服务器。

方式二:

不要尝试点击“启动服务器”的按钮。4723端口已经被WinAppDriver占掉了。

打开一个新的会话窗口

如果刚才是带参数启动 WinAppDriver 的,那么这里不需要修改设置;
如果没有带参数,而是以默认方式启动 WinAppDriver,那需要把 远程路径 改为“/”

(2)接下来,要配置 所需功能Desired Capabilities 下面的三个框。这里也就是配置测试目标程序的地方。要成功建立会话,这三个框很关键。否则你会发现,不管你怎么配,只会得到两种错误提示:

  • 一种是 HTTP 404,"Could not start session",它的意思是找不到需要测试的目标程序。这个错误的原因是,WinAppDriver监听的路径和Appium中设置的路径不一致。通常情况是,WinAppDriver在监听"/",而Appium用了默认路径"/wd/hub/":
  • 但即使改掉了这个问题,还是会遇到另一个错误,HTTP 405 Method Not Allowed
    "Bad capabilities. Specify either app or appTopLevelWindow to create a session":这个问题的原因是,Appium的右下方,也就是JSON Reprresentation那里的配置,根本不符合Windows App的要求。
  • 我们需要配置左边的三个框,或者手动修改这个JSON

JSON代码是:

{

"app": "F:\\上位机软件\\ProEditorV2.0.1.0-rs240617\\ProEditor.exe",

"deviceName": "WindowsPC",

"platformName": "Windows"

}

最终可以成功打开

元素识别工具

WinApp元素识别工具有很多,例如WinSpy、AccEvent、Inspect。最早的时候使用Inspect工具识别WinApp上的标题栏、菜单栏、工具栏、树视图及数据视图,但是随着技术的发展和人们的需求变化,微软推出了Accessibility Insights工具代替Inspect,在Inspect官网文档中也建议用户使用Accessibility Insights工具。

WinApp控件识别就可借助Accessibility Insights For Windows工具,该工具是微软官网推荐的一个辅助工具,可帮助测试人员轻松获取WinApp元素属性,比Inspect工具功能更丰富、界面更清爽、操作更方便。

作为官方的组件元素识别工具,inspect.exe 集成于 Windows SDK

如果本地不存在该文件,可以通过下面链接进行安装

https://download.microsoft.com/download/4/d/2/4d2b7011-606a-467e-99b4-99550bf24ffc/windowssdk/winsdksetup.exe

相比 inspect.exe,FlaUInspect 界面更简洁,功能更易用( 推荐 )

项目地址:GitHub - FlaUI/FlaUInspect: Inspect tool to inspect UIs from an automation perspective

(1)inspect.exe (这个用来检查获取元素属性的,可能不是很好用)

windows sdk安装路径中,有一个查找元素的组件,叫 inspect.exe,可通过搜索找到,并双击运行。打开如图界面,可以查看元素的名称,和元素支持的点击等操作。位置:C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64

(2)FlaUInspect

下载地址:Release FlaUInspect v1.3.0 · FlaUI/FlaUInspect · GitHub

下载解压后可以看到有个FlaUInspect.exe

注意:FlaUInspect.exe的运行是要依赖与.NET Framework环境的,所以需要安装.NET Framework 4.8

这个已经装过了位置:C:\WINDOWS\Microsoft.NET\Framework

(3)Accessibility Insights For Windows

进入Accessibility Insights工具下载页面Accessibility Insights,选择检查Windows应用程序,然后下载。

下载完成后双击运行,根据提示操作即可完成安装。启动Accessibility Insights For Windows程序

Accessibility Insights For Windows工具主要有三个功能,分别是Live Inspect、FastPass和Troubleshooting。

  • Live Inspect:允许开发人员将鼠标悬停在元素上方或设置键盘焦点来验证应用程序中的元素属性是否正确;
  • FastPass:通过简单的两步操作就可以帮助开发人员在5分钟之内识别常见的、影响较大的可访问性问题;
  • Troubleshooting:允许用户诊断和修复特定的可访问性问题。

WinApp自动化测试中需要借助Accessibility Insights For Windows工具的Live Inspect功能,获取元素属性,对元素进行定位或状态判断。

(1)Live Inspect功能的使用

打开Accessibility Insights For Windows工具。

进入Inspect界面,点击左侧导航栏的【Inspect】图标进入Live Inspect模式,

选择检查对象为元素【Element】,在Inspect界面的菜单栏中,将【What to select:】值选择成Element

(2)Inspect界面的菜单栏

一共有5个菜单。从左至右依次是检查对象、高亮显示、元素实时追踪、加载测试或事件文件。

  • 检查对象(What to select):是一个下拉选择框,选项有Element和Entire app两项。一般情况下,我们选择Element就可以了,在UIA树(UI Automation Tree,简写为UIA Tree)中可以清楚看到每个元素所在的位置。
  • 高亮显示:默认会开启高亮显示,在选择元素或组件时,会在应用程序中高亮显示选择的区域。关闭后选择区域就不会高亮显示。
  • 元素实时追踪:默认是开启状态,会跟随监听的应用程序页面变化而动态显示元素。关闭后不会动态显示应用程序的最新元素。
  • 定时器:内置的一个定时器,用以设置自动检查的延迟时间,自动检查中使用。
  • 加载测试或事件文件:用来加载事先保存的测试文件或事件监控文件。
(3) 查看元素属性

选择应用程序中的元素查看其属性。例如查看记事本中的编辑按钮,首先鼠标悬浮在记事本“编辑”按钮区域,然后鼠标移动到Accessibility Insights For Windows工具中UIA树下【按钮 ‘编辑’】节点,接着左键单击,即可在右侧的【DETAILS】详情区域下看到元素的属性

更多用法参考:FastPass in Accessibility Insights for Windows

简单实践

开始编写测试脚本,参考WinAppDriver/Docs/AuthoringTestScripts.md at master · microsoft/WinAppDriver · GitHub

创建测试项目

  1. 打开 Microsoft Visual Studio 2019

注意:在 Visual Studio 2019 中,请确保安装了可选的 .NET 桌面开发工作负载

  1. 创建测试项目和解决方案。即,在 Visual C# >> 单元测试项目>选择“新建项目”>模板
  2. 创建后,选择“项目”>“管理 NuGet 包...”> 浏览并搜索 Appium.WebDriver
  3. 安装测试项目的 Appium.WebDriver NuGet 包
  4. 开始编写测试(请参阅示例下的示例代码)

添加被测程序

(1)通过程序路径指定应用程序,例如指定写字板程序路径

 private const string NotepadAppId = @"C:\Windows\System32\notepad.exe";

(2)通过程序ID指定应用程序,下例为指定 闹钟时钟ID

private const string AlarmClockAppId = "Microsoft.WindowsAlarms_8wekyb3d8bbwe!App";

应用程序ID(AppId)

应用程序ID(AppId)是应用程序用户模型 ID (AppUserModelID),简称 AUMID。在Windows PowerShell中输入命令Get-StartApps便可看到当前用户安装的所有应用名称和 AUMID

测试代码

这段代码展示了如何使用 Appium 的 WindowsDriver 和 MSTest 框架来自动化测试 Windows 应用程序(记事本)。它包括设置测试环境、清理测试环境、初始化和清空文本框等操作,以确保每个测试方法都在干净的测试环境中执行。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium;
using System;
using System.Diagnostics;

/*
Microsoft.VisualStudio.TestTools.UnitTesting:引入了用于编写单元测试的 MSTest 框架的命名空间。
OpenQA.Selenium.Appium.Windows:引入了 Appium 的 Windows 驱动,用于与 Windows 应用程序进行交互。
OpenQA.Selenium.Remote 和 OpenQA.Selenium:引入了 Selenium WebDriver 的基本类和接口。
System 和 System.Diagnostics:一些基本的 .NET 框架命名空间,用于系统级操作和调试输出。
*/

namespace NotepadTest
{
    public class NotepadSession
    {
        protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
        private const string NotepadAppId = @"C:\Windows\System32\notepad.exe";

        protected static WindowsDriver<WindowsElement> session;
        protected static WindowsElement editBox;
/*
NotepadSession 类定义了一个自动化测试会话的基本结构。
WindowsApplicationDriverUrl 是 Appium Windows 驱动服务器的地址。
NotepadAppId 是要测试的记事本应用程序的路径。
session 是一个静态的 WindowsDriver<WindowsElement> 对象,用于管理应用程序的会话。
editBox 是一个静态的 WindowsElement 对象,代表记事本中的文本编辑框。
*/
        public static void Setup(TestContext context)
        {
            // Launch a new instance of Notepad application
            if (session == null)
            {
                // Create a new session to launch Notepad application
                DesiredCapabilities appCapabilities = new DesiredCapabilities();
                appCapabilities.SetCapability("app", NotepadAppId);
                session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
                Assert.IsNotNull(session);
                Assert.IsNotNull(session.SessionId);

                // Verify that Notepad is started with untitled new file
                
                Assert.AreEqual("无标题 - 记事本", session.Title);
                //Debug.WriteLine($"Expected: <无标题-记事本>, Actual: {editBox.Text}");

                // Set implicit timeout to 1.5 seconds to make element search to retry every 500 ms for at most three times
                session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);

                // Keep track of the edit box to be used throughout the session
                editBox = session.FindElementByClassName("Edit");
                Assert.IsNotNull(editBox);
            }
        }
/*
Setup 方法用于设置测试环境,确保记事本应用程序已经启动并且初始化了测试所需的元素。
如果 session 为 null,则创建一个新的 WindowsDriver<WindowsElement> 实例来启动记事本应用程序。
使用 DesiredCapabilities 设置应用程序的路径。
确保会话和会话 ID 不为空。
验证记事本应用程序是否以 "无标题 - 记事本" 的标题启动。
设置隐式超时为 1.5 秒,以便在元素搜索时进行重试。
查找记事本中的文本编辑框元素,并将其存储在 editBox 变量中。
*/

        public static void TearDown()
        {
            // Close the application and delete the session
            if (session != null)
            {
                session.Close();

                try
                {
                    // Dismiss Save dialog if it is blocking the exit
                    session.FindElementByName("Don't Save").Click();
                }
                catch { }

                session.Quit();
                session = null;
            }
        }
/*
TearDown 方法用于清理测试环境,关闭记事本应用程序并删除会话。
如果 session 不为 null,则关闭应用程序。
尝试点击 "Don't Save" 对话框中的按钮,以确保不会阻塞退出。
最后退出会话并将 session 设置为 null。
*/
        [TestInitialize]
        public void TestInitialize()
        {
            // Select all text and delete to clear the edit box
            editBox.SendKeys(Keys.Control + "a" + Keys.Control);
            editBox.SendKeys(Keys.Delete);
            Assert.AreEqual(string.Empty, editBox.Text);
        }
/*
TestInitialize 方法用于在每个测试方法执行之前初始化测试环境。
使用 editBox 对象发送键盘命令来选择并删除文本框中的所有文本。
断言确保编辑框中的文本已被清空。
*/
        

        protected static string SanitizeBackslashes(string input) => input.Replace("\\", Keys.Alt + Keys.NumberPad9 + Keys.NumberPad2 + Keys.Alt);
    }
}
/*
SanitizeBackslashes 方法用于处理输入字符串中的反斜杠 (\),将其替换为特定的键盘命令。
在这个例子中,用 Keys.Alt + Keys.NumberPad9 + Keys.NumberPad2 + Keys.Alt 替换 \,可能用于避免路径字符串在发送到应用程序时被误解释或转义
*/

这段代码是一个基于 MSTest 框架和 Appium WindowsDriver 的自动化测试示例,用于测试 Windows 记事本应用程序的不同功能。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using System.Threading;
using System;
/*
Microsoft.VisualStudio.TestTools.UnitTesting:引入了 MSTest 框架,用于编写单元测试和断言。
OpenQA.Selenium:引入了 Selenium WebDriver 的核心接口和类。
System.Threading 和 System:引入了一些基本的 .NET 框架命名空间,用于线程控制和其他系统级操作。
*/


namespace NotepadTest
{
/*
NotepadTest 命名空间内定义了一个名为 ScenarioEditor 的测试类,该类继承自 NotepadSession,即前面我们解析的自定义会话类。
*/
    [TestClass]
    public class ScenarioEditor : NotepadSession
    {
        [TestMethod]
        public void EditorEnterText()
        {
            // Type mixed text and apply shift modifier to 7890_ to generate corresponding symbols
            Thread.Sleep(TimeSpan.FromSeconds(2));
            editBox.SendKeys("abcdeABCDE 12345" + Keys.Shift + "7890-" + Keys.Shift + @"!@#$%");
            Assert.AreEqual(@"abcdeABCDE 12345&*()_!@#$%", editBox.Text);
        }
/*
功能:测试在记事本中输入文本并应用 Shift 键生成相应的符号。
步骤:
等待 2 秒钟(使用 Thread.Sleep)。
使用 editBox.SendKeys 方法向记事本编辑框发送一系列的键盘输入。
使用 Keys.Shift 来模拟按下 Shift 键,以生成特殊字符。
使用断言验证编辑框中的文本是否与预期相符。
*/
        [TestMethod]
        public void EditorKeyboardShortcut()
        {
            // Type a known text sequence, select, copy, and paste it three times
            editBox.SendKeys("789");
            editBox.SendKeys(Keys.Control + "a" + Keys.Control); // Select all using Ctrl + A keyboard shortcut
            editBox.SendKeys(Keys.Control + "c" + Keys.Control); // Copy using Ctrl + C keyboard shortcut
            editBox.SendKeys(Keys.Control + "vvv" + Keys.Control); // Paste 3 times using Ctrl + V keyboard shortcut
            Assert.AreEqual("789789789", editBox.Text);
        }
/*
功能:测试使用键盘快捷键在记事本中进行文本选择、复制和粘贴操作。
步骤:
向编辑框发送文本 "789"。
使用 Keys.Control 和字母键组合模拟 Ctrl + A(全选)、Ctrl + C(复制)、Ctrl + V(粘贴)操作。
使用断言验证编辑框中的文本是否正确地被粘贴了三次。
*/

        [TestMethod]
        public void EditorNonPrintableShortcutKey()
        {
            // Press F5 to get Time/Date from Notepad
            Assert.AreEqual(string.Empty, editBox.Text);
            editBox.SendKeys(Keys.F5);
            Assert.AreNotEqual(string.Empty, editBox.Text);
        }
/*
功能:测试在记事本中使用非打印的快捷键,如按下 F5 获取当前时间/日期。
步骤:
使用断言验证编辑框中的文本是否为空。
向编辑框发送 F5 键。
使用断言验证编辑框中的文本是否不再为空(说明时间/日期已经被插入)
*/

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            Setup(context);
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            TearDown();
        }
    }
}
/*
[ClassInitialize] 和 [ClassCleanup] 是 MSTest 的修饰符,用于在整个测试类的开始和结束时执行一次初始化和清理操作。
ClassInitialize 方法调用 Setup(context) 方法,初始化记事本应用程序会话。
ClassCleanup 方法调用 TearDown() 方法,关闭记事本应用程序并删除会话。
*/

MenuItemEdit 方法通过模拟用户操作测试了记事本应用程序的编辑菜单中的几个功能:获取时间/日期、全选、复制和粘贴。每个操作后使用断言来验证操作的正确性,以确保应用程序在这些功能上的行为符合预期.

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NotepadTest
{
    [TestClass]
    public class ScenarioMenuItem : NotepadSession
    {
        [TestMethod]
        public void MenuItemEdit()
        {
            // Select Edit -> Time/Date to get Time/Date from Notepad
            Assert.AreEqual(string.Empty, editBox.Text);
            session.FindElementByName("编辑(E)").Click();
            session.FindElementByXPath($"//MenuItem[starts-with(@Name, \"时间/日期\")]").Click();
            string timeDateString = editBox.Text;
            Assert.AreNotEqual(string.Empty, timeDateString);

            // Select all text, copy, and paste it twice using MenuItem Edit -> Select All, Copy, and Paste
            session.FindElementByName("编辑(E)").Click();
            session.FindElementByXPath($"//MenuItem[starts-with(@Name, \"全选\")]").Click();
            session.FindElementByName("编辑(E)").Click();
            session.FindElementByXPath($"//MenuItem[starts-with(@Name, \"复制\")]").Click();
            session.FindElementByName("编辑(E)").Click();
            session.FindElementByXPath($"//MenuItem[starts-with(@Name, \"粘贴\")]").Click();
            session.FindElementByName("编辑(E)").Click();
            session.FindElementByXPath($"//MenuItem[starts-with(@Name, \"粘贴\")]").Click();

            // Verify that the Time/Date string is duplicated
            Assert.AreEqual(timeDateString + timeDateString, editBox.Text);
        }

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            Setup(context);
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            TearDown();
        }
    }
}
/*
功能:
获取时间/日期:首先点击菜单项 Edit -> Time/Date 从记事本中获取当前时间或日期,并验证获取的时间/日期字符串不为空。
复制和粘贴:然后执行复制和粘贴操作,使用菜单项 Edit -> Select All, Copy, 和 Paste 来复制当前编辑框中的文本并粘贴两次。
验证:最后,验证编辑框中的文本是否成功复制和粘贴,即时间/日期字符串被成功重复。

分析步骤:
获取时间/日期:
首先,断言 Assert.AreEqual(string.Empty, editBox.Text); 确保编辑框最初为空。
然后点击菜单项 编辑(E),并使用 XPath 找到名为 时间/日期 的菜单项,并点击它。
将获取的时间/日期字符串存储在 timeDateString 变量中,并通过 Assert.AreNotEqual(string.Empty, timeDateString); 确保成功获取了时间/日期。

复制和粘贴:
接下来,依次点击菜单项 编辑(E),执行全选、复制和两次粘贴操作。
使用 XPath 查找菜单项并点击,依次执行全选、复制、粘贴操作。

验证复制和粘贴:
最后,使用 Assert.AreEqual(timeDateString + timeDateString, editBox.Text); 验证编辑框中的文本是否与两次获取的时间/日期字符串相匹配,以确认复制和粘贴操作是否成功。
*/

这些方法保证了在执行自动化测试过程中,能够在测试前进行必要的设置(如初始化测试会话),并在测试完成后进行适当的清理(如删除生成的测试文件)。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium;
using System.Threading;
using System;

namespace NotepadTest
{
    [TestClass]
    public class ScenarioPopupDialog : NotepadSession
    {
        private const string ExplorerAppId = @"C:\Windows\System32\explorer.exe";
        private const string TestFileName = "NotepadTestFile";
        private const string TargetSaveLocation = @"%TEMP%\";
/*
ExplorerAppId:Windows 文件资源管理器的可执行文件路径。
TestFileName:用于测试的文件名。
TargetSaveLocation:文件保存的目标位置,使用了 %TEMP% 表示临时文件夹。
*/
        [TestMethod]
        public void PopupDialogSaveFile()
        {
            session.FindElementByName("文件(F)").Click();
            session.FindElementByXPath($"//MenuItem[starts-with(@Name, \"另存为\")]").Click();
            Thread.Sleep(TimeSpan.FromSeconds(1)); // Wait for 1 second until the save dialog appears
            session.FindElementByAccessibilityId("FileNameControlHost").SendKeys(SanitizeBackslashes(TargetSaveLocation + TestFileName));
/*
FileNameControlHost 是一个用于命名文件路径或文件名输入的控件或元素的名称。在自动化测试脚本中,通过 FindElementByAccessibilityId("FileNameControlHost") 定位到这个控件,然后使用 SendKeys 方法向其中发送文件路径或文件名信息。
*/
            session.FindElementByName("保存(S)").Click();

            // Check if the Save As dialog appears when there's a leftover test file from previous test run
            try
            {
                Thread.Sleep(TimeSpan.FromSeconds(1)); // Wait for 1 second in case save as dialog appears
                 session.FindElementByName("确认另存为").FindElementByName("是(Y)").Click();
            }
            catch { }

            // Verify that Notepad has saved the edited text file with the given name
            Thread.Sleep(TimeSpan.FromSeconds(1.5)); // Wait for 1.5 seconds until the window title is updated
            Assert.IsTrue(session.Title.Contains(TestFileName));
        }
/*
TestMethod 标记表明这是一个测试方法。
打开文件菜单 (文件(F)) 并点击 另存为 菜单项。
等待 1 秒,以确保保存对话框出现。
在保存对话框中输入文件保存路径和文件名。
点击 保存(S) 按钮。
使用 try-catch 块尝试处理可能出现的 Confirm Save As 窗口,点击其中的 Yes 按钮。
*/

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            Setup(context);
        }


/*
目的:在整个测试类运行结束后执行的清理操作。
功能:
创建一个 WindowsDriver 实例,用于控制 Windows 资源管理器(Explorer)。
使用 SendKeys 方法模拟按键操作,将资源管理器导航到指定的保存目录。
通过查找文件并删除测试文件来清理测试环境。
最后,调用 TearDown() 方法清理测试会话和相关资源。
*/
        [ClassCleanup]
        public static void ClassCleanup()
        {
         // // 创建一个 Windows 资源管理器会话以删除上面保存的文本文件
            DesiredCapabilities appCapabilities = new DesiredCapabilities();
            appCapabilities.SetCapability("app", ExplorerAppId);
            appCapabilities.SetCapability("deviceName", "WindowsPC");

            WindowsDriver<WindowsElement> windowsExplorerSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
            Assert.IsNotNull(windowsExplorerSession);

           导航 Windows 资源管理器到目标保存位置文件夹
            windowsExplorerSession.Keyboard.SendKeys(Keys.Alt + "d" + Keys.Alt + SanitizeBackslashes(TargetSaveLocation) + Keys.Enter);

          // 验证文件确实保存在工作目录中并删除它
            windowsExplorerSession.FindElementByAccessibilityId("SearchEditBox").SendKeys(TestFileName + Keys.Enter);
            Thread.Sleep(TimeSpan.FromSeconds(2));
            WindowsElement testFileEntry = null;
            try
            {
                testFileEntry = windowsExplorerSession.FindElementByName("Items View").FindElementByName(TestFileName + ".txt") as WindowsElement;  // In case extension is added automatically
            }
            catch
            {
                try
                {
                    testFileEntry = windowsExplorerSession.FindElementByName("Items View").FindElementByName(TestFileName) as WindowsElement;
                }
                catch { }
            }

        // 存在时删除测试文件
            if (testFileEntry != null)
            {
                testFileEntry.Click();
                testFileEntry.SendKeys(Keys.Delete);
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }

            windowsExplorerSession.Quit();
            windowsExplorerSession = null;
            TearDown();
        }
    }
}

测试结果

参考文献

Get Your WPF Apps Automated With Appium

Appium: Windows系统桌面应用自动化测试(一) 【环境搭建】_appium-windows-CSDN博客

21、uwp UI自动化测试(WinAppDriver) - 博琼 - 博客园

搭建 WPF 上的 UI 自动化测试框架-腾讯云开发者社区-腾讯云

使用WinAPPDriver做UI自动化测试_winappuitest-CSDN博客

使用 C# 和 WPF 生成 Hello World 应用 - Visual Studio (Windows)

GitHub - telerik/Get-your-WPF-apps-automated-with-Appium: Demo app and test project demonstrating how to use Appium to automate WPF applications

使用 Appium 进行 WPF 自动化_wpf自动化测试-CSDN博客

UI Automation - Win32 apps

  • 38
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值