XII. Automated Testing for PC Games on Windows¶
1.Preface¶
Before reading this article, we strongly recommend that you to back to our previous tutorial on how to test Windows applications to review how to connect Windows windows in AirtestIDE, and how to call APIs for Windows operations. This will help you better understand the content of this article.
2.Newly Added Instructions¶
Airtest has always supported testing for Windows applications, but when it comes to testing PC games that require DirectInput devices, the previously provided methods cannot effectively simulate keyboard and mouse input. Therefore, we have added new APIs to support testing for such PC games.
1) key_press(key) and key_release(key)¶
Some PC games only accept keyboard scan codes sent from the hardware layer and do not respond to virtual key codes sent from the operating system layer through libraries such as pywinauto used in keyevent(). Therefore, we have provided two new APIs, key_press(key) and key_release(key), to simulate keyboard key press and release by sending keyboard scan codes. Some games require certain operations to be performed between key press and release, so we have split keyevent(…) into two steps, press and release, which is more accurate than passing a duration parameter. As these APIs are currently exclusive to the Windows platform, they have not been included in the public APIs. To use them, you need to first obtain the device, and then call the methods to simulate the corresponding keyboard operations.
win = device()
win.key_press('W')
... // some operations
win.key_release('W')
You can find the specific types of parameters accepted by key_press(key) and key_release(key) in the official Windows platform API documentation.
2) mouse_move(pos), mouse_down(button), and mouse_up(button)¶
Some 3D games require controlling the camera by moving the mouse and performing operations such as shooting by clicking and releasing mouse buttons. To cope with this, we extracted some logic from swipe(…) and touch(…) and encapsulated them into three new commands, mouse_move(pos), mouse_down(button), and mouse_up(button), to simulate mouse operations. These three commands can also be found in the official Windows platform API documentation. In some games with strict requirements for camera movement, such as aiming at enemies for shooting by moving the mouse, repeated debugging or algorithms may be required to determine the specific value of the pos parameter. Similarly, when simulating certain mouse operations, you also need to first obtain the device and then simulate the corresponding operations.
import win32api
win = device()
x, y = win32api.GetCursorPos()
win.mouse_move((x+10, y+10))
win.mouse_down('right')
... // some operations
win.mouse_up('right')
3.Common Issues and Solutions¶
1) Game window embedding issue¶
Some games may encounter issues when using the window embedded mode, including the inability to select the window or the disappearance of the game window. If the window cannot be selected, try searching for it. If the entire game window disappears after the connection, try using the non-embedded mode. If the window still cannot be connected, try the desktop mode.
Search for window
Non-embedded mode
Desktop mode
2) Image script in the desktop mode¶
If your script involves image recognition and you are using the desktop mode, it may recognize the images in the script instead of those in the game screen when running the script. In this case, right click the script and choose "Switch to Code Mode" under "Image/Code Mode" to run the script smoothly.
3) Cursor limitations in PC games¶
Translation: Some PC games have cursor restrictions that prevent players from moving the cursor outside the game screen. When writing scripts, you can use the Tab+Alt method to switch windows.