appium 常用API使用总结!

时间:2023-08-16 20:55:38

将常用函数进行适用总结,后期在使用的过程中直接查找调用即可

获取界面属性、控件属性

1、current_activity:获取activity名称

  device.current_activity

2、get_window_size():获取手机屏幕宽、高

  device.get_window_size()

3、tag_name:获取控件的class

  device.find_element_by_name(u"菜单").tag_name

4、text:获取控件的text

  device.find_element_by_id(u"com.android.mms:id/action_compose_new").text

5、get_attribute:获取控件的给定属性或属性

  device.find_element_by_id("com.android.mms:id/action_compose_new").get_attribute("enabled")

  注意点:1、checkable、checked、clickable、enabled、focusable、focused、scollable、long-clickable、password、selected,返回的是布尔值,True或False

      2、.get_attribute("name")  返回的是‘content_desc’的值

      3、.get_attribute("className")  返回的是‘class’的值

6、is_displayed:判断该控件是否对用户可见

  device.find_element_by_id("com.android.mms:id/action_compose_new").is_displayed()    返回True或False

7、size:获取控件的大小

  device.find_element_by_id("com.android.mms:id/action_compose_new").size

8、location:获取控件的位置

  device.find_element_by_id("com.android.mms:id/action_compose_new").location

9、current_context:返回当前会话的当前上下文

  device.current_context

10、context:返回当前会话的当前上下文

  device.context

11、scroll:从元素origin_el滚动至元素destination_el

  '''说明:scroll为横滚动,并且元素不能再同一页面上'''

  device.scroll(self.device.find_element_by_name(u"分类文件"), self.device.find_element_by_name(u"本地"))

12、drag_and_drop:将元素origin_el拖到目标元素destination_el

  device.drag_and_drop(self.device.find_element_by_name(u"主题"), self.device.find_element_by_name(u"QQ音乐"))

13、tap:模拟手指点击

  '''说明:模拟手指点击(最多五个手指),可设置按住时间长度(毫秒),用法:driver.tap([(x,y),(x1,y1)],500)'''

  device.tap([(218,687),])

14、swipe:从A点滑动至B点,滑动时间为毫秒

  device.swipe(418,1067,410,328)

15、flick:按住A点后快速滑动至B点

  device.flick(502,955,486,407)

16、pinch:在元素上执行模拟双指捏(缩小操作)

  device.pinch(self.device.find_element_by_name(u"相机"),300)

17、zoom:在元素上执行放大操作

  device.zoom(self.device.find_element_by_id("id"))

18、press_keycode:发送按键码(安卓仅有),按键码网上搜

  device.press_keycode(3)

19、long_press_keycode:发送一个长按的按键码(长按某键)

  device.long_press_keycode(3)

20、is_app_installed:检查app是否已安装

  device.is_app_installed("com.android.xxx")

21、install_app:安装app

  device.install_app(app_path)

22、remove_app:删除app

  device.remove_app(app_path)

23、close_app:关掉app

  device.close_app ()

24、clear:如果是文本输入元素,就清除文本

  element.clear()

25、network_connection:返回网络的连接类型

  '''返回一个指定网络连接类型的整数位掩码(android)'''

  device.network_connection

26、set_network_connection(connectionType):设置网络的连接(android)

  '''    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               '''

   device.set_network_connection(2)

27、available_ime_engines:返回android设备可用的输入法

  device.available_ime_engines

28、is_ime_active():检查设备是否有输入法服务活动,返回真/假。

  device.is_ime_active()

还有剩余查找元素等的API都比较简单,可在appium的底层进行查看,以上的所有都可以在appium的底层所查找到