研究Winappdriver测试Winform with Java(四)

系列文章目录



前言

当我们谈论测试自动化时,通常首先想到的是移动和Web自动化。然而,作为测试人员,我们也应该了解桌面自动化的最新技术。
但是关于桌面自动化的在线信息似乎较少。


提示:以下是本篇文章正文内容,下面案例可供参考

一、准备环境

以下是我们测试所需的组件列表:

  1. Windows 10: 我使用的是 Windows 10 x64。你可能在运行32位版本的Windows,但唯一的区别可能是一些组件的位置。

  2. 测试应用程序: 我们的测试将使用任何Windows计算机上都可用的默认“记事本”应用程序。

    • 位置:C:\Windows\System32\notepad.exe
  3. Windows Application Driver: 用于Windows自动化的主要组件。

  4. Windows 10 SDK: 为了能够检查应用程序内的元素。我们将使用“inspect.exe”工具来实现这一目的。

  5. IntelliJ IDEA: 用于编写我们的测试的集成开发环境(IDE)。

  6. Java JDK: 由于我们使用Java编写我们的测试,我们需要此组件使测试正常运行。

注意: 如果希望IntelliJ IDEA在项目配置期间自动安装Java JDK,则可以通过使用上述链接下载所有组件(除了Java JDK)并进行安装。安装这些组件应该不会有太大复杂度,只需按照安装向导的步骤操作即可。

安装完这些组件后,我们需要配置一些内容。
在这里插入图片描述

  1. 打开“设置”应用程序并查找“开发者模式”。启用此选项。
  2. 打开 IntelliJ IDEA并创建一个新项目。选择您喜欢的项目名称(例如“WindowsTest”),然后从左侧选择“Maven”作为构建自动化工具。从列表中选择Java 版本 1.8(它会给您一个下载选项)。

在这里插入图片描述

  1. 启动WinAppDriver。这是我们的关键组件,将用于自动化测试。
    在这里插入图片描述
    默认情况下,WinAppDriver 位于此处:

C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe

另外,请注意,与任何其他 Selenium 类似的工具一样,它也作为服务器在特定的 IP 地址和端口上运行。默认情况下,它是 http://127.0.0.1:4723,这就是我们将在测试中使用的地址。
启动 WinAppDriver 会话并将其最小化。它已准备好接受我们的测试请求。

二、为 Windows 自动化准备 Java 项目

1.引入库

我们已准备好编写 Windows 测试。创建项目后,让我们配置位于测试项目根目录中的 pom.xml 并使其看起来与此类似:

代码如下(示例):

<?xml version=1.0" encoding=”UTF-8"?>
<project xmlns=”http://maven.apache.org/POM/4.0.0"
 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
<groupId>org.nomadicdmitry</groupId>
 <artifactId>WindowsTest</artifactId>
 <version>1.0-SNAPSHOT</version>
 <dependencies>
 <! — https://mvnrepository.com/artifact/io.appium/java-client →
 <dependency>
 <groupId>io.appium</groupId>
 <artifactId>java-client</artifactId>
 <version>7.3.0</version>
 </dependency>
 <! — https://mvnrepository.com/artifact/org.testng/testng →
  <dependency>
 <groupId>org.testng</groupId>
 <artifactId>testng</artifactId>
 <version>7.1.0</version>
 <scope>test</scope>
 </dependency>
 <! — https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java →
 <dependency>
 <groupId>org.seleniumhq.selenium</groupId>
 <artifactId>selenium-java</artifactId>
 <version>3.141.59</version>
 </dependency>
 </dependencies>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>
 </properties>
</project>

添加这些行后,按右侧的 Maven“同步”按钮。我们的测试配置与本文中所做的类似(除了添加 Maven SureFire)。
我们将这三个库添加到我们的项目中:

io.appium
org.testng
org.seleniumhq.selenium

此外,我们通过指定“compiler.source”和“compile.target”值来指定使用 Java 1.8。最好明确指定所有内容

我们准备好编写测试了。在这种情况下,事情会很简单,我们不会使用 PageObject 模型。我们需要创建一个类作为我们的自动化脚本。

继续并在“[项目名称]\src\test\java\”中创建“NotepadTest”类。
测试项目的结构

2.编写 Windows 自动化测试

这就是有趣的部分。我们已经讨论了我们要自动化哪些应用程序。现在让我们决定测试用例是什么样的:
测试用例如下(示例):

Test Case #1:
1. Open “Notepad”
2. Open “Help” menu item
3. Press “About Notepad”
4. Press “OK” button
Test Case #2:
1. Open “Notepad”
2. Type current date
3. Clear the results
Test Case #3
1. Open “Notepad”
2. Open “Edit” menu item
3. Press “Time/Date” in dropdown menu
4. Confirm that the test is displayed in "Notepad"
5. Clear the results

首先手动编写和执行这些测试用例以了解测试流程始终是一个好主意。以下是“NotepadTest”类的代码:

public class NotepadTest {
private static WindowsDriver notepadSession = null;
public static String getDate(){
 LocalDate date = LocalDate.now();
 return date.toString();
 }
@BeforeClass
 public static void setUp() {
 try {
 DesiredCapabilities capabilities = new DesiredCapabilities();
 capabilities.setCapability(“app”,C:\\Windows\\System32\\notepad.exe”);
 capabilities.setCapability(“platformName”,Windows);
 capabilities.setCapability(“deviceName”,WindowsPC);
 notepadSession = new WindowsDriver(new URL(“http://127.0.0.1:4723), capabilities);
 notepadSession.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
} catch (Exception e) {
 e.printStackTrace();
  }
 }
@AfterMethod
 public void cleanApp(){
 notepadSession.quit();
 setUp();
 }
@AfterSuite
 public void tearDown(){
 notepadSession.quit();
 }
@Test
 public void checkAboutWindow() {
 notepadSession.findElementByName(Help).click();
 notepadSession.findElementByName(About Notepad).click();
 notepadSession.findElementByName(OK).click();
 }
@Test
 public void sendTestText(){
 notepadSession.findElementByClassName(Edit).sendKeys(getDate());
 notepadSession.findElementByClassName(Edit).clear();
 }
@Test()
 public void pressTimeAndDateButton(){
 notepadSession.findElementByName(Edit).click();
 notepadSession.findElementByAccessibilityId(26).click();
Assert.assertNotNull(notepadSession.findElementByClassName(Edit));
 notepadSession.findElementByClassName(Edit).clear();
  }
 }

这里是什么呢,让我们从头开始回顾一下代码:

LocalDate date = LocalDate.now();
 return date.toString();

这个函数很容易理解。我们使用它来生成当前的本地日期。然后我们将其转换为字符串。我们将进一步使用此函数将此文本输入到我们的记事本窗口中。

DesiredCapabilities capabilities = new DesiredCapabilities();
 capabilities.setCapability(“app”,C:\\Windows\\System32\\notepad.exe”);
 capabilities.setCapability(“platformName”,Windows);
 capabilities.setCapability(“deviceName”,WindowsPC);
 notepadSession = new WindowsDriver(new URL(“http://127.0.0.1:4723), capabilities);
Note: Highlighted "platformName" and "deviceName" properties are not necessary if you are performing tests using WinAppDriver. Those are only needed if you use Appium to clarify which platform we are using for tests.

接下来,我们设置执行测试的环境。我们正在设置可执行文件并指定要运行测试的 IP 地址和端口。
这些是默认参数,但请确保它与我们之前启动的已运行的 WinAppDriver 会话的地址匹配。这些行执行记事本应用程序的启动。
在执行测试之前,我们还会执行 2 秒超时:

notepadSession.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

之后,剩下要做的就是按下按钮并输入值。以下是按钮单击操作的示例:

notepadSession.findElementByName(Help).click();
notepadSession.findElementByName(About Notepad).click();
notepadSession.findElementByName(OK).click();

在这里插入图片描述
类似地,这也是将文本发送到特定元素的方式。我们获取 getDate() 函数的输出并将输出发送到记事本:

notepadSession.findElementByClassName(Edit).sendKeys(getDate());

如果您准备好执行测试,请右键单击并按“运行 NotepadTest”按钮:
在这里插入图片描述定位

三. Windows 桌面测试的元素

为了能够单击按钮并向字段中输入文本,我们需要首先能够找到这些元素。我们需要为测试中使用的每个元素找到“定位器”。有多种方法可以为我们的 Windows 测试定位元素:

Appium Desktop
WinAppDriver UI Recorder Tool
Inspect tool from Windows SDK

正如本文上面所讨论的,我们将使用此列表中的最后一个工具。由于我们已经安装了 Windows 10 SDK,因此我们应该可以使用此工具。如果您愿意,也可以随意尝试其他工具。
“inspect.exe”工具位于我系统上的以下目录中:

C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\

根据您的情况(如果您运行 32 位 Windows 系统),它可能会有所不同,但默认情况下它应该位于您的 Program Files\Windows Kits\10\bin 目录中。
让我们启动inspect.exe应用程序,看看它如何帮助我们。
检查“记事本”以确定定位元素的策略
这个想法很简单。您打开一个应用程序(例如我们示例中的“记事本”),然后指向您想要在测试中使用的元素。 Inspect 应用程序显示有关所选对象的相应信息。

我们以上面的例子为例。用户已指向“时间/日期”元素,应用程序显示了两种可供我们使用的定位器策略:

Name: Time/Date
AutomationId: 26

两个定位器都指向同一个对象(“时间/日期”菜单项),但如果您尝试通过“名称”访问它:

notepadSession.findElementByName(“Time/Date”);

在这种情况下它不起作用。这就是我们使用“AutomationId”的原因:

notepadSession.findElementByAccessibilityId(“26”);

可以看到,“AutomationId”在WinAppDriver中对应的名称是“AccessibilityId”。需要注意的一件事是,最好的定位器是具有将来不太可能更改的唯一 ID 的定位器。

还有更多选项可供选择。例如,当我们在记事本中输入信息时,我们使用“ClassName”元素定位器:

notepadSession.findElementByClassName(“Edit”);

四. Assert or not Assert? 断言还是不断言?

Assert.assertNotNull(notepadSession.findElementByClassName(Edit));

断言是一个强大的工具,主要用于单元测试。正如您在这个特定示例中看到的,我们正在检查记事本中的文本是否不为空。
由于我们在记事本会话中输入了文本,因此该测试将会通过。如果您好奇,可以尝试将“assertNotNull”更改为“assertNull”,然后看看测试将如何执行。


总结

在令人兴奋的测试自动化世界中还有很多值得探索的东西!

  • 34
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值