1、原因探究
appium 使用一个远程调试器建立连接来实现和 web 视图的交互。当在模拟器上执行下面例子的时候,我们可以直接建立连接,因为模拟器和 appium 服务器在同一台机器上。当在真机上运行用例时,appium 无法直接访问 web 视图,你如果直接使用driver.contexts会报如下错误
ERROR: setUpClass (testcases.fb_pay_success.FB_Pay_Success)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sarah/autoprojects/appium_ios/testcases/base_android.py", line 101, in setUpClass
print self.driver.contexts
File "/Library/Python/2.7/site-packages/appium/webdriver/webdriver.py", line 58, in contexts
return self.execute(Command.CONTEXTS)['value']
File "/Library/Python/2.7/site-packages/selenium-2.45.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/appium/webdriver/errorhandler.py", line 29, in check_response
raise wde
WebDriverException: Message: An unknown server-side error occurred while processing the command. (Original error: connect ECONNREFUSED)
通过查询官方文档(http://appium.io/slate/en/v1.3.4/?ruby#automating-hybrid-apps),找到如下
When executing against a real iOS device appium is unable to access the web view directly. Therefore the connection has to be established through the USB lead. To establish this connection we use the ios-webkit-debugger-proxy.
所以我们需要通过 USB 线缆来建立连接。我们使用 iso-webkit-debugger-proxy建立连接,测试从native切换到webview。
2、解决方法:
使用 brew 安装最新的 ios-webkit-debug-proxy。在终端运行一下命令:
# 如果你没有安装 brew 的话,先安装 brew。
> ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"
> brew update
> brew install ios-webkit-debug-proxy
你也可以通过 git 克隆项目来自己安装最新版本:
# Please be aware that this will install the proxy with the latest code (and not a tagged version).
> git clone https://github.com/google/ios-webkit-debug-proxy.git
> cd ios-webkit-debug-proxy
> ./autogen.sh
> ./configure
> make
> sudo make install
一旦安装好了, 你就可以启动代理:
# 将udid替换成你的设备的udid。确保端口 27753 没有被占用
# remote-debugger 将会使用这个端口。
> ios_webkit_debug_proxy -c 0e4b2f612b65e98c1d07d22ee08678130d345429:27753 -d
注意: 这个 ios-webkit-debug-proxy 需要 "web inspector" 打开着以便建立连接。在 settings > safari > advanced 里打开它。请注意 web inspector 在 iOS6 时候加入的 以前的版本没有。
3、python例子:
# python
# assuming we have an initialized `driver` object for an app
# switch to webview
webview = driver.contexts.last
driver.switch_to.context(webview)
# do some webby stuff
driver.find_element(:css, ".green_button").click
# switch back to native view
driver.switch_to.context(driver.contexts.first)
# do more native testing if we want
driver.quit()