appium对iOS混合应用的测试

本文介绍了解决Appium在iOS真机上进行Web视图测试时遇到的问题。由于Appium无法直接访问真机上的Web视图,需要通过ios-webkit-debug-proxy建立连接。文章提供了安装和配置该工具的方法,并给出Python测试示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值