How to use _windowpos_to_screenpos method in Airtest

Best Python code snippet using Airtest

win.py

Source:win.py Github

copy

Full Screen

...93 None94 """95 if isinstance(point, (tuple, list)):96 point = Point(x=point[0], y=point[1])97 point = self._windowpos_to_screenpos(point)98 self.mouse.press(button=button, coords=(point.x, point.y))99 time.sleep(duration)100 self.mouse.release(button=button, coords=(point.x, point.y))101 def swipe(self, point1: Union[Tuple[int, int], List, Point], point2: Union[Tuple[int, int], List, Point],102 duration: Union[float, int, None] = 0.01, steps=5):103 """104 滑动一段距离105 Args:106 point1: 起始点107 point2: 终点108 duration: 滑动结束后延迟多久抬起109 steps: 步长110 Returns:111 """112 if isinstance(point1, (tuple, list)):113 point1 = Point(x=point1[0], y=point1[1])114 if isinstance(point2, (tuple, list)):115 point2 = Point(x=point2[0], y=point2[1])116 start = self._windowpos_to_screenpos(point1)117 end = self._windowpos_to_screenpos(point2)118 interval = float(duration) / (steps + 1)119 self.mouse.press(coords=(start.x, start.y))120 time.sleep(interval)121 for i in range(1, steps):122 x = int(start.x + (end.x - start.x) * i / steps)123 y = int(start.y + (end.y - start.y) * i / steps)124 self.mouse.move(coords=(x, y))125 time.sleep(interval)126 self.mouse.move(coords=(end.x, end.y))127 self.mouse.release(coords=(end.x, end.y))128 # # -----------129 # if isinstance(point1, (tuple, list)):130 # point1 = Point(x=point1[0], y=point1[1])131 #132 # if isinstance(point1, (tuple, list)):133 # point2 = Point(x=point2[0], y=point2[1])134 #135 # start = self._windowpos_to_screenpos(point1)136 # end = self._windowpos_to_screenpos(point2)137 # interval = float(duration) / (steps + 1)138 # self.mouse.press(coords=(start.x, start.x))139 # time.sleep(interval)140 # for i in range(1, steps):141 # x = int(start.x + (end.x - start.x) * i / steps)142 # y = int(start.y + (end.y - start.y) * i / steps)143 # self.mouse.move(coords=(x, y))144 # time.sleep(interval)145 # self.mouse.move(coords=(end.x, end.y))146 # self.mouse.release(coords=(end.x, end.y))147 def keyevent(self, keycode: str):148 """149 Perform a key event150 References:151 https://pywinauto.readthedocs.io/en/latest/code/pywinauto.keyboard.html152 Args:153 keycode: key code number or name154 Returns:155 None156 """157 self.keyboard.send_keys(keycode)158 def text(self, text: str):159 """160 输入文字161 Args:162 text: 待输入的文字163 Returns:164 None165 """166 self.keyevent(text)167 def screenshot(self):168 img = Image(self.cap_fun.screenshot())169 if self.cap_method == SCREENSHOT_MODE.WindowsGraphicsCapture:170 # 上,下,左,右 self._window_border171 rect = Rect(x=self._window_border[2],172 y=self._window_border[0],173 width=self._screenshot_size.width,174 height=self._screenshot_size.height)175 return img.crop_image(rect)176 return img177 @staticmethod178 def set_foreground(handle) -> None:179 """180 将指定句柄的窗口带到前台并激活该窗口181 Returns:182 None183 """184 win32gui.SetForegroundWindow(handle)185 @property186 def rect(self) -> Rect:187 """188 获取窗口客户端区域当前所在屏幕的位置189 Returns:190 窗口的位置(以全屏左上角开始为原点的坐标)191 """192 clientRect = win32gui.GetClientRect(self._hwnd)193 point = win32gui.ScreenToClient(self._hwnd, (0, 0))194 rect = Rect(x=abs(point[0]), y=abs(point[1]), width=clientRect[2], height=clientRect[3])195 return rect196 @property197 def title(self) -> str:198 """199 获取窗口名200 Returns:201 窗口名202 """203 return self._top_window.texts()204 def kill(self) -> None:205 """206 关闭窗口207 Returns:208 None209 """210 self.app.kill()211 def _windowpos_to_screenpos(self, pos: Point):212 """213 转换相对坐标为屏幕的绝对坐标214 Args:215 pos: 需要转换的坐标216 Returns:217 Point218 """219 windowpos = self.rect.tl220 pos = pos + windowpos221 return pos222 def get_window_border(self):223 info = GetWindowInfo(self._hwnd)224 print(f'rcWindow: {info.rcWindow.left} {info.rcWindow.top} {info.rcWindow.right} {info.rcWindow.bottom}')225 print(f'rcClient: {info.rcClient.left} {info.rcClient.top} {info.rcClient.right} {info.rcClient.bottom}')...

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