How to test Windows applications

Copyright: Allow reproduced, reprinted but must retain the original link; do not for commercial or illegal purposes

Foreword

This tutorial explains how to use Windows AirtestIDE conduct automated test applications (Windows version NetEase cloud music is the case).

By reading this article, you will learn:

  • AirtestIDE use Windows software on a window script recording
  • How to call the Windows operating interface
  • How to specify a connection to a Windows window in the script and command line

1. Basic principles

AirtestIDE test support normal Windows applications rely mainly on image recognition frame (Airtest) locating the position, using pywinauto operation of the interface to simulate operation. 

Windows schematics

Poco in support of Windows is not yet available, we will introduce this feature as soon as possible, so stay tuned.

2. Connect the Windows window AirtestIDE

To use AirtestIDE test a Windows program, Windows first window you want to be tested to connect with AirtestIDE, in fact, is to tell Windows handle to the window to AirtestIDE. After a successful connection window, Airtest the window can be used as a device, just like in the next operating an Android device, capable of image recognition of the Windows window, click operation.

Note:  Due to the selected window to connect to Windows, operating in AirtestIDE in, when you run the script, this will depend on the particular window handle. So please do not close the window after selected, otherwise it will lead not find the corresponding window and run-time error.

Conventional connection method - a key embedding

Providing a key AirtestIDE embedded Windows window function, as follows:

The default connection method

Click AirtestIDE selected window button, will be able to choose a Windows application on the desktop has been started (this time edge of the window will display a green box), embed it in AirtestIDE. After the inset window, you can easily operate the window, record the statement, run the script in AirtestIDE in.

Another alternate method of connecting

由于Windows应用的底层实现各不相同,有些应用窗口如果使用默认的一键嵌入方法,在嵌入到AirtestIDE里之后会遇到一些问题(例如嵌入后无法用鼠标操作、无法正常嵌入、嵌入后无法显示图像等)。

针对这些问题,我们提供了一种无嵌入连接的方案,请在AirtestIDE的设置面板中,找到Device-Windows Embed Backup Method,勾选后即可使用备用方案来连接窗口。

接下来的连接方式和常规方法一样,点击选定窗口,然后选择待测窗口程序:

Alternate embedding scheme

从图中可以看到,这种窗口连接方式不会将Windows窗口嵌入到AirtestIDE里,虽然在脚本录制时不如默认嵌入方式简单好用,但是能够避免一些窗口嵌入带来的问题。

桌面模式

假如想要测试的窗口不止一个(拥有多个不同的窗口句柄),单独嵌入一个窗口可能无法满足测试需求,此时我们也提供了第三种方案:桌面模式。

Desktop mode entry

点击桌面模式按钮,此时AirtestIDE右侧的设备窗口将会完全隐藏,进入Windows桌面模式。

在此模式下,录制脚本与运行脚本都与正常情况下没有区别,只不过执行时将会对整个桌面进行截图识别,甚至可以识别到AirtestIDE代码窗口里的截图语句(因此在执行脚本时,请尽量缩小AirtestIDE的窗口,避免脚本界面里的截图干扰执行结果)。

连接注意事项

  • 由于在选定窗口时,桌面上可能会有不少同时打开着的其他窗口,导致选择时的绿色方框不一定能够很准确地框选到被测应用,请大家在点击选定窗口按钮之前,尽可能先把所有其他无关窗口都最小化,这样做可以让窗口选择更准确。
  • 为了防止出现异常情况,请不要选择自己的桌面、AirtestIDE软件本体。因为窗口嵌入的功能原理是,将一个Windows窗口设置为IDE的子窗口,因此如果试图连接桌面,可能会导致无法预料的异常情况发生。如果只是想测试桌面上的所有窗口,可以选择我们的桌面模式来录制脚本。
  • 由于Windows窗口在截图时需要记录分辨率,因此把窗口嵌入到IDE后大小将会被固定,无法修改。如果觉得嵌入后窗口过大,可以在嵌入之前先将窗口调整至合适大小再进行嵌入。

退出Windows连接

  • 直接关闭AirtestIDE即可退出本次Windows连接,并将先前嵌入的窗口自动弹出
  • 也可以通过右上角的Disconnect current device按钮来退出窗口连接

Exit windows mode

3. 录制与编写Airtest脚本

在成功连接上Windows窗口后,就相当于我们已经连接上了一台设备,接下来可以对它进行脚本的录制与回放了。

生成截图语句

首先,touch / wait / exists / assert_exists / assert_not_exists等Airtest带有截图的语句,与在Android/iOS设备上录制并没有太大区别:

image

但是需要注意的是,在使用鼠标拖拽框选好想要截图的位置后,需要双击该截图区域才能完成截图,而不会在鼠标松开时自动完成截图,这也是Windows下截图的最大区别。

因此截图的步骤是:

  • 拖动鼠标框选合适的区域
  • 双击鼠标完成截图
  • 点击鼠标右键可以重新进行框选
  • Esc按钮可以退出本次截图操作

以及,在录制swipe语句时,在完成截图区域框选后,需要单击滑动终点完成语句录制。

调用Windows接口

和Android/iOS一样,Airtest也对Windows下的常用操作进行了封装,底层操作接口使用的是pywinauto库。

因此,在编写Windows应用的测试脚本时,我们可能需要查阅以下几个API文档:

一个简单的示例

为了方便演示,我们假设这个脚本没有在命令行中传入参数,而是在脚本里使用connect_device接口来连上一个句柄为123456的窗口,并对它进行一些操作:

from airtest.core.api import *
dev = connect_device("Windows:///123456")
# 通用的接口调用方式,与其他平台相同:
touch(图片)

 

假如我们希望能够调用一些Windows的专属操作,比如查阅了Airtest的Windows专属API文档后,我们发现有一些操作是只有Windows窗口才有的:

# 调用Windows专属的接口,例如获取当前窗口的标题内容
print(dev.get_title())
# 把窗口移动到某个坐标位置
dev.move((100, 200))

 

接下来,假设我们现在想要使用鼠标滚轮,在查阅Airtest的API后发现,Airtest的Windows模块并没有封装鼠标滚轮的功能,此时我们可以进一步查询pywinauto的文档,寻找到mouse相关的章节后,就知道如何调用鼠标滚轮接口了:

dev.mouse.scroll(coords=(80, 100), wheel_dist=1)

 

输入keyevent

在Android中,我们可以通过keyevent("HOME")来实现按下HOME键的操作,而在Windows中,我们同样可以通过keyevent接口发送一些按键响应。Android的按键码是基于ADB的,而Airtest的Windows模块则封装使用了pywinauto支持的按键码,请查阅pywinauto.keyboard文档内容来编写Windows下的keyevent接口参数:

# 在pywinauto中,符号^也代表了CTRL键,因此^a即为全选(Ctrl+A)
keyevent("^a")
# 例如这是删除键的输入方式,需要加上括号{}
keyevent("{DEL}")

 

请大家根据实际需求,查阅文档后再编写按键响应的代码。

4. 如何在运行脚本时,指定连接某个窗口

在AirtestIDE的Windows模式中,运行脚本和查看报告与其他平台并无不同:

image

然而,最需要注意的一点是:在AirtestIDE里连接的窗口,都是用窗口句柄连接的。窗口句柄是每个Windows窗口对象拥有的独一无二的32位无符号整数,而且每次打开窗口,这个数值都会变化。

这就意味着,假如我们用某个窗口嵌入到AirtestIDE里写出了一个脚本,虽然这次可以直接运行,但是通过复制AirtestIDE里的脚本命令行的方式,是无法保证下一次还能够运行的。因为AirtestIDE里的命令行,将会有这样的参数内容--device Windows:///句柄,而下次再打开窗口,可能句柄已经发生了变化。

因此,我们也可以不使用句柄,而是通过其他的方式来连接窗口。Airtest的Windows模块,使用了pywinauto的connect接口来连接窗口,因此除了句柄以外,我们可以通过窗口标题来连接:

# 连接一个Windows窗口,窗口句柄为123456
Windows:///123456
# 连接一个Windows窗口,窗口名称匹配某个正则表达式
Windows:///?title_re=Unity.*
# 连接windows桌面,不指定任何窗口,对应IDE的桌面模式
Windows:///

 

在命令行中假如想使用窗口名称连接,不需要添加引号等符号,直接这样写:

airtest run test.air --device Windows:///?title_re=Unity.*

 

5. Poco支持

接入了Poco-SDK的Windows窗口

There are some Windows-app has access to the Poco (such as Unity game window), we are also able to use AirtestIDE to easily view and select the UI node. But before using this feature, you need to be told in advance AirtestIDE the Windows window location and size of the window.

In the "Options" - "Settings" - "Poco", select "Windows Content Area Rect", double-click the marquee window after confirming the main screen (right / esc cancel), then you can start using the Poco-related functions, as shown in shows:

image

Poco-Windows support

Poco support for Windows window, we are well documented and functionality, so stay tuned.

Guess you like

Origin www.cnblogs.com/AirtestProject/p/12118905.html