Python appium 常用方法
appium 文档:https://www.kancloud.cn/testerhome/appium_docs_cn/2001595
https://github.com/testerhome/appium/blob/master/docs/cn/about-appium/api.md
http://appium.io/docs/cn/about-appium/intro/
Desired Capabilitie 参数
http://appium.io/docs/cn/writing-running-appium/caps/index.html
1. 启动
from appium import webdriver
desired_caps = {
'platformName': 'Android',
#'deviceName': 'A7Q*********', 设备号连接
'deviceName':'SLY6R16C19004522',#IP:端口号 连接
#'platformVersion': '8.0',
# apk包名
'appPackage': 'tv.danmaku.bili',
# apk的launcherActivity
'appActivity': 'tv.danmaku.bili.ui.splash.SplashActivity'
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
2. driver
2.1 Activities
# 打开一个应用或者activity,仅安卓端
driver.start_activity('appPackage', 'appActiviyt')
# 获取当前activity
driver.current_activity()
# 等待指定的activity出现直到超时,interval为扫描间隔1秒即每隔几秒获取一次当前的activity, 返回的True 或 False
driver.wait_activity(activity, timeout, interval=1)
2.2 Applications
# 置后台5秒后再运行
driver.background_app(5)
# 关闭应用 driver.close_app() # 启动 (Launch) # 根据服务关键字 (desired capabilities) 启动会话 (session) 。请注意这必须在设定 autoLaunch=false 关键字时才能生效。 # 这不是用于启动指定的 app/activities ————你可以使用 start_activity 做到这个效果———— # 这是用来继续进行使用了 autoLaunch=false 关键字时的初始化 (Launch) 流程的。 driver.launch_app() # 检查app是否已安装 driver.is_app_installed('com.example.android.apis') # 安装应用到设备 driver.install_app('path/to/my.apk') # 卸载应用 driver.remove_app('com.example.android.apis')
#
driver.activate_app(app_id)
#如果某个应该程序app_id正在运行,则终止该应用程序
driver.terminate_app(app_id, **options)
driver.query_app_state(app_id)
driver.app_strings(language,string_file)
# 重置应用
driver.reset()
webdriver
# appium的webdriver提供了11种元素定位方法,在selenium的基础上扩展了三个,可以在pycharm里面输入driver.find_element_by然后会自动匹配出来
1. 通过id定位
Android
Android的resource-id对应ID定位方式,这个id也可能存在重复情况,可以通过index来获取需要的元素。(从0开始查找dom树中的同名resource-id属性)
使用appium-desktop来获取元素时,如果提示有id的定位方式,则可以只接获取,代表唯一。
find_element_by_id("com.xyh.commerce:id/ll_personal").click()
find_elements_by_id
2. 通过xpath定位
Android
Android的Xpath定位与PC的XPATH定位大同小异,可以通过相对路径的定位方式定位,区别在于,这里相对路径定位的//后只可以接Android的class属性或*。(//android.widget.Button[@text="登 录"])
iOS
iOS10 以上使用XCUITest框架后,原生框架不支持XPATH,Appium进行了转换,速度很慢不建议使用。
find_element_by_xpath()
find_elements_by_xpath()
3.
find_element_by_link_text
find_elements_by_link_text
4.
find_element_by_partial_link_text
find_elements_by_partial_link_text
5.
find_element_by_name
find_elements_by_name
6.
find_element_by_tag_name
find_elements_by_tag_name
7.classname
Android
Android的class属性对应ClassName定位方式,ClassName一般都是会重复的,可以通过index来获取需要的元素。(从0开始查找dom树中的同名class属性)
iOS
iOS的type属性对应CLassName定位方式,ClassName一般都是会重复的,可以通过index来获取需要的元素。(从0开始查找dom树中的同名class属性)
find_element_by_class_name
find_elements_by_class_name
8.
find_element_by_css_selector
find_elements_by_css_selector
9.
find_element
find_elements
execute_script
execute_async_script
MobileSearchContext
1.AccessibilityId
Android
Android的content-desc属性对应AccessibilityId定位方式,这个content-desc属性专门为残障人士设置,如果这个属性不为空则推荐使用。
iOS
iOS的label和name属性都对应AccessibilityId定位方式,如果有则推荐使用。
find_element_by_accessibility_id
find_elements_by_accessibility_id
2.Image
图片
find_element_by_image(img_path)
CustomSearchContext
1.find_element_by_custom("foo:bar")
AndroidSearchContext
Android的源生测试框架的定位方式,定位速度快。
1.find_element_by_android_uiautomator
UiSelector 用法: https://developer.android.google.cn/reference/android/support/test/uiautomator/UiSelector
# 这个在运行时,调用的是Android自带的UI框架UiAutomator的Api # 介绍几个简单常用的,text、className、resource-id # text # 匹配全部text文字 driver.find_element_by_android_uiautomator('new UiSelector().text("手机号")') # 包含text文字 driver.find_element_by_android_uiautomator('new UiSelector().textContains("机")') # 以text什么开始 driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("手")') # 正则匹配text driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^手.*")') # className driver.find_elements_by_android_uiautomator('new UiSelector().className("android.widget.TextView")') # classNameMatches driver.find_elements_by_android_uiautomator('new UiSelector().classNameMatches("^android.widget.*")') # resource-id、resourceIdMatches 类似我们html id 这个可能重复, driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.syqy.wecash:id/et_content")') # description driver.find_element_by_android_uiautomator('new UiSelector().description("S 日历")') # descriptionStartsWith driver.find_element_by_android_uiautomator('new UiSelector().descriptionStartsWith("日历")') # descriptionMatches driver.find_element_by_android_uiautomator('new UiSelector().descriptionMatches(".*历$")') #组合定位 self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/tab_name").text("我的")').click() #父子关系定位 self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").childSelector(text("股票"))') #兄弟关系定位 self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").fromParent(text("股票"))') #滚动查找 self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("查找的元素文本").instance(0));')
2.find_element_by_android_view_matcher
3.find_element_by_android_data_matcher
4.find_element_by_android_viewtag
iOSSearchContext
1.find_element_by_ios_predicate
仅支持iOS10以上,可以多个属性同时定位,推荐。(替代XPATH)
river.find_elements_by_ios_predicate("label == '登录'") driver.find_elements_by_ios_predicate("type='XCUIElementTypeOther' and name='联系人,标签, 第2个按钮,共3个'")
2.find_element_by_ios_uiautomation
iOS9.3以下使用,现在已经废弃,iOSPredicateString代替了iOSUIAutomation
隐式等待 全局有效
driver.implicitly_wait(5) # 单位5s
显示等待 单个元素有效
from selenium.webdriver.support.wait import WebDriverWait # driver 驱动对象
# timeout 超时的时长, 单位:秒
# poll_frequency 检测间隔时间,默认0.5秒
wait = WebDriverWait(driver, 5, 0.5)
# 没有定位到抛出TimeoutException button = wait.until(lambda x:x.find_element_by_id("tv.danmaku.bili:id/agree")) button.click()
wait.until_not()

浙公网安备 33010602011771号