Appium 【已解决】:Appium 无法再使用 tag_name 定位(Ruby 语言脚本)

和曦 · 2015年02月17日 · 最后由 和曦 回复于 2015年02月17日 · 1411 次阅读

问题:在使用 appium 运行 contactmanager 的 ruby 脚本时,无法使用 tag_name 进行元素定位.

环境:
Window7_64bit 系统,安装了以下最新的 appium gem 包

*** LOCAL GEMS ***
appium_console (1.0.3)
appium_lib (6.0.0)

运行成功的代码 (使用 name,id 定位):

# encoding: utf-8
require 'rubygems'
require 'appium_lib'

describe 'ContactForAppium' do
   APP_PATH = 'D:/Appium_Script/apps/ContactManager.apk'
   def desired_caps
      {
         caps:       {
            app:               APP_PATH,
            appActivity:      '.ContactManager',
            appPackage:       'com.example.android.contactmanager',
            platformName:     'android',
            platformVersion:  '4.4.2',#API:19
            deviceName:       'emulator-5554',
         },
         appium_lib: {
            sauce_username: nil,
            sauce_access_key: nil,
            debug: true,
            wait: 60
         }
      }
   end

   before do
      @driver = Appium::Driver.new(desired_caps).start_driver
   end

   after do
      @driver.quit
   end

   it 'Contact' do
      el = @driver.find_element(:name, 'Add Contact')
      puts el.text
      el.click
      id = @driver.find_element(:id, "com.example.android.contactmanager:id/contactNameEditText")
      id.send_keys("My name")
      phone = @driver.find_element(:id, "com.example.android.contactmanager:id/contactPhoneEditText")
      phone.send_keys("13800000001")
      email = @driver.find_element(:id, "com.example.android.contactmanager:id/contactEmailEditText")
      email.send_keys("someone@somewhere.com")
      @driver.find_element(:name, "Save").click
   end
end

运行失败的代码 (使用 tag_name 定位):

# encoding: utf-8
require 'rubygems'
require 'appium_lib'

describe 'ContactForAppium' do
   APP_PATH = 'D:/Appium_Script/apps/ContactManager.apk'
   def desired_caps
      {
         caps:       {
            app:               APP_PATH,
            appActivity:      '.ContactManager',
            appPackage:       'com.example.android.contactmanager',
            platformName:     'android',
            platformVersion:  '4.4.2',#API:19
            deviceName:       'emulator-5554',
         },
         appium_lib: {
            sauce_username: nil,
            sauce_access_key: nil,
            debug: true,
            wait: 60
         }
      }
   end

   before do
      @driver = Appium::Driver.new(desired_caps).start_driver
   end

   after do
      @driver.quit
   end

   it 'Contact' do
      name = @driver.find_element(:name, 'Add Contact')
      puts name.text
      name.click
      tag_name = @driver.find_elements(:tag_name, "textfield")
      tag_name.send_keys("My name")    
   end
end

报错 log:

d:\Appium_Script\spec>rspec -f doc ContactForAppium2_fail.rb

ContactForAppium
Add Contact
  Contact (FAILED - 1)

Failures:

  1) ContactForAppium Contact
     Failure/Error: tag_name = @driver.find_elements(:tag_name, 'textfield')
     Selenium::WebDriver::Error::UnknownCommandError:
       Invalid locator strategy: tag name
     # ./ContactForAppium2_fail.rb:38:in `block (2 levels) in <top (required)>'

Finished in 31.73 seconds (files took 0.64104 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./ContactForAppium2_fail.rb:34 # ContactForAppium Contact
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 3 条回复 时间 点赞

刚刚又上官网查了一下,使用 tag_name 定位的方法应该是被废弃了.
http://appium.io/slate/en/master/?ruby#lock

New locator strategies

We’ve removed the following locator strategies:

    name
    tag name

We have now added the accessibility_id strategy to do what name used to do. The specifics will be relative to your Appium client.

tag name has been replaced by class name. So to find an element by its UI type, use the class name locator strategy for your client.

Note about class name and xpath strategies: these now require the fully-qualified class name for your element. This means that if you had an xpath selector that looked like this:

//table/cell/button

It would now need to be:

//UIATableView/UIATableCell/UIAButton

(And likewise for Android: button now needs to be android.widget.Button)

We’ve also added the following locator strategies:

    -ios uiautomation
    -android uiautomator

Refer to your client for ways to use these new locator strategies.

代码调整成如下已测试通过.

it 'Contact' do
   button = @driver.find_element(:class_name, 'android.widget.Button')
   puts button.text
   button.click
   tag_name = @driver.find_element(:class_name, 'android.widget.EditText')
   tag_name.send_keys("My name")    
end

好像很早就变更了,

Python 的没有用 tag
PHP 的根据 Python 的例子改过之后 也运行成功

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