Best Python code snippet using Airtest
android.py
Source:android.py  
...151        """152        if self._screen_proxy:153            self._screen_proxy.teardown_stream()154        self._screen_proxy = ScreenProxy.auto_setup(self.adb, default_method=cap_method)155    def get_deprecated_var(self, old_name, new_name):156        """157        Get deprecated class variables158        When the airtest version number>=1.1.2, the call device.minicap/device.javacap is removed, and relevant compatibility is made here, and DeprecationWarning is printed159        airtestçæ¬å·>=1.1.2æ¶ï¼å»æäºdevice.minicap/device.javacapè¿æ ·çè°ç¨ï¼å¨æ¤åäºç¸å
³çå
¼å®¹ï¼å¹¶æå°DeprecationWarning160        Usage:  Android.minicap=property(lambda self: self.get_deprecated_var("minicap", "screen_proxy"))161        Args:162            old_name: "minicap"163            new_name: "screen_proxy"164        Returns:165            New implementation of deprecated object, e.g self.minicap -> self.screen_proxy166            dev.minicap.get_frame_from_stream()  ->  dev.screen_proxy.get_frame_from_stream()167        Examples:168            >>> dev = Android()169            >>> isinstance(dev.minicap, ScreenProxy)  # True170            >>> dev.minicap.get_frame_from_stream()  # --> dev.screen_proxy.get_frame_from_stream()171        """172        warnings.simplefilter("always")173        warnings.warn("{old_name} is deprecated, use {new_name} instead".format(old_name=old_name, new_name=new_name),174                      DeprecationWarning)175        return getattr(self, new_name)176    def get_default_device(self):177        """178        Get local default device when no serialno179        Returns:180            local device serialno181        """182        if not ADB().devices(state="device"):183            raise IndexError("ADB devices not found")184        return ADB().devices(state="device")[0][0]185    @property186    def uuid(self):187        """188        Serial number189        :return:190        """191        ult = [self.serialno]192        if self.display_id:193            ult.append(self.display_id)194        if self.input_event:195            ult.append(self.input_event)196        return "_".join(ult)197    def list_app(self, third_only=False):198        """199        Return list of packages200        Args:201            third_only: if True, only third party applications are listed202        Returns:203            array of applications204        """205        return self.adb.list_app(third_only)206    def path_app(self, package):207        """208        Print the full path to the package209        Args:210            package: package name211        Returns:212            the full path to the package213        """214        return self.adb.path_app(package)215    def check_app(self, package):216        """217        Check if package exists on the device218        Args:219            package: package name220        Returns:221            True if package exists on the device222        Raises:223             AirtestError: raised if package is not found224        """225        return self.adb.check_app(package)226    def start_app(self, package, activity=None):227        """228        Start the application and activity229        Args:230            package: package name231            activity: activity name232        Returns:233            None234        """235        return self.adb.start_app(package, activity)236    def start_app_timing(self, package, activity):237        """238        Start the application and activity, and measure time239        Args:240            package: package name241            activity: activity name242        Returns:243            app launch time244        """245        return self.adb.start_app_timing(package, activity)246    def stop_app(self, package):247        """248        Stop the application249        Args:250            package: package name251        Returns:252            None253        """254        return self.adb.stop_app(package)255    def clear_app(self, package):256        """257        Clear all application data258        Args:259            package: package name260        Returns:261            None262        """263        return self.adb.clear_app(package)264    def install_app(self, filepath, replace=False, install_options=None):265        """266        Install the application on the device267        Args:268            filepath: full path to the `apk` file to be installed on the device269            replace: True or False to replace the existing application270            install_options: list of options, default is []271        Returns:272            output from installation process273        """274        return self.adb.install_app(filepath, replace=replace, install_options=install_options)275    def install_multiple_app(self, filepath, replace=False, install_options=None):276        """277        Install multiple the application on the device278        Args:279            filepath: full path to the `apk` file to be installed on the device280            replace: True or False to replace the existing application281            install_options: list of options, default is []282        Returns:283            output from installation process284        """285        return self.adb.install_multiple_app(filepath, replace=replace, install_options=install_options)286    def uninstall_app(self, package):287        """288        Uninstall the application from the device289        Args:290            package: package name291        Returns:292            output from the uninstallation process293        """294        return self.adb.uninstall_app(package)295    def snapshot(self, filename=None, ensure_orientation=True, quality=10, max_size=None):296        """297        Take the screenshot of the display. The output is send to stdout by default.298        Args:299            filename: name of the file where to store the screenshot, default is None which is stdout300            ensure_orientation: True or False whether to keep the orientation same as display301            quality: The image quality, integer in range [1, 99]302            max_size: the maximum size of the picture, e.g 1200303        Returns:304            screenshot output305        """306        # default not write into file.307        screen = self.screen_proxy.snapshot(ensure_orientation=ensure_orientation)308        if filename:309            aircv.imwrite(filename, screen, quality, max_size=max_size)310        return screen311    def shell(self, *args, **kwargs):312        """313        Return `adb shell` interpreter314        Args:315            *args: optional shell commands316            **kwargs: optional shell commands317        Returns:318            None319        """320        return self.adb.shell(*args, **kwargs)321    def keyevent(self, keyname, **kwargs):322        """323        Perform keyevent on the device324        Args:325            keyname: keyevent name326            **kwargs: optional arguments327        Returns:328            None329        """330        self.adb.keyevent(keyname)331    def wake(self):332        """333        Perform wake up event334        Returns:335            None336        """337        # å
å°è¯è¿ç»åé224å82è§£éå±å¹ï¼å¦æè§£é失败ï¼åè¯ç¨yosemiteè§£é338        self.adb.keyevent("KEYCODE_WAKEUP")339        if self.adb.is_locked():340            self.adb.keyevent("KEYCODE_MENU")341            time.sleep(0.5)342        if self.adb.is_locked():343            self.home()344            self.recorder.install_or_upgrade()  # ææ¶Yosemiteåªç¨äºime345            self.adb.shell(['am', 'start', '-a', 'com.netease.nie.yosemite.ACTION_IDENTIFY'])346            time.sleep(0.5)347            self.home()348    def home(self):349        """350        Press HOME button351        Returns:352            None353        """354        self.keyevent("HOME")355    def text(self, text, enter=True, **kwargs):356        """357        Input text on the device358        Args:359            text: text to input360            enter: True or False whether to press `Enter` key361            search: True or False whether to press `Search` key on IME after input362        Returns:363            None364        """365        search = False if "search" not in kwargs else kwargs["search"]366        if self.ime_method == IME_METHOD.YOSEMITEIME:367            try:368                self.yosemite_ime.text(text)369            except AdbError:370                # é¨åææºå¦oppo/vivoçï¼å¨æ²¡æå®è£
/å¯ç¨yosemiteè¾å
¥æ³æ¶æ æ³ä½¿ç¨ï¼æ¹ç¨adb shell input textè¾å
¥371                self.adb.text(text)372        else:373            self.adb.text(text)374        if search:375            try:376                self.yosemite_ime.code("3")377            except AdbError:378                self.adb.shell(["input", "keyevent", "84"])379        # 游æè¾å
¥æ¶ï¼è¾å
¥ææå
容åç¹å»Enter确认ï¼å¦ä¸éè¦ï¼enter置为Falseå³å¯ã380        if enter:381            self.adb.shell(["input", "keyevent", "ENTER"])382    def touch(self, pos, duration=0.01):383        """384        Perform touch event on the device385        Args:386            pos: coordinates (x, y)387            duration: how long to touch the screen388        Returns:389            None390        """391        self.touch_proxy.touch(pos, duration)392    def double_click(self, pos):393        self.touch(pos)394        time.sleep(0.05)395        self.touch(pos)396    def swipe(self, p1, p2, duration=0.5, steps=5, fingers=1):397        """398        Perform swipe event on the device399        Args:400            p1: start point401            p2: end point402            duration: how long to swipe the screen, default 0.5403            steps: how big is the swipe step, default 5404            fingers: the number of fingers. 1 or 2.405        Returns:406            None407        """408        self.touch_proxy.swipe(p1, p2, duration=duration, steps=steps, fingers=fingers)409    def pinch(self, center=None, percent=0.5, duration=0.5, steps=5, in_or_out='in'):410        """411        Perform pinch event on the device, only for minitouch and maxtouch412        Args:413            center: the center point of the pinch operation414            percent: pinch distance to half of screen, default is 0.5415            duration: time interval for swipe duration, default is 0.8416            steps: size of swipe step, default is 5417            in_or_out: pinch in or pinch out, default is 'in'418        Returns:419            None420        Raises:421            TypeError: An error occurred when center is not a list/tuple or None422        """423        self.touch_proxy.pinch(center=center, percent=percent, duration=duration, steps=steps, in_or_out=in_or_out)424    def swipe_along(self, coordinates_list, duration=0.8, steps=5):425        """426        Perform swipe event across multiple points in sequence, only for minitouch and maxtouch427        Args:428            coordinates_list: list of coordinates: [(x1, y1), (x2, y2), (x3, y3)]429            duration: time interval for swipe duration, default is 0.8430            steps: size of swipe step, default is 5431        Returns:432            None433        """434        self.touch_proxy.swipe_along(coordinates_list, duration=duration, steps=steps)435    def two_finger_swipe(self, tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, offset=(0, 50)):436        """437        Perform two finger swipe action, only for minitouch and maxtouch438        Args:439            tuple_from_xy: start point440            tuple_to_xy: end point441            duration: time interval for swipe duration, default is 0.8442            steps: size of swipe step, default is 5443            offset: coordinate offset of the second finger, default is (0, 50)444        Returns:445            None446        """447        self.touch_proxy.two_finger_swipe(tuple_from_xy, tuple_to_xy, duration=duration, steps=steps, offset=offset)448    def logcat(self, *args, **kwargs):449        """450        Perform `logcat`operations451        Args:452            *args: optional arguments453            **kwargs: optional arguments454        Returns:455            `logcat` output456        """457        return self.adb.logcat(*args, **kwargs)458    def getprop(self, key, strip=True):459        """460        Get properties for given key461        Args:462            key: key name463            strip: True or False whether to strip the output or not464        Returns:465            property value(s)466        """467        return self.adb.getprop(key, strip)468    def get_ip_address(self):469        """470        Perform several set of commands to obtain the IP address471            * `adb shell netcfg | grep wlan0`472            * `adb shell ifconfig`473            * `adb getprop dhcp.wlan0.ipaddress`474        Returns:475            None if no IP address has been found, otherwise return the IP address476        """477        return self.adb.get_ip_address()478    def get_top_activity(self):479        """480        Get the top activity481        Returns:482            (package, activity, pid)483        """484        return self.adb.get_top_activity()485    def get_top_activity_name(self):486        """487        Get the top activity name488        Returns:489            package/activity490        """491        tanp = self.get_top_activity()492        if tanp:493            return tanp[0] + '/' + tanp[1]494        else:495            return None496    def is_keyboard_shown(self):497        """498        Return True or False whether soft keyboard is shown or not499        Notes:500            Might not work on all devices501        Returns:502            True or False503        """504        return self.adb.is_keyboard_shown()505    def is_screenon(self):506        """507        Return True or False whether the screen is on or not508        Notes:509            Might not work on all devices510        Returns:511            True or False512        """513        return self.adb.is_screenon()514    def is_locked(self):515        """516        Return True or False whether the device is locked or not517        Notes:518            Might not work on some devices519        Returns:520            True or False521        """522        return self.adb.is_locked()523    def unlock(self):524        """525        Unlock the device526        Notes:527            Might not work on all devices528        Returns:529            None530        """531        return self.adb.unlock()532    @property533    def display_info(self):534        """535        Return the display info (width, height, orientation and max_x, max_y)536        Returns:537            display information538        """539        if not self._display_info:540            self._display_info = self.get_display_info()541        display_info = copy(self._display_info)542        # update ow orientation, which is more accurate543        if self._current_orientation is not None:544            display_info.update({545                "rotation": self._current_orientation * 90,546                "orientation": self._current_orientation,547            })548        return display_info549    def get_display_info(self):550        """551        Return the display info (width, height, orientation and max_x, max_y)552        Returns:553            display information554        """555        self.rotation_watcher.get_ready()556        return self.adb.get_display_info()557    def get_current_resolution(self):558        """559        Return current resolution after rotation560        Returns:561            width and height of the display562        """563        # 注æé»è¾¹é®é¢ï¼éè¦ç¨å®åæ¥å£è·ååºå两ç§å辨ç564        w, h = self.display_info["width"], self.display_info["height"]565        if self.display_info["orientation"] in [1, 3]:566            w, h = h, w567        return w, h568    def get_render_resolution(self, refresh=False):569        """570        Return render resolution after rotation571        Args:572            refresh: whether to force refresh render resolution573        Returns:574            offset_x, offset_y, offset_width and offset_height of the display575        """576        if refresh or 'offset_x' not in self._display_info:577            self.adjust_all_screen()578        x, y, w, h = self._display_info.get('offset_x', 0), \579            self._display_info.get('offset_y', 0), \580            self._display_info.get('offset_width', 0), \581            self._display_info.get('offset_height', 0)582        if self.display_info["orientation"] in [1, 3]:583            x, y, w, h = y, x, h, w584        return x, y, w, h585    def start_recording(self, max_time=1800, bit_rate_level=1, bit_rate=None):586        """587        Start recording the device display588        Args:589            max_time: maximum screen recording time, default is 1800590            bit_rate_level: bit_rate=resolution*level, 0 < level <= 5, default is 1591            bit_rate: the higher the bitrate, the clearer the video592        Returns:593            None594        Examples:595            Record 30 seconds of video and export to the current directory test.mp4::596            >>> from airtest.core.api import connect_device, sleep597            >>> dev = connect_device("Android:///")598            >>> # Record the screen with the lowest quality599            >>> dev.start_recording(bit_rate_level=1)600            >>> sleep(30)601            >>> dev.stop_recording(output="test.mp4")602            Or set max_time=30, the screen recording will stop automatically after 30 seconds::603            >>> dev.start_recording(max_time=30, bit_rate_level=5)604            >>> dev.stop_recording(output="test_30s.mp4")605            The default value of `max_time` is 1800 seconds, so the maximum screen recording time is half an hour.606            You can modify its value to obtain a longer screen recording::607            >>> dev.start_recording(max_time=3600, bit_rate_level=5)608            >>> dev.stop_recording(output="test_hour.mp4")609        """610        if not bit_rate:611            if bit_rate_level > 5:612                bit_rate_level = 5613            bit_rate = self.display_info['width'] * self.display_info['height'] * bit_rate_level614        return self.recorder.start_recording(max_time=max_time, bit_rate=bit_rate)615    def stop_recording(self, output="screen.mp4", is_interrupted=False):616        """617        Stop recording the device display. Recoding file will be kept in the device.618        Args:619            output: default file is `screen.mp4`620            is_interrupted: True or False. Stop only, no pulling recorded file from device.621        Returns:622            None623        """624        return self.recorder.stop_recording(output=output, is_interrupted=is_interrupted)625    def _register_rotation_watcher(self):626        """627        Register callbacks for Android and minicap when rotation of screen has changed628        callback is called in another thread, so be careful about thread-safety629        Returns:630            None631        """632        self.rotation_watcher.reg_callback(lambda x: setattr(self, "_current_orientation", x))633    def _touch_point_by_orientation(self, tuple_xy):634        """635        Convert image coordinates to physical display coordinates, the arbitrary point (origin) is upper left corner636        of the device physical display637        Args:638            tuple_xy: image coordinates (x, y)639        Returns:640        """641        x, y = tuple_xy642        x, y = XYTransformer.up_2_ori(643            (x, y),644            (self.display_info["width"], self.display_info["height"]),645            self.display_info["orientation"]646        )647        return x, y648    def adjust_all_screen(self):649        """650        Adjust the render resolution for all_screen device.651        Return:652            None653        """654        info = self.display_info655        ret = self.adb.get_display_of_all_screen(info)656        if ret:657            info.update(ret)658            self._display_info = info659# Compatible with old code, such as device.minicap660Android.minicap=property(lambda self: self.get_deprecated_var("minicap", "screen_proxy"))661Android.javacap=property(lambda self: self.get_deprecated_var("javacap", "screen_proxy"))662Android.minitouch=property(lambda self: self.get_deprecated_var("minitouch", "touch_proxy"))...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!!
