Python侧开28期-偕行-学习笔记-appium设备交互API

设备交互 Api

  • 测试过程中模拟来电、来短信
  • 模拟网络切换
  • 横竖屏切换
  • App处理
  • 录屏
  • 运行过程中获取系统日志
  • 截图
  • 剪切板操作
  • Android原生模拟器控制

1、关于driver–说在前面

(1)先来看WebDriver类的源码

  • WebDirver类继承了N多个类,设备交互的api就有特定的类与之对应;
    • ActionHelpers:更多鼠标动作处理
    • Applications:安装(应用程序)包管理
    • Clipboard:剪切板
    • Common:里面有通知栏处理
    • Gsm:原生模拟器电点处理
    • Keyboard:键盘操作处理
    • LogEvent:日志处理
    • Network:网络处理
    • Power:原生模拟器电量处理
    • ScreenRecord:录屏处理
    • Sms:原生模拟器短线处理
    • SystemBars:系统状态栏处理
class WebDriver(
    webdriver.Remote,
    ActionHelpers,
    Activities,
    Applications,
    Clipboard,
    Context,
    Common,
    DeviceTime,
    Display,
    ExecuteDriver,
    ExecuteMobileCommand,
    Gsm,
    HardwareActions,
    ImagesComparison,
    Keyboard,
    Location,
    LogEvent,
    Network,
    Performance,
    Power,
    RemoteFS,
    ScreenRecord,
    Session,
    Settings,
    Sms,
    SystemBars,
):

(2)设备交互api索引

  • 说明:这部分api是appium1.x中的api,appium2.x中或许有些改动,之所以列在这里是为了知道appium都能进行哪些设备交互,然后具体的用法到appium2.x源码中找对应方法就行;

2、 模拟电话、短信

  • appium可以模拟来电话,来短信功能,在app运行过程中收到短信/电话,app如何做处理的,专属的一些场景
  • 只支持原生模拟器,不支持mumu,genimotion等

(1)模拟电话

  • 先上源码:
class GsmCallActions:
    CALL = 'call'
    ACCEPT = 'accept'
    CANCEL = 'cancel'
    HOLD = 'hold'

class Gsm(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
    def make_gsm_call(self, phone_number: str, action: str) -> 'WebDriver':
        """Make GSM call (Emulator only)

        Android only.

        Args:
            phone_number: The phone number to call to.
            action: The call action.
                A member of the const `appium.webdriver.extensions.android.gsm.GsmCallActions`

        Usage:
            self.driver.make_gsm_call('5551234567', GsmCallActions.CALL)

        Returns:
            Union['WebDriver', 'Gsm']: Self instance
        """
        ext_name = 'mobile: gsmCall'
        constants = extract_const_attributes(GsmCallActions)
        if action not in constants.values():
            logger.warning(
                f'{action} is unknown. Consider using one of {list(constants.keys())} constants. '
                f'(e.g. {GsmCallActions.__name__}.CALL)'
            )
        args = {'phoneNumber': phone_number, 'action': action}
        try:
            self.assert_extension_exists(ext_name).execute_script(ext_name, args)
        except UnknownMethodException:
            # TODO: Remove the fallback
            self.mark_extension_absence(ext_name).execute(Command.MAKE_GSM_CALL, args)
        return cast('WebDriver', self)
  • 具体用法:
driver.make_gsm_call("55512345678",GsmCallActions.CALL)
driver.make_gsm_call("55512345678",GsmCallActions.ACCEPT)
driver.make_gsm_call("55512345678",GsmCallActions.CANCEL)
driver.make_gsm_call("55512345678",GsmCallActions.HOLD)

(2)模拟短线

  • 先上源码:
class Sms(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
    def send_sms(self, phone_number: str, message: str) -> 'WebDriver':
        """Emulate send SMS event on the connected emulator.

        Android only.

        Args:
            phone_number: The phone number of message sender
            message: The message to send

        Usage:
            self.driver.send_sms('555-123-4567', 'Hey lol')

        Returns:
            Union['WebDriver', 'Sms']: Self instance
        """
        ext_name = 'mobile: sendSms'
        args = {'phoneNumber': phone_number, 'message': message}
        try:
            self.assert_extension_exists(ext_name).execute_script(ext_name, args)
        except UnknownMethodException:
            # TODO: Remove the fallback
            self.mark_extension_absence(ext_name).execute(Command.SEND_SMS, args)
        return cast('WebDriver', self)
  • 具体用法:
driver.send_sms("555-123-4567", "Appium Test")

3、 网络设置

  • 先上源码:
class NetSpeed:
    GSM = 'gsm'  # GSM/CSD (up: 14.4(kbps), down: 14.4(kbps))
    SCSD = 'scsd'  # HSCSD (up: 14.4, down: 57.6)
    GPRS = 'gprs'  # GPRS (up: 28.8, down: 57.6)
    EDGE = 'edge'  # EDGE/EGPRS (up: 473.6, down: 473.6)
    UMTS = 'umts'  # UMTS/3G (up: 384.0, down: 384.0)
    HSDPA = 'hsdpa'  # HSDPA (up: 5760.0, down: 13,980.0)
    LTE = 'lte'  # LTE (up: 58,000, down: 173,000)
    EVDO = 'evdo'  # EVDO (up: 75,000, down: 280,000)
    FULL = 'full'  # No limit, the default (up: 0.0, down: 0.0)


class NetworkMask:
    WIFI = 0b010
    DATA = 0b100
    AIRPLANE_MODE = 0b001


class Network(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
    @property
    def network_connection(self) -> int:
        """Returns an integer bitmask specifying the network connection type.

        Android only.
        Possible values are available through the enumeration `appium.webdriver.ConnectionType`

        This API only works reliably on emulators (any version) and real devices
        since API level 31.
        """
        ext_name = 'mobile: getConnectivity'
        try:
            result_map = self.assert_extension_exists(ext_name).execute_script(ext_name)
            return (
                (NetworkMask.WIFI if result_map['wifi'] else 0)
                | (NetworkMask.DATA if result_map['data'] else 0)
                | (NetworkMask.AIRPLANE_MODE if result_map['airplaneMode'] else 0)
            )
        except UnknownMethodException:
            # TODO: Remove the fallback
            return self.mark_extension_absence(ext_name).execute(Command.GET_NETWORK_CONNECTION, {})['value']

    def set_network_connection(self, connection_type: int) -> int:
        """Sets the network connection type. Android only.

        Possible values:

            +--------------------+------+------+---------------+
            | Value (Alias)      | Data | Wifi | Airplane Mode |
            +====================+======+======+===============+
            | 0 (None)           | 0    | 0    | 0             |
            +--------------------+------+------+---------------+
            | 1 (Airplane Mode)  | 0    | 0    | 1             |
            +--------------------+------+------+---------------+
            | 2 (Wifi only)      | 0    | 1    | 0             |
            +--------------------+------+------+---------------+
            | 4 (Data only)      | 1    | 0    | 0             |
            +--------------------+------+------+---------------+
            | 6 (All network on) | 1    | 1    | 0             |
            +--------------------+------+------+---------------+

        These are available through the enumeration `appium.webdriver.ConnectionType`

        This API only works reliably on emulators (any version) and real devices
        since API level 31.

        Args:
            connection_type: a member of the enum `appium.webdriver.ConnectionType`

        Return:
            int: Set network connection type
        """
        ext_name = 'mobile: setConnectivity'
        try:
            return self.assert_extension_exists(ext_name).execute_script(
                ext_name,
                {
                    'wifi': bool(connection_type & NetworkMask.WIFI),
                    'data': bool(connection_type & NetworkMask.DATA),
                    'airplaneMode': bool(connection_type & NetworkMask.AIRPLANE_MODE),
                },
            )
        except UnknownMethodException:
            # TODO: Remove the fallback
            return self.mark_extension_absence(ext_name).execute(
                Command.SET_NETWORK_CONNECTION, {'parameters': {'type': connection_type}}
            )['value']

    def toggle_wifi(self) -> 'WebDriver':
        """Toggle the wifi on the device, Android only.
        This API only works reliably on emulators (any version) and real devices
        since API level 31.

        Returns:
            Union['WebDriver', 'Network']: Self instance
        """
        ext_name = 'mobile: setConnectivity'
        try:
            self.assert_extension_exists(ext_name).execute_script(
                ext_name, {'wifi': not (self.network_connection & NetworkMask.WIFI)}
            )
        except UnknownMethodException:
            self.mark_extension_absence(ext_name).execute(Command.TOGGLE_WIFI, {})
        return cast('WebDriver', self)

    def set_network_speed(self, speed_type: str) -> 'WebDriver':
        """Set the network speed emulation.

        Android Emulator only.

        Args:
            speed_type: The network speed type.
                A member of the const appium.webdriver.extensions.android.network.NetSpeed.

        Usage:
            self.driver.set_network_speed(NetSpeed.LTE)

        Returns:
            Union['WebDriver', 'Network']: Self instance
        """
        constants = extract_const_attributes(NetSpeed)
        if speed_type not in constants.values():
            logger.warning(
                f'{speed_type} is unknown. Consider using one of {list(constants.keys())} constants. '
                f'(e.g. {NetSpeed.__name__}.LTE)'
            )
        ext_name = 'mobile: networkSpeed'
        try:
            self.assert_extension_exists(ext_name).execute_script(ext_name, {'speed': speed_type})
        except UnknownMethodException:
            # TODO: Remove the fallback
            self.mark_extension_absence(ext_name).execute(Command.SET_NETWORK_SPEED, {'netspeed': speed_type})
        return cast('WebDriver', self)
  • 具体用法:
driver.set_network_connection(6)

4、 横竖屏切换

  • 先上源码,webdriver.py模块中源码:
# MJSONWP for Selenium v4
    @property
    def orientation(self) -> str:
        """
        Gets the current orientation of the device
        :Usage:
            ::
                orientation = driver.orientation
        """
        return self.execute(Command.GET_SCREEN_ORIENTATION)['value']

    # MJSONWP for Selenium v4
    @orientation.setter
    def orientation(self, value: str) -> None:
        """
        Sets the current orientation of the device
        :Args:
         - value: orientation to set it to.
        :Usage:
            ::
                driver.orientation = 'landscape'
        """
        allowed_values = ['LANDSCAPE', 'PORTRAIT']
        if value.upper() in allowed_values:
            self.execute(Command.SET_SCREEN_ORIENTATION, {'orientation': value})
        else:
            raise WebDriverException('You can only set the orientation to \'LANDSCAPE\' and \'PORTRAIT\'')
  • 具体用法:
    • 说明:源码中虽然有这样的api,但是在selenium的driver中,不在appium的driver中!!!
# 设置为横盘
driver.orientation("LANDSCAPE")
# 设置为竖屏
driver.orientation("PORTRAIT")
# 获取横竖屏状态
driver.orientation()

5、日志处理

# 设置日志事件--在 Appium 服务器上记录自定义事件
driver.log_event('appium', 'funEvent')
# 获取可用日志类型的列表。这仅适用于 w3c兼容浏览器
driver.log_types
# 通过给定的日志类型获取日志
driver.get_log('browser')
driver.get_log('driver')
driver.get_log('client')
driver.get_log('server')

6、app操作

  • 注意:新版本貌似弃用了很多app的操作方法,比如:launch_app、background_app、reset、remove_app等等;
# 根据apk路径安装app
driver.install_app("C:/Users/DELL/Downloads/测试apk/touchaction.apk")
# 检查app是否被安装
driver.is_app_installed("cn.kmob.screenfingermovelock")
# 激活app,如果应用程序未运行或者在后台运行
driver.activate_app("com.xueqiu.android")
# 查看app的状态
driver.query_app_state("com.xueqiu.android")

# 返回上一页,相当于点击手机的返回键一次
driver.back()
# 关闭--但是调用了之后app的进程并没有被杀死,why?是capability配置的问题吗?---确实是capability的问题,在appium2.x中需要新增两个配置参数;
# 设置以下两个参数来控制启动app和关闭掉app
#caps["appium:forceAppLaunch"] = True
#caps["appium:shouldTerminateApp"] = True
driver.quit()

7、剪切板操作

# 设置剪切板内容
with open("data.txt",'rb',encoding='utf-8') as f:
    data = f.read()
# 设置二进制数据
driver.set_clipboard(content=data)
# 设置字符串数据
driver.set_clipboard_text("字符串数据")
# 获取剪切板内容
# 获取剪切板的二进制数据
driver.get_clipboard()
# 获取剪切板的字符串数据
driver.get_clipboard_text()

8、 其它常用操作

(1)锁屏

# 解锁设备。如果设备已锁定,则不会进行任何更改。
driver.unlock()
# 锁定设备5s。如果设备已解锁,则不会进行任何更改
driver.lock(5)
# 检查设备是否已锁定
driver.is_locked()

(2) 截图

  • driver.get_screenshot_as_file('./photos/img.png')
    image

(3)录屏

  • 录屏:模拟器需要 androidAPI>27,华为不支持,只支持 8.0以上的版本
    • 开始录制:self.driver.start_recording_screen()
    • 结束录制:self.driver.stop_recording_screen()

9、Android原生模拟器控制

(1) android 模拟器创建

  • Android Studio
  • 在命令行启动模拟器
    • emulator -list-avds 模拟器列表
    • emulator ‘@foo’ or ‘-avd foo’

(2)配置

  • capability里面需要配置
    • avd: ‘模拟器名’
  • 注意自动启动模拟器,只能是sdk的模拟器,第三方模拟器不支持,原生7.0版本不支持