WinAppDriver 自动化测试winform程序

WinAppDriver 自动化测试winform程序

前言

WinAppDriver是Windows系统上的一个应用程序驱动工具,开源免费。与Selenium工具类似,都是用来实现产品UI自动化测试的一个工具。

WinAppDriver运行时对系统是有要求的,只能运行在Windows10或Windows Server 2016以上系统。如果测试程序兼容性,WinAppDriver很显然不能满足Windows10或Windows Server 2016以下系统的测试。因此使用WinAppDriver实现的自动化测试脚本是有局限性的。

WinAppDriver支持测试UWP、WinForms、WPF、Win32应用程序。

UWP: Universal Windows Platform,即Windows通用应用平台,在Windows 10 Mobile/Surface(Windows平板电脑)/PC/Xbox/HoloLens等平台上运行。它并不是为某一个终端而设计,而是可以在所有Windows10设备上运行。

WinForms: Windows Forms,是微软的.NET开发框架的图形用户界面部分,该组件通过将现有的Windows API(Win32 API)封装为托管代码提供了对Windows本地(native)组件的访问方式。

WPF: Windows Presentation Foundation,是微软推出的基Windows的用户界面框架,属于.NET Framework 3.0的一部分。它提供了统一的编程模型、语言和框架,真正做到了分离界面设计人员与开发人员的工作;同时它提供了全新的多媒体交互用户图形界面。

Win32: Classic Windows,是标准windows程序,完全拥有window的特性,可以通过鼠标点击窗口来完成控制。

在这里插入图片描述

1. 环境搭建

前提条件

电脑系统需要Windows 10或Windows Server 2016或者更高版本,这是前提条件

1.1 打开Windows PC的开发者模式

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

1.2 下载Windows driver并安装

github下载地址:https://github.com/Microsoft/WinAppDriver/releases

在这里插入图片描述

选择与你电脑对应的exe安装

安装好之后,运行WinAppDriver.exe(记得要用admin权限运行), 默认路径 (C:\Program Files (x86)\Windows Application Driver)

也可以自定义地址或端口:
在cmd窗口中输入

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

在这里插入图片描述

在这里插入图片描述
这样就说明运行成功

2. Windows 自动化脚本

运行脚本前要打开 WinAppDriver.exe

对于Windows App来说,只需要传一个app capabilities 即可。

对于UWP的App,app对应的值为Application Id(App ID)。关于如何获取APP ID,可以使用powershell命令get-StartApps来获取,打开powershell终端运行:get-StartApps | select-string "计算器"即可获取值(运行命令之前先打开计算器)。

DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", CalculatorAppId);
appCapabilities.SetCapability("deviceName", "WindowsPC");
appCapabilities.SetCapability("platformName", "Windows");
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);

3. Windows定位元素

使用Windows SDK提供的工具inspect.exe(C:\Program Files (x86)\Windows Kits\10\bin\x86或者C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64根据系统查看)来定位,详情查看inspect,或者使用AccExplorer32、UISpy定位。
支持的定位方式:

在这里插入图片描述

4. 示例

这个是官方给的示例

CalculatorSession.cs

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
using System;namespace CalculatorTest
{public class CalculatorSession{// Note: append /wd/hub to the URL if you're directing the test at Appiumprivate const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";private const string CalculatorAppId = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App";protected static WindowsDriver<WindowsElement> session;public static void Setup(TestContext context){// Launch Calculator application if it is not yet launchedif (session == null){// Create a new session to bring up an instance of the Calculator application// Note: Multiple calculator windows (instances) share the same process IdDesiredCapabilities appCapabilities = new DesiredCapabilities();appCapabilities.SetCapability("app", CalculatorAppId);appCapabilities.SetCapability("deviceName", "WindowsPC");appCapabilities.SetCapability("platformName", "Windows");session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);Assert.IsNotNull(session);// Set implicit timeout to 1.5 seconds to make element search to retry every 500 ms for at most three timessession.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);}}public static void TearDown(){// Close the application and delete the sessionif (session != null){ session.Quit();session = null;}}}
}

ScenarioStandard.cs

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium.Windows;
using System.Threading;
using System;namespace CalculatorTest
{[TestClass]public class ScenarioStandard : CalculatorSession{private static WindowsElement header;private static WindowsElement calculatorResult;[TestMethod]public void Addition(){// Find the buttons by their names and click them in sequence to perform 1 + 7 = 8session.FindElementByName("一").Click();session.FindElementByName("加").Click();session.FindElementByName("七").Click();session.FindElementByName("等于").Click();Assert.AreEqual("8", GetCalculatorResultText());}[TestMethod]public void Division(){// Find the buttons by their accessibility ids and click them in sequence to perform 88 / 11 = 8session.FindElementByAccessibilityId("num8Button").Click();session.FindElementByAccessibilityId("num8Button").Click();session.FindElementByAccessibilityId("divideButton").Click();session.FindElementByAccessibilityId("num1Button").Click();session.FindElementByAccessibilityId("num1Button").Click();session.FindElementByAccessibilityId("equalButton").Click();Assert.AreEqual("8", GetCalculatorResultText());}[TestMethod]public void Multiplication(){// Find the buttons by their names using XPath and click them in sequence to perform 9 x 9 = 81//session.FindElementByXPath("//Button[@Name='Nine']").Click();//session.FindElementByXPath("//Button[@Name='Multiply by']").Click();//session.FindElementByXPath("//Button[@Name='Nine']").Click();//session.FindElementByXPath("//Button[@Name='Equals']").Click();session.FindElementByAccessibilityId("num9Button").Click();session.FindElementByAccessibilityId("num9Button").Click();session.FindElementByAccessibilityId("multiplyButton").Click();session.FindElementByAccessibilityId("num9Button").Click();session.FindElementByAccessibilityId("equalButton").Click();Assert.AreEqual("891", GetCalculatorResultText());}[TestMethod]public void Subtraction(){// Find the buttons by their accessibility ids using XPath and click them in sequence to perform 9 - 1 = 8session.FindElementByXPath("//Button[@AutomationId=\"num9Button\"]").Click();session.FindElementByXPath("//Button[@AutomationId=\"minusButton\"]").Click();session.FindElementByXPath("//Button[@AutomationId=\"num1Button\"]").Click();session.FindElementByXPath("//Button[@AutomationId=\"equalButton\"]").Click();Assert.AreEqual("8", GetCalculatorResultText());}[TestMethod][DataRow("一",   "加",      "九", "10")][DataRow("九",  "减",     "二",   "8")][DataRow("八", "除以", "二", "4")]public void Templatized(string input1, string operation, string input2, string expectedResult){// Run sequence of button presses specified above and validate the resultssession.FindElementByName(input1).Click();session.FindElementByName(operation).Click();session.FindElementByName(input2).Click();session.FindElementByName("等于").Click();Assert.AreEqual(expectedResult, GetCalculatorResultText());}[ClassInitialize]public static void ClassInitialize(TestContext context){// Create session to launch a Calculator windowSetup(context);// Identify calculator mode by locating the headertry{header = session.FindElementByAccessibilityId("Header");}catch{header = session.FindElementByAccessibilityId("ContentPresenter");}            // Ensure that calculator is in standard modeif (!header.Text.Equals("标准", StringComparison.OrdinalIgnoreCase)){session.FindElementByAccessibilityId("TogglePaneButton").Click();Thread.Sleep(TimeSpan.FromSeconds(1));var splitViewPane = session.FindElementByClassName("SplitViewPane");splitViewPane.FindElementByName("标准").Click();Thread.Sleep(TimeSpan.FromSeconds(1));Assert.IsTrue(header.Text.Equals("标准", StringComparison.OrdinalIgnoreCase));}// Locate the calculatorResult elementcalculatorResult = session.FindElementByAccessibilityId("CalculatorResults");Assert.IsNotNull(calculatorResult);}[ClassCleanup]public static void ClassCleanup(){TearDown();}[TestInitialize]public void Clear(){session.FindElementByName("清除").Click();Assert.AreEqual("0", GetCalculatorResultText());}private string GetCalculatorResultText(){return calculatorResult.Text.Replace("显示为 ", string.Empty).Trim();}}
}

winform程序类似,先获取元素,然后模拟点击,输入框的模拟输入即可。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/804885.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024/4/6—力扣—简化路径

代码实现&#xff1a; // 分割/得到名字 char **split(const char *s, int *returnSize) {int n strlen(s);char **ans (char **)malloc(sizeof(char *) * n);int l 0, r 0, len 0;while (r < n) {while (r < n && s[r] /) {r;}l r;while (r < n &…

Python | 海表面温度(SST) | 长期趋势和异常分析

趋势和异常分析&#xff08;Trend and anomaly)在大气和海洋学研究中被广泛用于探测长期变化。 趋势分析&#xff08;Trend Analysis&#xff09;&#xff1a; 趋势分析是一种用于检测数据随时间的变化趋势的方法。在海洋学和大气学中&#xff0c;常见的趋势分析包括海表面温…

WeTrade众汇账户类型有哪几种?FX110网

WeTrade众汇是一个在线交易平台&#xff0c;允许用户买卖各种金融工具&#xff0c;包括交易外汇、金属、能源、指数、股票和加密货币。 WeTrade众汇不仅提供多种交易市场&#xff0c;还提供多种有竞争力的工具和服务。那么&#xff0c;WeTrade众汇账户类型有哪几种&#xff1f;…

CF938Div3(A-F)

A: 买n个酸奶&#xff0c;一次一瓶a元,一次买两瓶可以优惠价b元,也可以a元,问恰好买n瓶需要多少钱. void solve() {int n, a, b;cin >> n >> a >> b;int ans min(a * n, n / 2 * b n % 2 * a);cout << ans << endl; } B: 给你一个数组,问能…

AWVS/Acunetix Premium V24.3.2403高级版漏洞扫描器

前言 Acunetix Premium 是一种 Web 应用程序安全解决方案&#xff0c;用于管理多个网站、Web 应用程序和 API 的安全。集成功能允许您自动化 DevOps 和问题管理基础架构。 Acunetix Premium&#xff1a;全面的 Web 应用程序安全解决方案 Web 应用程序对于企业和组织与客户、…

Vue 大文件切片上传实现指南包会,含【并发上传切片,断点续传,服务器合并切片,计算文件MD5,上传进度显示,秒传】等功能

Vue 大文件切片上传实现指南 背景 在Web开发中&#xff0c;文件上传是一个常见的功能需求&#xff0c;尤其是当涉及到大文件上传时&#xff0c;为了提高上传的稳定性和效率&#xff0c;文件切片上传技术便显得尤为重要。通过将大文件切分成多个小块&#xff08;切片&#xff0…

【Python 基础知识课程】Python的第一个程序

Python 简介 Python 是一种功能强大且用途广泛的编程语言&#xff0c;广泛用于数据科学、Web 开发、自动化等高需求领域。 幸运的是&#xff0c;对于初学者来说&#xff0c;它也是一种很好的学习语言&#xff0c;因为Python代码更容易阅读和编写。它的简单性使其成为初学者的完…

【Qt】:窗口

窗口 一.概述二.菜单栏1.一个简单的菜单2.添加快捷键3.嵌套子菜单4.添加下划线5.添加图标 三.工具栏1.创建一个简单的工具栏2.设置工具栏的停靠位置 四.状态栏五.浮动窗口 一.概述 Qt窗口是通过QMainWindow类来实现的。 QMainWindow是一个为用户提供主窗口程序的类&#xff0c…

Utilize webcam to capture photo with camera

1. Official Guide& my github Official course my github 2. Overcome Webcam js Error in Chrome: Could not access webcam link 直接把代码拷贝到本机的下述目录下 To ignore Chrome’s secure origin policy, follow these steps. Navigate to chrome://flags/#un…

StarRocks实战——华米科技埋点分析平台建设

目录 前言 一、原有方案及其痛点 二、引入StarRocks 三、方案改造 3.1 架构设计 3.2 数据流程 3.3 性能指标 3.4 改造收益 前言 华米科技是一家基于云的健康服务提供商&#xff0c;每天都会有海量的埋点数据&#xff0c;以往基于HBase建设的埋点计算分析项目往往效率上…

小红书APP闪退,电商ERP系统接口该如何测试呢?

大数据时代&#xff0c; 数据收集不仅是科学研究的基石&#xff0c; 更是企业决策的关键。 然而&#xff0c;如何高效地收集数据 成了摆在我们面前的一项重要任务。 本文将为你揭示&#xff0c; 一系列实时数据采集方法&#xff0c; 助你在信息洪流中&#xff0c; 找到…

OJ 栓奶牛【C】【Python】【二分算法】

题目 算法思路 要求的距离在最近木桩与最远木桩相隔距离到零之间&#xff0c;所以是二分法 先取一个中间值&#xff0c;看按照这个中间值可以栓多少奶牛&#xff0c;再与输入奶牛数比较&#xff0c;如果大于等于&#xff0c;则增大距离&#xff0c;注意这里等于也是增大距离…

苍穹外卖---文件上传-阿里OSS

一&#xff1a;开通阿里云对象存储服务oss,创建bucket&#xff0c;获得密钥 二&#xff1a;在程序中集成上传文件功能 1.连接阿里云OSS对象存储服务器 声明一个配置属性的文件用于传入连接的参数 package com.sky.properties;import lombok.Data; import org.springframewo…

浏览器工作原理与实践--虚拟DOM:虚拟DOM和实际的DOM有何不同

虚拟DOM是最近非常火的技术&#xff0c;两大著名前端框架React和Vue都使用了虚拟DOM&#xff0c;所以我觉得非常有必要结合浏览器的工作机制对虚拟DOM进行一次分析。当然了&#xff0c;React和Vue框架本身所蕴含的知识点非常多&#xff0c;而且也不是我们专栏的重点&#xff0c…

二手车商的套路

https://www.dongchedi.com/article/7126394624675578405 https://www.dongchedi.com/article/7126394624675578405 现在&#xff0c;有越来越多的人去了解二手车&#xff0c;二手车相对于新车来说&#xff0c;更加的亲民划算。很多新车需要四五十万&#xff0c;而二手车有可…

Qt Creator 新建项目

&#x1f40c;博主主页&#xff1a;&#x1f40c;​倔强的大蜗牛&#x1f40c;​ &#x1f4da;专栏分类&#xff1a;QT❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 目录 一、使用 Qt Creator 新建项目 1、新建项目 2、选择项目模板 3、选择项目路径 4、选择构建系统 5…

你不可不知的5款小众神器

Hey小伙伴们&#x1f44b;&#xff0c;是不是还在为工作中的效率不高而烦恼呢&#xff1f;别急&#xff0c;今天我就给大家安利5款超实用的国产小众工作效软件&#xff0c;让你的工作台效率翻倍&#xff0c;轻松应对各种工作挑战&#xff01;&#x1f31f; 1️⃣ 亿可达 ycoda…

视频图像的两种表示方式YUV与RGB(4)

本篇主要讲YUV与RGB之间的转换&#xff0c;包括YUV444 颜色编码格式 转为 RGB 格式 &#xff0c;RGB颜色编码格式转为 YUV444 格式。 一、 YUV与RGB之间的转换 YUV与RGB颜色格式之间进行转换时 , 涉及一系列的数学运算 ; YUV 颜色编码格式转为RGB格式的转换公式 取决于 于 YUV …

蓝桥杯每日一题:矩形牛棚(单调栈)

作为一个资本家&#xff0c;农夫约翰希望通过购买更多的奶牛来扩大他的牛奶业务。 因此&#xff0c;他需要找地方建立一个新的牛棚。 约翰购买了一大块土地&#xff0c;这个土地可以看作是一个 R行&#xff08;编号 1∼R1&#xff09;C 列&#xff08;编号 1∼C1&#xff09;…

Python requests 模块

爬虫、网络编程、接口......对于Python工程师来讲都绕不过一个强大的模块---requests&#xff0c;本篇文章就深入详细讲一讲requests模块。同时也先也分享一下开源API网站&#xff1a;Gitee-API文档、JSONPlaceholder API文档、和风天气API文档、Postman Echo API网站&#xff…