appium基本使用(Android)

一.环境搭建

详情可见:https://www.cnblogs.com/lihongtaoya/p/16971096.html

二.元素定位

详情可见:https://www.cnblogs.com/lihongtaoya/p/16988850.html

三.启动app基本配置

1.使用时首先导入webdriver模块

from appium import webdriver

2.通过webdriver调用Remote()完成初始化配置

driver = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=desired)

其中command_executor参数一般固定为http://127.0.0.1:4723/wd/hub,desired_capabilities则为字典类型的配置。

desired = {
    "platformName": "Android",  # 手机系统
    "platformVersion": "11",  # 手机系统版本
    "deviceName": '90bf8faf',  # 连接的设备(adb devices)
    "automationName": "UiAutomator2",  # 自动化测试框架 (1.4以上的appium不用写)
    "appPackage": "app包名",  # app包名
    "appActivity": "app启动页名",  # app的启动页面
    "autoGrantPermissions": "true",  # 默认允许app获取相关权限
    "noReset": True  # 保留登录模式
}

上述参数中是启动app的基本配置,其中app包名和页面名可通过dumpsys window|grep mCu指令来查看。

其它参数配置可参考这位大佬的博客:https://www.cnblogs.com/wysk/p/7346659.html

3.实例

from appium import webdriver

# from selenium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

desired = {
    "platformName": "Android",  # 手机系统
    "platformVersion": "11",  # 手机系统版本
    "deviceName": '90bf8faf',  # 连接的设备(adb devices)
    "automationName": "UiAutomator2",  # 自动化测试框架 (1.4以上的appium不用写)
    "appPackage": "app包名",  # app包名
    "appActivity": "app启动页名",  # app的启动页面
    "autoGrantPermissions": "true",  # 默认允许app获取相关权限
    "noReset": True  # 保留登录模式
}
driver = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=desired)

四.常用方法与属性

1.implicitly_wait():设置隐性等待

driver.implicitly_wait(20)  # 设置隐性等待,单位为秒

具体等待用法可见:https://www.cnblogs.com/lihongtaoya/p/16725333.html

2.page_source:打印页面代码

print(driver.page_source)  # 打印当前页面代码

使用场景:我们在用uiautomatorviewer工具定位app页面元素时,有的页面(登录页面)禁止截图,

                  无法定位到元素,这时就可以使用page_source来打印页面代码来手动定位

3.install_app("url"):安装app

方法一

driver.install_app(r"C:\Users\username\Desktop\1.apk")  # 安装apk

方法二

os.system(r"adb install C:\Users\username\Desktop\1.apk")

4.remove_app("包名"):根据包名卸载app

driver.remove_app("包名")  # 卸载app,app_id为包名,pm list packages可查看

5.is_app_installed("包名"):根据包名判断当前设备有没有安装app

driver.is_app_installed("tv.danmaku.bili")  # 根据包名判断当前是否安装了软件,安装了返回true,否则返回flase

6.click():点击事件

7.send_key():输入

8.clear():清空

9.get_attribute("name") :获取元素值

10.text:获取文本值

11.size:获取元素宽高

12.tag_name:获取tabname属性

13.is_displayed():判断元素是否可见(隐藏返回false,非隐藏返回true)

14.get_attribute("checked") :判断单选框或复选框是否被选中(选中返回true,未选中返回false)

15.is_selected():判断下拉框

16.is_enabled():判断元素是否可点击,可点击返回true,不可点击返回false

17.hide_keyboard():隐藏键盘

driver.hide_keyboard()  # 隐藏键盘(前提键盘弹出)

18.start_activity():打开填写的app页

driver.start_activity(app_package="包名", app_activity="页面名")  # 打开设置的页面名和包名

19.current_activity:获取当前页面名

driver.current_activity

20.current_package:获取当前包名

driver.current_package

21.切换/查看视窗

# WebView.setWebContentsDebuggingEnabled(true)  # 打开开发者模式
print(driver.current_context) # 查看当前所在的视窗
print(driver.contexts) # 查看当前页面有多少视窗,返回数组
print(driver.context) # 查看当前所在的视窗
driver.switch_to.context("context_name") # 切换视窗

1)若要切换到webview视窗内,需要让开发打开开发者模式:WebView.setWebContentsDebuggingEnabled(true)

      才能用driver.contexts查看视窗,然后切换。

2)current_context与context均为查看当前视窗,无区别。

22.scroll(e2, e1):页面滑动

driver.scroll(e2, e1)  # 页面滑动,从元素e2滑动到e1(可横向和纵向滑动)

23.drag_and_drop(e1, e2):元素拖拽

driver.drag_and_drop(e1, e2)  # 将元素e1拖拽到元素e2

24.tap(positions: List[Tuple[int, int]], duration: Optional[int]):按住某个坐标点

driver.tap([(498, 276), (338, 276)], 3000)  # 设置同时按住多个坐标,元组类型的坐标[(),()]。可设置按住时长ms。

25.swipe(start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0):页面滚动

driver.swipe(x1,y1,x2,y2,3000)  #从(x1,y1)坐标滚动到(x2,y2)坐标,可设置滚动时长ms

26.flick(start_x: int, start_y: int, end_x: int, end_y: int):坐标拖拽

driver.flick(x1,y1,x2,y2)  # 从(x1,y1)坐标拖拽到(x2,y2)坐标

与swipe()的区别在于不能自定义拖拽时长

27.wait_activity():设置页面等待

driver.wait_activity("页面名", timeout=5, interval=1)  # 设置等待页面的出现,timeout为等待时长,interval每间隔多久设别一次(false或true)

28.background_app(ms):设置返回后台运行多少s后再返回app

driver.background_app(5)  # 设置返回后台运行多少s,后在返回app

29.shake():摇一摇手机

driver.shake()  # 摇一摇手机

30.open_notifications():打开设备通知栏

driver.open_notifications()  # 打开设备通知栏

31.toggle_location_services():打开定位设置页

driver.toggle_location_services()  # 打开定位设置页

32.location:返回元素坐标

element.location  # 返回元素xy坐标
element.location.get('x')  # 返回元素x坐标
element.location.get('y')  # 返回元素y坐标

33.get_screenshot_as_base64():手机截屏

driver.get_screenshot_as_base64()  # 手机截屏
详情可见:https://www.cnblogs.com/lihongtaoya/p/16759942.html

34.execute_script():执行js脚本

driver.execute_script()  # 执行js脚本

35.get_window_size():获取当前屏幕的长和宽

driver.get_window_size()  # 获取当前屏幕的长和宽

36.quit():关闭页面

driver.quit()

37.press_keycode() :短按键

driver.press_keycode(26)  # 26为按键码,按键码详情可见:https://blog.csdn.net/weixin_43004606/article/details/113552686

38.long_press_keycode():长按键码

driver.long_press_keycode(26)  # 长按键码

和press_keycode()的区别一个是长按(如长按关机键),一个是短按

39.w3c_actions用法

本来是想介绍MultiAction和TouchAction的使用的,但是两者在appium2.0以后就弃用了,所以本次就只

介绍w3c_actions的用法了。

 使用时应先导入ActionChains模块

om selenium.webdriver import ActionChains

1)click():指定元素左击

action.w3c_actions.pointer_action.click(element)  # 指定元素左击

2)context_click():指定元素右击

action.w3c_actions.pointer_action.context_click(element)  # 指定元素右击

3)double_click():双击

4)click_and_hold():长按元素

action.w3c_actions.pointer_action.click_and_hold(element)  # 设置长按元素

5)pause():设置等待时长

actions.w3c_actions.pointer_action.pause(5)  # 当前状态等待5s

6)release():松开

actions.w3c_actions.pointer_action.release()  # 松开元素

7)perform():执行鼠标事件

action.perform()

8)move_to_location():鼠标移动到指定坐标

actions.w3c_actions.pointer_action.move_to_location(x=971, y=256).click()  # 鼠标移动到指定坐标

9)move_to():鼠标移动到指定元素

actions.w3c_actions.pointer_action.move_to(element=driver.find_element(AppiumBy.XPATH, '//*[@text="取消"]')).click()  # 鼠标移动到指定元素上

10)pointer_down():按下

11)pointer_up():松开

实例用法

示例一:设置按下一个元素长按5s后松开

action.w3c_actions.pointer_action.click_and_hold(element)  # 设置长按元素
actions.w3c_actions.pointer_action.pause(5)  # 当前状态等待5s
actions.w3c_actions.pointer_action.release()  # 松开元素
action.perform()

示例二:移动到某个元素上按下再松开

actions.w3c_actions.pointer_action.move_to(element=driver.find_elements(AppiumBy.XPATH, '//*[@resource-id="com.vivo.browser:id/image" and @class="android.widget.ImageView"]')[0]).pointer_down()  # 按下
actions.w3c_actions.pointer_action.pointer_up()  # 松开

示例三:从一个坐标点滑动到另一个坐标点然后再松开

actions.w3c_actions.pointer_action.move_to_location(x=500, y=1000).pointer_down().move_to_location(x=500,y=500).pointer_up()

40.页面放大和缩小

这里不做过多的介绍了,直接看实例代码

from selenium.webdriver import ActionChains

# 放大
actions = ActionChains(driver)
actions.w3c_actions.devices = []  # 创建多devices集
fin1 = actions.w3c_actions.add_pointer_input('touch', 'fin1')  # 添加一个虚拟设备,一个手指
fin2 = actions.w3c_actions.add_pointer_input('touch', 'fin2')  # 添加一个虚拟设备,第二个手指
fin1.create_pointer_move(x=500, y=1000)  # 手指一移动到某点
fin2.create_pointer_move(x=500, y=1000)  # 手指二移动到某点
fin1.create_pointer_down()  # 手指一按下
fin2.create_pointer_down()  # 手指二按下
fin1.create_pointer_move(x=500, y=500)  # 手指一移动
fin2.create_pointer_move(x=500, y=1500)  # 手指二移动
fin1.create_pointer_up(0)  # 松开手指一释放
fin2.create_pointer_up(0)  # 松开手指二释放
actions.perform()  # 执行
time.sleep(3)
# 缩小
action = ActionChains(driver)
action.w3c_actions.devices = []
fin1 = action.w3c_actions.add_pointer_input('touch', 'fin1')
fin2 = action.w3c_actions.add_pointer_input('touch', 'fin2')
fin1.create_pointer_move(x=500, y=500)
fin2.create_pointer_move(x=500, y=1500)
fin1.create_pointer_down()
fin2.create_pointer_down()
fin1.create_pointer_move(x=500, y=1000)
fin2.create_pointer_move(x=500, y=1000)
fin1.create_pointer_up(0)
fin2.create_pointer_up(0)
action.perform()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

文章来源:https://www.cnblogs.com/lihongtaoya/ ,请勿转载
posted @ 2023-01-07 15:27  余生没有余生  阅读(908)  评论(0编辑  收藏  举报