使用 Capybara 进行 Select2 功能测试
Select2 feature test with Capybara
我正在尝试用水豚编写 rspec 功能测试,但是,
我在测试 select2 元素时遇到了一些问题。
看我的测试代码。
对水豚使用功能测试
feature "Backend Landing Pages" do
let!(:landing_page) { create(:landing_page, country: country) }
let!(:country) { create(:country, id: 2) }
let!(:restaurant) { create(:restaurant_with_locale) }
let!(:landing_page_restaurant) { create(:landing_page_restaurant,landing_page: landing_page, restaurant: restaurant) }
before(:each) do
login
click_on("Website")
click_on("Landing Pages")
end
scenario "user creates a landingpage", js: true do
first(:link,"netherlands").click
fill_in "landing_page_domain", with: landing_page.domain
fill_in "landing_page_slug_nl", with: landing_page.slug_nl
fill_in "landing_page_slug_en", with: landing_page.slug_en
fill_in "landing_page_header_title", with: landing_page.header_title
fill_in "landing_page_title", with: landing_page.title
attach_file "landing_page_header_image",( Rails.root + 'app/assets/images/site_builder/image3.jpg')
page.find('#landing_page_restaurant_select').set('Le Garage - Amsterdam')
page.save_screenshot ("test.png")
click_on("Save")
expect(page).to have_content("successfully created")
expect(LandingPage.count).to eq 2
expect(LandingPage.last.landing_page_restaurants.count).to eq 1
end
scenario "user edits a LandingPage", js: true do
click_on("Edit")
expect(page).to have_content 'Edit landing Page '
page.save_screenshot ("edit.png")
end
end
我遇到下一个错误。
Failures:
1) Backend Landing Pages user creates a landingpage
Failure/Error: expect(LandingPage.last.landing_page_restaurants.count).to eq 1
expected: 1
got: 0
(compared using ==)
# ./spec/features/backend/landing_pages_spec.rb:33:in `block (2 levels) in <top (required)>'
谁能看出我在这里做错了什么
无法弄清楚为什么餐厅没有连接到 landingPages
提前致谢
在您提到的问题中,您使用的是 select2,但随后您调用了我认为是常规 select 元素的集合。当您使用 JS 小部件时,它们通常会覆盖它们基于它们所在表单的提交事件构建的隐藏元素的值。因此,您设置的值可能会被覆盖。而不是设置隐藏元素的值(大多数水豚驱动程序不允许出于这个特定原因 - 不确定您使用的是哪个驱动程序)您需要复制用户行为。从 select2 示例页面看来,select2 小部件是从一个 span 元素和一个带有选项的 ul 列表构建的。因此,类似于
find('span.select2').click # the selector may need to be more specific to locate the exact span, without your html I don't know what that selector would be
find('li', text: 'Le Garage - Amsterdam').click
这将点击打开下拉菜单的 select2 元素,然后点击带有正确文本的 li 元素 - 从而 select 编辑它。
我正在尝试用水豚编写 rspec 功能测试,但是, 我在测试 select2 元素时遇到了一些问题。 看我的测试代码。 对水豚使用功能测试
feature "Backend Landing Pages" do
let!(:landing_page) { create(:landing_page, country: country) }
let!(:country) { create(:country, id: 2) }
let!(:restaurant) { create(:restaurant_with_locale) }
let!(:landing_page_restaurant) { create(:landing_page_restaurant,landing_page: landing_page, restaurant: restaurant) }
before(:each) do
login
click_on("Website")
click_on("Landing Pages")
end
scenario "user creates a landingpage", js: true do
first(:link,"netherlands").click
fill_in "landing_page_domain", with: landing_page.domain
fill_in "landing_page_slug_nl", with: landing_page.slug_nl
fill_in "landing_page_slug_en", with: landing_page.slug_en
fill_in "landing_page_header_title", with: landing_page.header_title
fill_in "landing_page_title", with: landing_page.title
attach_file "landing_page_header_image",( Rails.root + 'app/assets/images/site_builder/image3.jpg')
page.find('#landing_page_restaurant_select').set('Le Garage - Amsterdam')
page.save_screenshot ("test.png")
click_on("Save")
expect(page).to have_content("successfully created")
expect(LandingPage.count).to eq 2
expect(LandingPage.last.landing_page_restaurants.count).to eq 1
end
scenario "user edits a LandingPage", js: true do
click_on("Edit")
expect(page).to have_content 'Edit landing Page '
page.save_screenshot ("edit.png")
end
end
我遇到下一个错误。
Failures:
1) Backend Landing Pages user creates a landingpage
Failure/Error: expect(LandingPage.last.landing_page_restaurants.count).to eq 1
expected: 1
got: 0
(compared using ==)
# ./spec/features/backend/landing_pages_spec.rb:33:in `block (2 levels) in <top (required)>'
谁能看出我在这里做错了什么 无法弄清楚为什么餐厅没有连接到 landingPages
提前致谢
在您提到的问题中,您使用的是 select2,但随后您调用了我认为是常规 select 元素的集合。当您使用 JS 小部件时,它们通常会覆盖它们基于它们所在表单的提交事件构建的隐藏元素的值。因此,您设置的值可能会被覆盖。而不是设置隐藏元素的值(大多数水豚驱动程序不允许出于这个特定原因 - 不确定您使用的是哪个驱动程序)您需要复制用户行为。从 select2 示例页面看来,select2 小部件是从一个 span 元素和一个带有选项的 ul 列表构建的。因此,类似于
find('span.select2').click # the selector may need to be more specific to locate the exact span, without your html I don't know what that selector would be
find('li', text: 'Le Garage - Amsterdam').click
这将点击打开下拉菜单的 select2 元素,然后点击带有正确文本的 li 元素 - 从而 select 编辑它。