The road to advanced testing: pywinauto helps you open the door to automation on the PC side!
Recently, some little cuties are also asking how to do PC-side automation?
I am curious about this technology, can I use python to automate PC client testing?
The answer is of course yes, so today I have compiled an article here, and talked with you about how to use python to do PC-side automation!
Two, environmental installation
Use python to achieve PC-side automation, provided that python is installed first.
Then you also need to use a third-party library: pywinauto.
Here we first need to install this library, the installation command is as follows:
3. Instructions for use
After the installation is complete, you can refer to the instructions on the official document for instructions on use.
As shown below:
The above content is translated as follows:
That is to say, after we have installed Pywinauto, we must first determine which accessibility technology (pywinauto's backend) can be used in our application.
There are two accessibility technologies supported on windows:
◆ Win32 API (backend="win32") default backend
◆ MS UI Automation (backend="uia")
If you are not sure what kind of backend the program is suitable for, you can do it with the help of GUI object inspection tools. Commonly used inspection tools include Inspect.ex, Spy++, etc.
This is not too much introduction, everyone learns and downloads on their own, it is very simple to use, and very convenient.
Four, quick start
After installing the environment and determining the applicable backend of the application, then we can use the Pywinauto module to automate the application.
01 Connect to the app
Here we use the Notepad program that comes with the window as a case to explain it to everyone.
To connect to an application, you first need to use
pywinauto.application.Application() to create an application object, and then connect to the application.
There are two methods as follows:
Method 1: Open the operating application directly through the start method
Method 2: Connect to the opened application, you can connect to the specified application through connect
Run the above code, it will automatically open Notepad Notepad, as follows
02window selection
After opening this window, if we want to operate the window, then select this window first. There are several ways to select the window.
03Control operation
Each piece of content in the program window is a control. If we want to operate a piece of content in this window, we need to select the corresponding control.
◆ Get all controls
We can use the print_control_identifiers() method to get the direct child controls under this window, as follows:
The printed result is as follows:
The content printed above corresponds to the following parts of the window:
◆ Select control
There are several methods for selecting controls. The simplest method is as follows:
◆ Content input
After selecting the edit window above, we can enter content
Content input method: type_key()
The results of the operation are as follows:
04Keyboard operation
After entering the above content, next we have to modify the content in the edit box
Use the keyboard to complete the following operations: select all (ctrl+A) copy (Ctrl+C) paste (Ctrl+V) enter (Enter) paste (Ctrl+V)
◆ pywinauto.keyboard.send_key
pywinauto simulates the keyboard operation, you need to use
the method pywinauto.keyboard.send_keys
The results of the operation are as follows:
◆ Common key operation
##### Alphabet keys are represented by key lowercase letters
Some commonly used keys
Key modifier
For more keyboard operations, you can refer to the official website document
05Mouse operation
All methods of pywinauto to operate the mouse are encapsulated in the pywinauto. mouse module, before using the mouse.
First, import the mouse module, which contains a series of mouse operation events.
◆ pywinauto. mouse
Mouse movement: move method
move(coords=(x-axis coordinate, y-axis coordinate))
Slow mouse case
Mouse click: click
Five, description
It is time for everyone to introduce the basic use of the pywinauto module here. Students who are interested in PC-side automation can learn in this direction.
Recommend a software testing learning exchange group: 785128166, there are shared videos, interview guidance, test materials, mind maps, and videos in the group. They are all dry goods, you can download and watch. Mainly share test foundation, interface test, performance test, automated test, TestOps architecture, Jmeter, LoadRunner, Fiddler, MySql, Linux, resume optimization, interview skills, and actual video data of large-scale test projects. Use every minute and every second of your time to learn to improve yourself, and don't use "no time" to conceal your mental laziness! Try hard while you are young, and give your future self an explanation!
Public number: Programmer Erhei, after paying attention, you can receive a large amount of learning materials for free.
Good things should be shared with friends