Appium appium + cucumber + android 无法定位元素

55hhy · July 02, 2015 · Last by 55hhy replied at January 04, 2016 · 1446 hits

重新编辑这个帖子,因为这两天我也尝试了很多 github 上的测试工程,但是一直无法定位元素,不是因为 path 问题,可能是哪个点我没有 get 到,所以这次要写详细些。

首先,决定用https://github.com/appium/sample-code, 因为 appium_lib 还不错的样子

其次,我用 appium 录制的脚本如下(为啥 appium 不能用 id 用 xpath,xpath 那么长,不喜欢啊,等我解决这个问题就换 id)

然后脚本内容:

./support/appium.txt:
[caps]
platformName = "android"
deviceName = "Genymotion"
appPackage = "com.*******"
appActivity = "com.****"
appPath = "./../../****.apk"

[appium_lib]
sauce_username = ""
sause_access_key = ""


./support/env.rb:
require 'rspec/expectations'
require 'appium_lib'
require 'cucumber/ast'
require 'rubygems'

class AppiumWorld
end

caps = Appium.load_appium_txt file: File.expand_path('./', __FILE__), verbose: true
$driver = Appium::Driver.new(caps)
Appium.promote_appium_methods AppiumWorld

World do
  AppiumWorld.new
end

Before {$driver.start_driver}
After {$driver.driver_quit}

./step_definitions/login.rb:
Given(/^I am on login page$/) do
  start_activity app_package: 'app_package', app_activity: 'app_activity.LoginActivity'
#应用之前有一个splashactivity动画过渡的,所以我需要到达LoginActivity
end

And (/^I enter username and password$/) do
  find_element(:xpath, "//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.View[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.EditText[1]").send_keys "jengage1"
  find_element(:xpath, "//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.View[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[2]/android.widget.EditText[1]").send_keys "ef"
end

最后,appium 报错信息:

info: [debug] Device is at API Level 19
info: [debug] executing cmd: /Users/evahao/Documents/android-sdk-macosx/platform-tools/adb -s 192.168.56.101:5555 shell "am start -S -n app_package/app_activity.LoginActivity"
info: [debug] Responding to client with success: {"status":0,"value":"Successfully launched the app.","sessionId":"328bce3b-c98c-4402-8eab-8300cc48d633"}
info: <-- POST /wd/hub/session/328bce3b-c98c-4402-8eab-8300cc48d633/appium/device/start_activity 200 605.173 ms - 104 {"status":0,"value":"Successfully launched the app.","sessionId":"328bce3b-c98c-4402-8eab-8300cc48d633"}
info: --> POST /wd/hub/session/328bce3b-c98c-4402-8eab-8300cc48d633/element {"using":"xpath","value":"//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.View[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.EditText[1]"}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.View[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.EditText[1]","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.View[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.EditText[1]","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding //android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.view.View[1]/android.widget.RelativeLayout[2]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.EditText[1] using XPATH with the contextId:  multiple: false
info: [debug] Condition unmet after 418ms. Timing out.
info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"Could not find an element using supplied strategy. "},"sessionId":"328bce3b-c98c-4402-8eab-8300cc48d633"}
info: <-- POST /wd/hub/session/328bce3b-c98c-4402-8eab-8300cc48d633/element 500 420.494 ms - 230 
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"Could not find an element using supplied strategy. ","status":7}
info: --> DELETE /wd/hub/session/328bce3b-c98c-4402-8eab-8300cc48d633 {}
info: Shutting down appium session

cucumber log:
    And I enter username and password # features/step_definitions/login.rb:5
      Could not find an element using supplied strategy.  (Selenium::WebDriver::Error::NoSuchElementError)
#这里很奇怪,我用的是Appium::Driver,为啥错误信息里面是Selenium::WebDriver??

补充:我在代码或者用 uiautomatorviewer,都能发现 id,但是用 id 也是报错

共收到 9 条回复 时间 点赞

xpath 错了,你用 get source 看看 xml 里面节点的名称吧。

55hhy #2 · July 03, 2015 Author

#1 楼 @chenhengjie123 我重新编辑了 能再帮我看看嘛

# 这里很奇怪,我用的是 Appium::Driver,为啥错误信息里面是 Selenium::WebDriver??

AppiumDriver 继承自 WebDriver 啊,异常这边就直接用的 Webdriver 的。

报错很明显,就是没有找到,你的 XPATH 不对啊。 干嘛用这么长的? 你自己可以使用 xml 的 xpath 啊?

55hhy #4 · July 06, 2015 Author

#3 楼 @lihuazhang
其实之前我是用 ID 的, 但是目前看来这个问题是因为环境里面还缺东西
继续 bundle update

Gemfile 太坑了:
source 'https://www.rubygems.org'

gem 'appium_lib',         '~> 7.0.0'
gem 'rest-client',        '~> 1.6.7'
gem 'rspec',              '~> 2.14.1'
gem 'cucumber',           '~> 1.3.15'
gem 'rspec-expectations', '~> 2.14.5'
gem 'spec',               '~> 5.3.4'
gem 'sauce_whisk',        '~> 0.0.13'
gem 'test-unit',          '~> 2.5.5'

#4 楼 @55hhy gemfile 怎么坑了? 你先熟悉下 ruby 的 gem 再下这个结论。

55hhy #6 · July 06, 2015 Author

#5 楼 @lihuazhang
我的理解是我 bundle update 时会自动找到 Gemfile 来更新
但是更新中我的 iMac 现在报的错如下
(之前我已经很努力各种解决问题了,试过 google 各种了,现在依旧还在路上)

Evas-iMac:engage_android_automation evahao$ bundle update
Fetching gem metadata from https://www.rubygems.org/...........
Resolving dependencies...
Using awesome_print 1.6.1 (was 1.2)
/Library/Ruby/Gems/2.0.0/gems/bundler-1.7.8/lib/bundler.rb:302: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777
Installing json 1.8.3 (was 1.8.2)
Using mini_portile 0.6.2
Using nokogiri 1.6.6.2
Using ffi 1.9.10 (was 1.9.6)
Using childprocess 0.5.6 (was 0.5.5)
Installing multi_json 1.11.2 (was 1.10.1)
Installing rubyzip 1.1.7
Installing websocket 1.2.2 (was 1.2.1)
Using selenium-webdriver 2.46.2 (was 2.44.0)
Using blankslate 2.1.2.4
Using parslet 1.5.0
Using toml 0.1.2
Using appium_lib 7.0.0 (was 3.0.3)
Using builder 3.2.2
Installing numerizer 0.1.1
Installing chronic_duration 0.10.6
Using diff-lcs 1.2.5
Using gherkin 2.12.2
Using multi_test 0.1.2 (was 0.1.1)
Installing cucumber 1.3.20 (was 1.3.18)
Installing mime-types 1.25.1
Installing rest-client 1.6.9 (was 1.6.8)
Installing rspec-core 2.14.8
Installing rspec-expectations 2.14.5
Installing rspec-mocks 2.14.6
Using rspec 2.14.1

NoMethodError: undefined method `size' for nil:NilClass
An error occurred while installing sauce_whisk (0.0.13), and Bundler cannot continue.
Make sure that `gem install sauce_whisk -v '0.0.13'` succeeds before bundling.

然后我安装 sauce_whisk,没有加版本号是因为这个旧版本安装失败了,然后就觉得新版本会好装些

Evas-iMac:engage_android_automation evahao$ gem install sauce_whisk
Fetching: unf_ext-0.0.7.1.gem (100%)
Building native extensions.  This could take a while...
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/ext/builder.rb:54: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777
Successfully installed unf_ext-0.0.7.1
Fetching: unf-0.1.4.gem (100%)
Successfully installed unf-0.1.4
Fetching: domain_name-0.5.24.gem (100%)
Successfully installed domain_name-0.5.24
Fetching: http-cookie-1.0.2.gem (100%)
Successfully installed http-cookie-1.0.2
Fetching: rest-client-1.8.0.gem (100%)
Successfully installed rest-client-1.8.0
Fetching: sauce_whisk-0.0.18.gem (100%)
ERROR:  While executing gem ... (NoMethodError)
    undefined method `size' for nil:NilClass
Evas-iMac:engage_android_automation evahao$ gem install sauce_whisk
ERROR:  While executing gem ... (NoMethodError)
    undefined method `size' for nil:NilClass

iMac 我还在尝试中

至于我的 MacBook 上出现的问题就是

haohuiyan@haohuiyan:~/Documents/android_engage_automation$bundle update
Fetching gem metadata from https://www.rubygems.org/...........
Resolving dependencies...
Using awesome_print (1.6.1) 
Using json (1.8.3) 
Using mini_portile (0.6.0) 

Bundler::GemspecError: Could not read gem at /Users/haohuiyan/.rvm/gems/ruby-2.0.0-p247/cache/nokogiri-1.6.3.1.gem. It may be corrupted.
An error occurred while installing nokogiri (1.6.3.1), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.6.3.1'` succeeds before bundling.

haohuiyan@haohuiyan:~/Documents/android_engage_automation$gem install nokogiri
Fetching: nokogiri-1.6.6.2.gem (100%)Fetching: nokogiri-1.6.6.2.gem
ERROR:  Error installing nokogiri:
    invalid gem: package is corrupt, exception while verifying: undefined method `size' for nil:NilClass (NoMethodError) in /Users/haohuiyan/.rvm/gems/ruby-2.0.0-p247/cache/nokogiri-1.6.6.2.gem
55hhy #7 · July 07, 2015 Author

都不好意思再编辑这个帖子了,再编辑一次:)
我不明白为什么这个例子里面会有 Gemfile,实际是不需要的。
然后关于卡在 bundle install 的今天解决了:
http://appium.io/slate/en/tutorial/ios.html?ruby#install-overview

gem update --system
gem install --no-rdoc --no-ri bundler
gem update
gem cleanup
gem uninstall -aIx appium_lib
gem uninstall -aIx appium_console
gem install --no-rdoc --no-ri appium_console

楼主, 问哈您是否已经可以自动生成测试报告?

55hhy #9 · January 04, 2016 Author

#8 楼 @tongxiaoxing cucumber 是可以自动生成报告的

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up