水豚升级 Capybara::Query

Capybara Upgrade Capybara::Query

我有一个使用 Capybara 2.7.1 版的旧项目。一个文件有以下代码

module Capybara
  module Node
    module Finders
      def all(*args)
        using_wait_time 1 do
          xpath = args.find { |a| a.is_a? String }
          has_selector? xpath
          query = Capybara::Query::Query.new(*args)
          synchronize(1) do
            result = query.resolve_for(self)
            raise Capybara::ExpectationNotMet, result.failure_message unless result.matches_count?
            result
          end
        end
      end
    end
  end
end

我们现已升级到 Capybara 3.15.0,看起来 Capybara::Query 已被弃用。我尝试使用 Queries::SelectorQuery 而不是 Query 如下所示

session_options = Capybara.session_options
query = Capybara::Queries::SelectorQuery.new(*args, session_options: session_options)

但是以下断言失败了

def content_xpath
  unless @content_xpath
    @content_xpath = "//td[@class='clsCenterButton' and ./label[contains(text(),'#{@label}')]]"
    @content_xpath = "//td[@class='clsCenterButton' and ./label[@title='#{@label}']]" if page.all(@content_xpath, visible: true).count>1
  end
  @content_xpath
end

Unused parameters passed to Capybara::Queries::SelectorQuery : [{:visible=>true}] (ArgumentError)

using_wait_time 10 do
  expect(page).to have_selector content_xpath
  expect(page).to have_selector content_xpath, visible: !(@collapsed)
end

Unused parameters passed to Capybara::Queries::SelectorQuery : [{:visible=>true}] (ArgumentError)

Gemfile(升级前)

source 'http://rubygems.org'
ruby '2.0.0'

gem 'cucumber', '2.0.0'
gem 'gherkin'
gem 'rspec-expectations', '3.2'
gem 'rspec'
gem 'capybara', '2.7.1'
gem 'selenium-webdriver', '2.53.4'
gem 'cuporter', '~>0.3.14'
gem 'nokogiri', '~>1.6.0'
gem 'savon', '~>2.1.0'
gem 'nori', '~>2.0.4'
gem 'chronic', '0.9.1'
gem 'json', '~>1.7.6'
gem 'rest-client', '~>1.6.7'
gem 'flog', '~>4.1.0'
gem 'flay', '~>2.3.0'
gem 'roodi', '~>2.1.0'
gem "require_all", "~> 1.3.2"
gem 'mongo', "~> 1.12.5"
gem 'bson_ext', "~> 1.12.5"
gem 'xml-simple', '~> 1.1.3'
gem 'roo', '~> 1.2.3'
gem "zip-zip", "~> 0.2"
gem 'rubyzip', '1.0.0'
gem 'tiny_tds', '0.7.0'
gem 'poltergeist', '1.10.0'
gem 'addressable', '2.4.0'
gem 'rack', '1.6.4'
gem 'cuke_sniffer'
gem 'rubocop'
gem 'net-ssh', '2.9.2'
gem 'sinatra'
gem 'cucumber_statistics'
gem 'activesupport', '4.2.5.2'
gem 'headless', '2.2.3'
gem 'colorize'
gem 'parallel'
gem 'parallel_tests'

Gemfile(升级后)

source 'http://rubygems.org'
ruby '2.3.0'

gem 'cucumber', '2.2.0'
gem 'gherkin', '6.0.15'
gem 'rspec-expectations', '3.8.2'
gem 'rspec', '3.8.0'
gem 'capybara', '3.15.0'
gem 'selenium-webdriver', '3.141.0'
gem 'cuporter', '0.3.14'
gem 'nokogiri', '1.10.2'
gem 'savon', '2.12.0'
gem 'nori', '2.6.0'
gem 'chronic', '0.10.2'
gem 'json', '2.2.0'
gem 'rest-client', '2.0.2'
gem 'flog', '4.6.2'
gem 'flay', '2.12.0'
gem 'roodi', '5.0.0'
gem 'require_all', '1.5.0'
gem 'mongo', '2.8.0'
gem 'bson_ext', '1.5.1'
gem 'xml-simple', '1.1.5'
gem 'roo', '2.8.2'
gem "zip-zip", '0.3'
gem 'rubyzip', '1.2.2'
gem 'tiny_tds', '2.1.2'
gem 'poltergeist', '1.18.1'
gem 'addressable', '2.6.0'
gem 'rack', '2.0.6'
gem 'cuke_sniffer', '1.1.0'
gem 'rubocop', '0.66.0'
gem 'net-ssh', '5.2.0'
gem 'sinatra', '2.0.5'
gem 'cucumber_statistics', '2.3.0'
gem 'activesupport', '5.2.2.1'
gem 'headless', '2.3.1'
gem 'colorize', '0.8.1'
gem 'parallel', '1.16.0'
gem 'parallel_tests', '2.28.0'
gem 'geckodriver-helper', '0.23.0'

有什么建议吗?

看起来您的 all 方法只是试图将等待行为添加到 Capybara 内置 all 方法(并且以一种非常奇怪的方式进行,这会破坏选择器的灵活性也有类型)。无论如何,这已成为 Capybara 3 中的默认行为,因此我建议您只删除 all monkeypatch。