Question

I have a checkbox, that once set to true (or ticked) it opens a textfield. It works, but since its implementation my tests no longer pass. I have tried identifying the path via xpath

    it "can decide price" do 
  visit '/posts/new'
  fill_in 'post_description', with: 'Owl in a hat'
  expect(page).to have_css("#want_sell")
  find(:css, "#want_sell").set(true)
  expect(page).to have_xpath("//*[@id='price']")

I get the error

User posting an image chooses to sell prints can decide price
 Failure/Error: expect(page).to have_xpath("//*[@id='price']")
 Capybara::ExpectationNotMet:
   expected to find xpath "//*[@id='price']" but there were no matches. Also found "", which matched the selector but not all filters.

I have changed the

      find(:css, "#want_sell").set(true)

to

blah.click

and

blah.set('true')

but I still have the same error.

JavaScript

$('#want_sell').click(function() {
8      $("#price").toggle(this.checked);
9  });

Ruby/rails

<%= f.label :want_sell?, 'Want to sell your item?' %></br>  
<%= f.check_box :want_sell, :id => 'want_sell' %>
Was it helpful?

Solution

ok folks, I did it.

Here's how:

First, I needed to add Poltergeist to Capybara and specify it in my test, like so

    it "can decide price", js: true do 

Second, I had pre-existing javascript errors that needed to be fixed before Poltergeist would even run. Third, this code wasn't connected,

<%= f.label :want_sell?, 'Want to sell your item?' %></br>  
<%= f.check_box :want_sell, :id => 'want_sell' %> 

it should have been like this

<%= f.label :want_sell?, 'Want to sell your item?' %></br>  
<%= f.check_box :want_sell?, :id => 'want_sell' %> 

so my test spec could click the label as a reference to the checkbox.

now i can simply write in my tests...

 check 'Want to sell your item?'

Total missions bruv.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top