How to use _fix_op_pos method in Airtest

Best Python code snippet using Airtest

win.py

Source:win.py Github

copy

Full Screen

...126 Returns:127 None128 """129 self.keyevent(text)130 def _fix_op_pos(self, pos):131 """Fix operation position."""132 # 如果是全屏的话,就进行双屏修正,否则就正常即可133 if not self.handle:134 pos = list(pos)135 pos[0] = pos[0] + self.monitor["left"]136 pos[1] = pos[1] + self.monitor["top"]137 return pos138 def key_press(self, key):139 """Simulates a key press event.140 Sends a scancode to the computer to report which key has been pressed.141 Some games use DirectInput devices, and respond only to scancodes, not142 virtual key codes. You can simulate DirectInput key presses using this143 method, instead of the keyevent() method, which uses virtual key144 codes.145 :param key: A string indicating which key to be pressed.146 Available key options are:147 {'ESCAPE', '1', '2', '3', '4', '5', '6', '7', '8', '9',148 '0', '-', '=', 'BACKSPACE', 'TAB', 'Q', 'W', 'E', 'R', 'T',149 'Y', 'U', 'I', 'O', 'P', '[', ']', 'ENTER', 'LCTRL', 'A',150 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', "'", '`',151 'LSHIFT', 'BACKSLASH', 'Z', 'X', 'C', 'V', 'B', 'N', 'M',152 ',', '.', '/', 'RSHIFT', '*', 'LALT', 'SPACE', 'CAPS_LOCK',153 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9',154 'F10', 'NUM_LOCK', 'SCROLL_LOCK', 'NUMPAD_7', 'NUMPAD_8',155 'NUMPAD_9', 'NUMPAD_-', 'NUMPAD_4', 'NUMPAD_5', 'NUMPAD_6',156 'NUMPAD_+', 'NUMPAD_1', 'NUMPAD_2', 'NUMPAD_3', 'NUMPAD_0',157 'NUMPAD_.', 'F11', 'F12', 'PRINT_SCREEN', 'PAUSE',158 'NUMPAD_ENTER', 'RCTRL', 'NUMPAD_/', 'RALT', 'HOME', 'UP',159 'PAGE_UP', 'LEFT', 'RIGHT', 'END', 'DOWN', 'PAGE_DOWN',160 'INSERT', 'DELETE', 'LWINDOWS', 'RWINDOWS', 'MENU'}.161 """162 key_press(key)163 def key_release(self, key):164 """Simulates a key release event.165 Sends a scancode to the computer to report which key has been released.166 Some games use DirectInput devices, and respond only to scancodes, not167 virtual key codes. You can simulate DirectInput key releases using this168 method. A call to the key_release() method usually follows a call to169 the key_press() method of the same key.170 :param key: A string indicating which key to be released.171 """172 key_release(key)173 def touch(self, pos, **kwargs):174 """175 Perform mouse click action176 References:177 https://pywinauto.readthedocs.io/en/latest/code/pywinauto.mouse.html178 Args:179 pos: coordinates where to click180 **kwargs: optional arguments181 Returns:182 None183 """184 duration = kwargs.get("duration", 0.01)185 right_click = kwargs.get("right_click", False)186 button = "right" if right_click else "left"187 steps = kwargs.get("steps", 1)188 offset = kwargs.get("offset", 0)189 start = self._action_pos(win32api.GetCursorPos())190 end = self._action_pos(pos)191 start_x, start_y = self._fix_op_pos(start)192 end_x, end_y = self._fix_op_pos(end)193 interval = float(duration) / steps194 time.sleep(interval)195 for i in range(1, steps):196 x = int(start_x + (end_x-start_x) * i / steps)197 y = int(start_y + (end_y-start_y) * i / steps)198 self.mouse.move(coords=(x, y))199 time.sleep(interval)200 self.mouse.move(coords=(end_x, end_y))201 for i in range(1, offset+1):202 self.mouse.move(coords=(end_x+i, end_y+i))203 time.sleep(0.01)204 for i in range(offset):205 self.mouse.move(coords=(end_x+offset-i, end_y+offset-i))206 time.sleep(0.01)207 self.mouse.press(button=button, coords=(end_x, end_y))208 time.sleep(duration)209 self.mouse.release(button=button, coords=(end_x, end_y))210 def double_click(self, pos):211 pos = self._fix_op_pos(pos)212 coords = self._action_pos(pos)213 self.mouse.double_click(coords=coords)214 def right_click(self, pos):215 pos = self._fix_op_pos(pos)216 coords = self._action_pos(pos)217 self.mouse.right_click(coords=coords)218 def hover(self, pos):219 pos = self._fix_op_pos(pos)220 coords = self._action_pos(pos)221 self.mouse.move(coords=coords)222 def swipe(self, p1, p2, duration=0.8, steps=5):223 """224 Perform swipe (mouse press and mouse release)225 Args:226 p1: start point227 p2: end point228 duration: time interval to perform the swipe action229 steps: size of the swipe step230 Returns:231 None232 """233 # 设置坐标时相对于整个屏幕的坐标:234 x1, y1 = self._fix_op_pos(p1)235 x2, y2 = self._fix_op_pos(p2)236 from_x, from_y = self._action_pos(p1)237 to_x, to_y = self._action_pos(p2)238 interval = float(duration) / (steps + 1)239 self.mouse.press(coords=(from_x, from_y))240 time.sleep(interval)241 for i in range(1, steps):242 self.mouse.move(coords=(243 int(from_x + (to_x - from_x) * i / steps),244 int(from_y + (to_y - from_y) * i / steps),245 ))246 time.sleep(interval)247 for i in range(10):248 self.mouse.move(coords=(to_x, to_y))249 time.sleep(interval)250 self.mouse.release(coords=(to_x, to_y))251 """p1 = self._fix_op_pos(p1)252 p2 = self._fix_op_pos(p2)253 from_x, from_y = self._action_pos(p1)254 to_x, to_y = self._action_pos(p2)255 self.mouse.press(coords=(from_x, from_y))256 time.sleep(0.5)257 self.mouse.move(coords=(to_x, to_y))258 time.sleep(0.5)259 self.mouse.release(coords=(to_x, to_y))"""260 def mouse_move(self, pos):261 """Simulates a `mousemove` event.262 Known bug:263 Due to a bug in the pywinauto module, users might experience \264 off-by-one errors when it comes to the exact coordinates of \265 the position on screen.266 :param pos: A tuple (x, y), where x and y are x and y coordinates of...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful