0

我正在尝试用 capybara 编写一个 rspec 功能测试,但是在对 select2 元素进行测试时遇到了一些麻烦。看我的测试代码。使用 capybara 进行功能测试

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

提前致谢

4

1 回答 1

3

在您提到的问题中,您使用的是 select2,但随后您调用了我认为是常规选择元素的 set。当您使用 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 元素 - 从而选择它。

于 2015-12-29T18:01:34.557 回答