使用Select2和Capybara运行黄瓜测试选择一个选项



我有一个通过ajax加载选项的select2 v4。我正在进行一项黄瓜测试,需要在其中选择列表的2个选项,但是我似乎无法打开列表并加载列表(通常在我键入2或字符时填充它)。

我尝试过:

如下所示:

@session.execute_script("$('#publish_to').select2('open')")

@session.first(".input.publish_to .select2-container").click

@session.first("#publish_to").find(".select2-choice").click

没有给我一个错误,但我没有选择选项,因此我假设点击并不是实际工作。我尝试选择选项的东西:

# This one cannot find the css:
@session.find(".select2-results__options", text: client.email).click
# This one gives me a Timeout error 
@session.evaluate_script "$('#publish_to').val(#{client.id}).trigger('change')"
# This one gives me a Timeout error 
@session.evaluate_script "$('.select2-search__field').trigger('keydown').val('#{client.email}').trigger('keyup')";
sleep 10
@session.find('.select2-search__option', text: client.email).click

trigger的任何东西都给了我超时错误,所以我尝试等待 jQuery.active,但是我从来没有 true甚至等待2分钟:

counter = 0
 timeout_in_sec = 120
 while counter < timeout_in_sec && @session.evaluate_script('jQuery.active').zero?
   sleep 1.second
   counter+=1
 end

我尝试使用gem capybara-select2运行: @session.select2 client.email,css:'#publish_to',搜索:true但是我得到了错误的 undefined method select2'for# and I have world(capybaraselect2) in my env.rb`

我正在使用Ruby gem 'cucumber-rails'

使用Cucumber v3.1.2

Poltergeist驱动程序大致等于7年历史的Safari版本,这意味着它不支持许多当前的JS/CSS。这意味着您的问题可能仅仅是Select2不再与Poltergeist兼容(没有很多多填充)。您将更好地更新使用真正的浏览器(稳定 - 通过硒等)或直接旋转poltergeist的镀铬驱动程序(高度beta)的直接(Aptarition是其中之一)。这些将允许您使用可见的浏览器(可用于调试)或无头。

以下代码使用Chrome通过Selenium使用Chrome并与Select2演示站点进行交互以选择通过Ajax加载的条目。

require "selenium/webdriver"
require "capybara/dsl"
sess = Capybara::Session.new(:selenium_chrome)
sess.visit("https://select2.org/data-sources/ajax")
sess.first('.select2-container', minimum: 1).click
sess.find('.select2-dropdown input.select2-search__field').send_keys("capy")
sleep 5 # just to watch the browser search
sess.find('.select2-results__option', text: 'teamcapybara/capybara').click
sess.assert_selector(:css, '.select2-selection__rendered', text: 'teamcapybara/capybara')
sleep 5 # just to see the effect

相关内容

最新更新