1.Mouse and keyboard operation(python framework)
Airtest mouse and keyboard operation is based on a python frameword pywinauto
(it's installed already with airtest). We can use this framework when the operation is out of airtest framework
Mouse
import pywinauto
# mouse click, coords means the coordinate
pywinauto.mouse.click(button='left', coords=(0, 0))
# double click
pywinauto.mouse.double_click(button='left', coords=(0, 0))
# move mouse to target position
pywinauto.mouse.move(coords=(0, 0))
# right click
pywinauto.mouse.right_click(coords=(0, 0))
# scroll the mouse
pywinauto.mouse.scroll(coords=(0, 0), wheel_dist=1)
Keyboard
Airtest keyevent
method can send key event.
from airtest.core.api import *
keyevent("^a") # ctrl+a
keyevent("{DELETE}") # delete key
pywinauto.keyboard
can also operate keyboard
dev = device()
dev.keyboard.SendKeys("^a")
dev.keyboard.SendKeys("{DELETE}")
2. Mouse hover on some icon
# get user_card
user_card = poco(xxx)
# by .get_position(), we can get the coordinate of user_card
#(left up corner(0,0),right down corner(1,1),all coordinates in the screen is between 0-1)
user_card_pos = user_card.get_position()
# coordinate*resolution,for example, resolution is 1920*1080,
# and the result should be int
x = int(user_card_pos[0] * 1920)
y = int(user_card_pos[1] * 1080)
# use pywinauto mouse module to move mouse
# (pywinauto is already installed inside airtest,no need to install one more time)
mouse.move(coords=(x, y))
Get screen resolution
import win32api, win32con
x_resolution = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
y_resolution = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
put the resolution code into the first one, and we can get:
from pywinauto import mouse
user_card = poco('xxx')
# print(tuple(user_card.get_position()))
x = int(user_card.get_position()[0] * x_resolution)
y = int(user_card.get_position()[1] * y_resolution)
mouse.move(coords=(x, y))
PS: If we use dual monitor, all coordinates are set from the up left corner of the first screen. If your app is run on the second screen, we should calculate the coordinates in other way.