Best Python code snippet using Airtest
win.py
Source:win.py  
...195        right_click = kwargs.get("right_click", False)196        button = "right" if right_click else "left"197        steps = kwargs.get("steps", 1)198        offset = kwargs.get("offset", 0)199        start = self._action_pos(win32api.GetCursorPos())200        end = self._action_pos(pos)201        start_x, start_y = self._fix_op_pos(start)202        end_x, end_y = self._fix_op_pos(end)203        interval = float(duration) / steps204        time.sleep(interval)205        for i in range(1, steps):206            x = int(start_x + (end_x-start_x) * i / steps)207            y = int(start_y + (end_y-start_y) * i / steps)208            self.mouse.move(coords=(x, y))209            time.sleep(interval)210        self.mouse.move(coords=(end_x, end_y))211        for i in range(1, offset+1):212            self.mouse.move(coords=(end_x+i, end_y+i))213            time.sleep(0.01)214        for i in range(offset):215            self.mouse.move(coords=(end_x+offset-i, end_y+offset-i))216            time.sleep(0.01)217        self.mouse.press(button=button, coords=(end_x, end_y))218        time.sleep(duration)219        self.mouse.release(button=button, coords=(end_x, end_y))220    def double_click(self, pos):221        pos = self._fix_op_pos(pos)222        coords = self._action_pos(pos)223        self.mouse.double_click(coords=coords)224    def swipe(self, p1, p2, duration=0.8, steps=5):225        """226        Perform swipe (mouse press and mouse release)227        Args:228            p1: start point229            p2: end point230            duration: time interval to perform the swipe action231            steps: size of the swipe step232        Returns:233            None234        """235        # è®¾ç½®åæ æ¶ç¸å¯¹äºæ´ä¸ªå±å¹çåæ :236        x1, y1 = self._fix_op_pos(p1)237        x2, y2 = self._fix_op_pos(p2)238        from_x, from_y = self._action_pos(p1)239        to_x, to_y = self._action_pos(p2)240        interval = float(duration) / (steps + 1)241        self.mouse.press(coords=(from_x, from_y))242        time.sleep(interval)243        for i in range(1, steps):244            self.mouse.move(coords=(245                int(from_x + (to_x - from_x) * i / steps),246                int(from_y + (to_y - from_y) * i / steps),247            ))248            time.sleep(interval)249        for i in range(10):250            self.mouse.move(coords=(to_x, to_y))251        time.sleep(interval)252        self.mouse.release(coords=(to_x, to_y))253    def mouse_move(self, pos):254        """Simulates a `mousemove` event.255        Known bug:256            Due to a bug in the pywinauto module, users might experience \257            off-by-one errors when it comes to the exact coordinates of \258            the position on screen.259        :param pos: A tuple (x, y), where x and y are x and y coordinates of260                    the screen to move the mouse to, respectively.261        """262        if not isinstance(pos, tuple) or len(pos) != 2:  # pos is not a 2-tuple263            raise ValueError('invalid literal for mouse_move: {}'.format(pos))264        try:265            self.mouse.move(coords=self._action_pos(pos))266        except ValueError:  # in case where x, y are not numbers267            raise ValueError('invalid literal for mouse_move: {}'.format(pos))268    def mouse_down(self, button='left'):269        """Simulates a `mousedown` event.270        :param button: A string indicating which mouse button to be pressed.271                       Available mouse button options are:272                       {'left', 'middle', 'right'}.273        """274        buttons = {'left', 'middle', 'right'}275        if not isinstance(button, str) or button not in buttons:276            raise ValueError('invalid literal for mouse_down(): {}'.format(button))277        else:278            coords = self._action_pos(win32api.GetCursorPos())279            self.mouse.press(button=button, coords=coords)280    def mouse_up(self, button='left'):281        """Simulates a `mouseup` event.282        A call to the mouse_up() method usually follows a call to the283        mouse_down() method of the same mouse button.284        :param button: A string indicating which mouse button to be released.285        """286        buttons = {'left', 'middle', 'right'}287        if not isinstance(button, str) or button not in buttons:288            raise ValueError('invalid literal for mouse_up(): {}'.format(button))289        else:290            coords = self._action_pos(win32api.GetCursorPos())291            self.mouse.release(button=button, coords=coords)292    def start_app(self, path, **kwargs):293        """294        Start the application295        Args:296            path: full path to the application297            kwargs: reference: https://pywinauto.readthedocs.io/en/latest/code/pywinauto.application.html#pywinauto.application.Application.start298        Returns:299            None300        """301        self.app = self._app.start(path, **kwargs)302    def stop_app(self, pid):303        """304        Stop the application305        Args:306            pid: process ID of the application to be stopped307        Returns:308            None309        """310        self._app.connect(process=pid).kill()311    @require_app312    def set_foreground(self):313        """314        Bring the window foreground315        Returns:316            None317        """318        SetForegroundWindow(self._top_window)319    def get_rect(self):320        """321        Get rectangle322        Returns:323            win32structures.RECT324        """325        if self.app and self._top_window:326            return self._top_window.rectangle()327        else:328            return RECT(right=GetSystemMetrics(0), bottom=GetSystemMetrics(1))329    @require_app330    def get_title(self):331        """332        Get the window title333        Returns:334            window title335        """336        return self._top_window.texts()337    @require_app338    def get_pos(self):339        """340        Get the window position coordinates341        Returns:342            coordinates of topleft corner of the window (left, top)343        """344        rect = self.get_rect()345        return (rect.left, rect.top)346    @require_app347    def move(self, pos):348        """349        Move window to given coordinates350        Args:351            pos: coordinates (x, y) where to move the window352        Returns:353            None354        """355        self._top_window.MoveWindow(x=pos[0], y=pos[1])356    @require_app357    def kill(self):358        """359        Kill the application360        Returns:361            None362        """363        self.app.kill()364    def _action_pos(self, pos):365        if self.app:366            pos = self._windowpos_to_screenpos(pos)367        pos = (int(pos[0]), int(pos[1]))368        return pos369    # @property370    # def handle(self):371    #     return self._top_window.handle372    @property373    def focus_rect(self):374        return self._focus_rect375    @focus_rect.setter376    def focus_rect(self, value):377        # set focus rect to get rid of window border378        assert len(value) == 4, "focus rect must be in [left, top, right, bottom]"...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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
