之前我们在终端试着调用过WDA API, 今天我们在看一个Python封装的api库
https://github.com/openatx/facebook-wda
安装方式(一):
pip install --pre facebook-wda
安装方式(二):
git clone https://github.com/openatx/facebook-wda.gitcd facebook-wda/python setup.py install
用Xcode开启WDA 会话, 然后再编写和执行脚本
import wda# Enable debug will see http Request and Response# wda.DEBUG = True# get env from $DEVICE_URL if no arguments pass to wda.Client# http://localhost:8100 is the default value if $DEVICE_URL is emptyc = wda.Client()# Show Statusprint c.status()
输出:
/usr/bin/python2.7 /Users/jackey/Documents/iOS/code/iOS-Auto/Python_Client/Python_Client.py{u‘ios‘: {u‘ip‘: u‘192.168.1.101‘, u‘simulatorVersion‘: u‘11.2.1‘}, u‘state‘: u‘success‘, u‘os‘: {u‘version‘: u‘11.2.1‘, u‘name‘: u‘iOS‘}, u‘build‘: {u‘time‘: u‘Dec 25 2018 11:48:43‘}, u‘sessionId‘: u‘24AFBCFD-8CA0-4E4F-BEC3-21AD1170D880‘}Process finished with exit code 0
返回Home Screen
# Press home buttonc.home()
截屏
# Take a screenshotc.screenshot(‘screen.png‘)
打开和关闭app
# Open apps = c.session(‘NOVA.ProductDemo‘)# print app oritationprint s.orientation# Close apps.close()
还可用一下方式代替上面的代码:
with c.session(‘NOVA.ProductDemo‘) as s: print s.orientation
使用浏览器打开指定网站, 然后关闭
# 使用safari浏览器打开百度s = c.session(‘com.apple.mobilesafari‘,[‘-u‘, ‘http://www.baidu.com‘])print s.orientation# 关闭浏览器s.close()
打印app的bundle_id和sessionid
# open apps = c.session(‘NOVA.ProductDemo‘)# Current bundleId and session Idprint s.bundle_id, s.id
输出:
/usr/bin/python2.7 /Users/jackey/Documents/iOS/code/iOS-Auto/Python_Client/Python_Client.pyNOVA.ProductDemo E56C8902-DDB6-485A-8B0B-AA907CF55C59Process finished with exit code 0
截屏保存为png
# Screenshot return PIL.Image# Requires pillow, installed by "pip install pillow" s.screenshot().save("s.png")
截屏并旋转90度
from PIL import Images.screenshot().transpose(Image.ROTATE_90).save("correct.png")
调整显示方向
# Open url with safaris = c.session(‘com.apple.mobilesafari‘,[‘-u‘,‘http://www.baidu.com‘])print s.orientation# Wait 5stime.sleep(5)# Print orientationprint s.orientation# Change orientations.orientation = wda.LANDSCAPE
获取屏幕尺寸
# Get width and heightprint s.window_size()
模拟touch
# Simulate touchs.tap(200, 200)
Click, 类似tap, 但支持小数
s.click(200, 200)s.click(0.5, 0.5) # click center of screens.click(0.5, 200) # click center of x, and y(200)
滑动
# Simulate swipe, utilizing drag api
# 从(x1,y1)划向(x2,y2)s.swipe(x1, y1, x2, y2, 0.5) # 0.5s
# 从屏幕右边往左划s.swipe_left()
# 从屏幕左边往右划s.swipe_right()
# 从屏幕底部往上划s.swipe_up()
# 从屏幕顶部往上划s.swipe_down()
长按
# tap holds.tap_hold(x, y, 1.0)
关闭键盘
# Hide keyboard (not working in simulator), did not success using latest WDAs.keyboard_dismiss()
查找元素
# For example, expect: True or False# using id to find element and check if existss(id="URL").exists # return True or False# using id or other query conditionss(id=‘URL‘)s(name=‘URL‘)s(text="URL") # text is alias of names(nameContains=‘UR‘)s(label=‘Address‘)s(labelContains=‘Addr‘)s(name=‘URL‘, index=1) # find the second element. index starts from 0# combines search conditions# attributes bellow can combines# :"className", "name", "label", "visible", "enabled"s(className=‘Button‘, name=‘URL‘, visible=True, labelContains="Addr")
高阶查询
s(xpath=‘//Button[@name="URL"]‘)s(classChain=‘**/Button[`name == "URL"`]‘)s(predicate=‘name LIKE "UR*"‘)s(‘name LIKE "U*L"‘) # predicate is the first argument, without predicate= is ok
例如:
# Open apps = c.session(‘NOVA.ProductDemo‘)print s.window_size()s.click(100,640)e = s(text=‘京东超市‘).get(timeout=10)
如果提示:
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe4 in position 0: ordinal not in range(128)
需要在代码中添加:
import sysreload(sys)sys.setdefaultencoding(‘utf8‘)
元素操作: (点击、滑动、设置文本...)
举例:
# Open apps = c.session(‘NOVA.ProductDemo‘)print s.window_size()s.click(100,640)e = s(text=‘京东超市‘).get(timeout=10)e.tap() # tap element