Appium Appium 运行 demo 测试代码,遇到的坑

bingle · 2014年12月29日 · 最后由 果子 回复于 2015年05月26日 · 1619 次阅读

刚接触这方面,用的 mac 虚拟机,appium 版本 1.3.3,xcode 版本 6.1,自带的 iOS sdk 和 iOS simulator sdk 都是 8.1。
从 git clone sample-code 项目后,终端进入 apps/TestApp 目录,输入命令

xcodebuild –sdk iphonesimulator8.1

此命令之前必须要在 xcode 中对工程编译做如下设置:iOS deployment Target 设为 8.1,保持两者的 sdk 版本一致,否则会编译失败。
附:查看系统的 sdk 版本

xcodebuild –showsdks

对 ios_sample.py 脚本中的 line 23 :

'platformVersion': '7.1',

修改为 8.1。
启动 appium server,运行客户端脚本会报错如下:

test_scroll (__main__.SimpleIOSTests) ... ERROR
test_ui_computation (__main__.SimpleIOSTests) ... ERROR

======================================================================
ERROR: test_scroll (__main__.SimpleIOSTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "ios_simple.py", line 57, in test_scroll
    el = self.driver.find_element_by_accessibility_id('OK')
  File "/Library/Python/2.7/site-packages/appium/webdriver/webdriver.py", line 135, in find_element_by_accessibility_id
    return self.find_element(by=By.ACCESSIBILITY_ID, value=id)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 662, in find_element
    {'using': by, 'value': value})['value']
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, 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
NoSuchElementException: Message: An element could not be located on the page using the given search parameters.


======================================================================
ERROR: test_ui_computation (__main__.SimpleIOSTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "ios_simple.py", line 42, in test_ui_computation
    self._populate()
  File "ios_simple.py", line 32, in _populate
    els = self.driver.find_elements_by_ios_uiautomation('elements()')
  File "/Library/Python/2.7/site-packages/appium/webdriver/webdriver.py", line 101, in find_elements_by_ios_uiautomation
    return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 677, in find_elements
    {'using': by, 'value': value})['value']
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 173, 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 error occurred while executing user supplied JavaScript.

明显代码有问题,调用 api 参数不正确。这里就是大坑了,谁会怀疑这里的代码都不正确,难道作者自己都不跑一遍么?
关于第一个测试用例报错,我自己用 simulator 手动模拟操作,点击第六个 button,根本就不会弹出 alert,所以找不到 ok 的按钮,把代码的 line 56 至 line 58 注释掉就行了。
再看第二个测试用例报错的地方,这里https://github.com/appium/python-client有标准用法说明

el = self.driver.find_element_by_ios_uiautomation('.elements()[0]')

els = self.driver.find_elements_by_ios_uiautomation('.elements()')

因此在代码的 line 32 和 line 39 分别加一个点符号。
run,走起

chenquanbindeMac:python chenquanbin$ python ios_simple.py
test_scroll (__main__.SimpleIOSTests) ... ok
test_ui_computation (__main__.SimpleIOSTests) ... ok

----------------------------------------------------------------------
Ran 2 tests in 157.202s

OK

完美通关!!!SSS

共收到 6 条回复 时间 点赞

sample-code 年久失修了!

#1 楼 @lihuazhang 是啊,坑死新手,没有人 commit 么

实践证明, 测试用例才是最好的演示例子和文档.

#2 楼 @bingle 我们 testerhome 打算维护一套 sample。 年后会做

mscdeMacBook-Pro:appium msc$ xcodebuild -sdk iphonesimulator8.3
Build settings from command line:
SDKROOT = iphonesimulator8.3

xcodebuild: error: The directory /Users/msc/appium does not contain an Xcode project.

这是啥原因呀?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册