0

I'm trying to write a rspec feature test with capybara but, I have some trouble with a test on a select2 element. see my test code. Using feature test with 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

I'm getting the next error.

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)>'

Who can see what im doing wrong here cant figure out why the restaurant is not connected to the landingPages

thanks in advance

1 Answers1

3

In the question you mention you are using a select2, but then you are calling set on what I assume is a regular select element. When you are using JS widgets they will often overwrite the values of the hidden elements they build off on a submit event of the form they're in. Because of this it is probable the value you're setting is getting overridden. Rather than set the values of hidden elements (which most capybara drivers don't allow for this specific reason - not sure which driver you're using) you need to replicate the users behaviors. From the select2 examples page it appears the select2 widget is built from a span element and a ul list with the options in it. Therefore something along the lines of

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

This would click on the select2 element opening the dropdown, and then click on the li element with the correct text - thereby selecting it.

Thomas Walpole
  • 42,399
  • 5
  • 53
  • 67