为什么我在传入 ':css' 时从 watir-webdriver 方法 "assert_valid_as_attribute" 得到 'invalid attribute' 异常?

Why do I get an 'invalid attribute' exception from watir-webdriver method "assert_valid_as_attribute" when passing in ':css'?

我最近继承了一个 Ruby 测试套件,它是 运行ning watir-webdriver (0.6.4),我处于套件 运行s 的不幸情况在我同事的机器上就好了,但有几个案例在我的机器上出错了。我已经尝试将我所有 gem 的每个版本锁定到他机器上的确切版本以尝试解决兼容性问题,但没有骰子。核心错误似乎是我得到以下源自 watir 内部“assert_valid_as_attribute”方法的异常:

(Watir::Exception::MissingWayOfFindingObjectException: invalid attribute: :css)

我的代码中的原始调用是:

search_results.selectable_cards_plural.take(assets_selected).each { |assets| assets.fire_event ("onclick") }

search_results{...}.take returns 一组 div。

有人见过这个问题吗?我已经看到另外两个 SO 帖子提到了这个问题,但它们似乎都没有解决我的特定问题。

我在下面包含了我的 gemfile.lock 的一部分,以防任何人都能轻松发现兼容性问题。我是 运行宁 Ruby 2.5.0。是的,我计划在不久的将来将测试更新到 运行 更新的 gem 版本,但我希望同时进行这些遗留回归测试 运行ning。

上面提到的选择器: def search_results; @browser.div(:class, 'search-results-grid') end def selectable_cards_plural; divs(:css => 'div.p-card-hover.p-card-selectable:not(.p-card-selected)') end

被选中的元素是: class="p-card-hover p-card-selectable css-g6ribn-StyledCardHover-visible-visible-visible-visible-StyledCardHover erkh22a0"

环境信息: macOS macOS 10.14.3(莫哈韦沙漠) Chrome 版本 72.0.3626.121 浏览器驱动程序:@browser = Watir::Browser.new :chrome, :prefs => chrome_prefs, 开关:%w[--window-size=1600,900 --忽略证书错误 --disable-popup-blocking --disable-translate]

堆栈跟踪:

Asset Section: Adding # When selecting specific # from search adds assets to section
     Failure/Error: search_results.selectable_cards_plural.take(assets_selected).each { |assets| assets.fire_event ("onclick") }

     Watir::Exception::MissingWayOfFindingObjectException:
       invalid attribute: :css
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:238:in `assert_valid_as_attribute'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:219:in `normalize_selector'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:198:in `block in normalized_selector'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:195:in `each'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:195:in `normalized_selector'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:98:in `find_all_by_multiple'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/locators/element_locator.rb:46:in `locate_all'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/element_collection.rb:96:in `elements'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/element_collection.rb:86:in `to_a'
     # /Users/jnguyen/.rvm/gems/ruby-2.5.0/gems/watir-webdriver-0.6.4/lib/watir-webdriver/element_collection.rb:29:in `each'
     # ./lib/live_editor/assets_le.rb:37:in `take'
     # ./lib/live_editor/assets_le.rb:37:in `select_assets'
     # ./spec/smoke/live_editor/standard/asset_add_num_spec.rb:23:in `block (3 levels) in <top (required)>'

Gemfile.lock

    {...}
    commonwatir (4.0.0)
    rake (12.3.2)
    rspec (3.5.0)
      rspec-core (~> 3.5.0)
      rspec-expectations (~> 3.5.0)
      rspec-mocks (~> 3.5.0)
    rspec-core (3.5.2)
      rspec-support (~> 3.5.0)
    rspec-expectations (3.5.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.5.0)
    rspec-mocks (3.5.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.5.0)
    rspec-support (3.5.0)
    rspec_html_formatter (0.3.1)
      activesupport (>= 4.1.4)
      rouge (>= 1.6.1)
      rspec-core (>= 3.0.3)
    selenium-webdriver (2.53.4)
      childprocess (~> 0.5)
      rubyzip (~> 1.0)
      websocket (~> 1.0)
    watir (5.0.0)
      commonwatir (~> 4)
      watir-webdriver
    watir-rspec (2.0.3)
      rspec (~> 3.0)
      watir (~> 5.0)
    watir-scroll (0.1.0)
      watir-webdriver
    watir-webdriver (0.6.4)
      selenium-webdriver (>= 2.18.0)

PLATFORMS
  ruby

DEPENDENCIES
  {...}
  rake
  rspec (= 3.5.0)
  rspec-core (= 3.5.2)
  rspec_html_formatter (~> 0.3.1)
  selenium-webdriver (= 2.53.4)
  watir (~> 5.0.0)
  watir-rspec (= 2.0.3)
  watir-scroll (~> 0.1.0)
  watir-webdriver (= 0.6.4)

BUNDLED WITH
   1.17.3

版本似乎不匹配。

在v0.6.4中,以下会抛出异常:

gem 'selenium-webdriver', '=2.53.4'
gem 'watir-webdriver', '=0.6.7'

require 'watir-webdriver'

browser = Watir::Browser.new :chrome
at_exit { browser.close }
p browser.divs(:css => 'div').count
#=> `assert_valid_as_attribute': invalid attribute: :css (Watir::Exception::MissingWayOfFindingObjectException)

在这个版本中,对 :css 定位器的唯一支持是它自己。上面的示例将包含 :tag_name 定位器,这是异常的原因。您将不得不使用 #elements 而不是 divs

这已在 v0.6.8 (https://github.com/watir/watir/pull/244) 中解决。我希望您的代码库至少是使用 v0.6.8 创建的。