选择select2和Capybara运行Cucumber测试的选项

问题描述 投票:0回答:1

我有一个select2 v4,通过AJAX加载选项。我正在运行Cucumber测试,我需要选择列表的2个选项,但我似乎无法打开和加载列表(通常在我输入2或字符时填充)。

我试过了:

正如建议here

@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:'#upload_to',搜索:true但是我得到#undefined methodWorld(CapybaraSelect2)and I haveenv.rb的错误in myselect2'

我使用Cucumber v3.1.2与红宝石gem 'cucumber-rails'

ruby-on-rails cucumber capybara jquery-select2 jquery-select2-4
1个回答
0
投票

恶作剧驱动程序大致相当于一个7岁的Safari版本,这意味着它不支持很多当前的JS / CSS。这意味着您的问题可能只是因为select2不再与Poltergeist兼容(没有大量的填充)。你可以更好地使用真正的浏览器(稳定的 - 通过硒等铬)或者直接驱动Chrome驱动程序(高度测试版)来剥离Poltergeist(Apparition就是其中之一)。这些将允许您使用可见的浏览器(对调试很有用)或无头运行。

以下代码使用Chrome通过selenium并与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
© www.soinside.com 2019 - 2024. All rights reserved.