Advanced test road: pywinauto help you open the door to automation end pc!

Foreword

We Lemon class of small cute, after completion us lemons class automated course, mastered the interface automation, web automation, app automate these tasks commonly used in automation skills at work is sufficient enough to deal with more than 90% of the automation needs.

But recently there are some lovely small PC end automation asking how to do it?

This technology more curious, can not be achieved using python PC client automated testing?

The answer of course is yes, so today specifically put together here again the article, and we talk about how to use python to do with the PC end automation!

Environment Installation

PC side using python to do to achieve automation, the premise is first installed python.

We also need to use a third-party libraries: pywinauto.

Here we first need to install this library, install command as follows:


 

Instructions for use

After installed, you can refer to the instructions on the instructions on the official documents.

As shown below:


 

Above translation is as follows:

That is after we installed Pywinauto, we must first determine what kind of accessibility technology (pywinauto the backend) can be used in our application.

On the windows are supported assistive technology in two ways:

◆ Win32 API (backend = "win32") default backend

◆ MS UI Automation (backend="uia")

If you are not sure in the end the program suitable for the kind of backend, can be done by means of GUI Object Inspector, commonly used inspection tools Inspect.ex, Spy ++ and so on.

This is not too much description, and download all of you to learn, easy to use, very convenient.

Getting Started

After installing the environment, and determine the applicable application backend, then the next Pywinauto we can use this module to automate the operation of the application.

01 connected to the application

Side window with a notepad program as a case for everyone to do the explaining.

Connected application, you first need to use pywinauto.application.Application () to create an application object, and then connect to the application.

The following two methods:

1: Direct opening operation of the application by the start method


 

方式二:连接已经打开应用程序,可以通过connect连接到指定的应用程序


 

运行以上代码,就会自动打开记事本记事本,如下


 

02 窗口选择

打开这个窗口之后,我们要操作该窗口的话,那么就的先选中这个窗口,关于窗口的选择有一下几种方式


 

03 控件操作

程序窗口中的每一块内容,都是一个控件,我们要对这个窗口的某一块内容进行操作,就需要选择到对应的控件。

◆ 获取所有控件

我们可以通过print_control_identifiers()这个方法,来获取这个窗口下的直接子控件,如下:


 

打印出来的结果如下:


 

上面打印出来的内容分别对应窗口种的以一下部分:


 

◆ 选择控件

关于控件选择的方法有好几种,最简单的方法如下:


 

 

◆ 内容输入

在上面选择到编辑窗口之后,我们就可以进行内容输入了

内容输入的方法:type_key()


 

运行结果如下:


 

04 键盘操作

输入完上述内容之后,接下来我们要修改编辑框中的内容

通过键盘完成以下操作:全选(ctrl+A) 复制(Ctrl+C) 粘贴(Ctrl+V) 回车(Enter) 粘贴(Ctrl+V)

◆ pywinauto.keyboard.send_key

pywinauto模拟操作键盘,需要使用到 pywinauto.keyboard.send_keys这个方法


 

运行结果如下:

◆ 常见的按键操作

##### 字母按键用按键小写字母表示

常用的一些按键


 

按键修饰符


 

更多的键盘操作大家可以参考官网文档

05 鼠标操作

pywinauto操作鼠标的所有方法都封装在pywinauto. mouse这个模块中,使用鼠标之前。

首先要导入mouse模块,mouse模块中包含了一系列的鼠标操作事件。

◆ pywinauto. mouse

鼠标移动:move方法

move(coords=(x轴坐标,y轴坐标))

缓慢移动鼠标案例


 

鼠标点击:click


 

 

说明

关于pywinauto模块的基本使用就该大家介绍到这里了,对pc端自动化感兴趣的同学可以往这个方向去深入学习。

本文由柠檬班木森老师原创,转载需注明出处!

让软件测试学习变得更简单!

最好用的HTTP客户端命令行工具

是什么?

扫码即可解锁解题视频

 


 

点一下在看再走吧


 

Guess you like

Origin www.cnblogs.com/nmb123/p/12187599.html