重新编辑这个帖子,因为这两天我也尝试了很多 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 也是报错


↙↙↙阅读原文可查看相关链接,并与作者交流