0

Currently, this is the code in my view:

<!-- Renders selected categories as a hash: {"1"=>"Sports", "3"=>"Fashion", "4"=>"World"} -->

  <% unless @categories.nil? %>
    <% @categories.each do |t| %>
      <%= t.name %>
      <%= check_box_tag "categories[#{t.id}]", t.name %>

      <br />
    <% end %>
  <% end %>

This is rendered on a browser, and the respective element on the web page is as follows:

  Entertainment
  <input id="categories_6" name="categories[6]" type="checkbox" value="Entertainment" />

  <br />
  Health
  <input id="categories_5" name="categories[5]" type="checkbox" value="Health" />

  <br />
  Politics
  <input id="categories_3" name="categories[3]" type="checkbox" value="Politics" />

  <br />
  Sports
  <input id="categories_8" name="categories[8]" type="checkbox" value="Sports" />

  <br />
  Tech
  <input id="categories_4" name="categories[4]" type="checkbox" value="Tech" />

  <br />
  Travel
  <input id="categories_7" name="categories[7]" type="checkbox" value="Travel" />

  <br />
  United States
  <input id="categories_1" name="categories[1]" type="checkbox" value="United States" />

  <br />
  World
  <input id="categories_2" name="categories[2]" type="checkbox" value="World" />

  <br />

In my Cucumber test, I would like to be able check categories that I have listed, and as such I have the following as my scenario/steps:

Step:

When /^(?:|I )check "([^"]*)"$/ do |field|
  check(field)
end

Scenario:

@javascript 
Scenario: a signed in user can post an article with an associated category
  Then I should see "Sign out"
  Then I should see "New Article"
  Then I should see "Hello"
  When I follow "New Article"
  And I fill in "Url" with "Hello_World_Article_URL.com"
  And I fill in "Initial comment" with "My first comment"
  And I check "categories_6"
  And I press "Create Article"
  Then I should see "Article created!"
  Then article url "Hello_World_Article_URL.com" should exist

However, this fails at the And I check "categories_6" step, reporting a Capybara ElementNotFound Error. I am unclear as to why referencing the checkbox by "id" did not work correctly. Does anyone have insight into what I may have done incorrectly or how I could go about fixing this issue?

Thanks.

1 Answer 1

0

Make web_steps that take the HTML id of the element you want to test. Use the Capybara find method to locate the element on the page. Don't forget to add the '#' to your find argument so it finds the id. Then use the be_checked matcher, like so,

Then /^checkbox "([^"]*)" should be unchecked$/ do |id|
  expect(find("##{id}")).not_to be_checked
end

Then /^checkbox "([^"]*)" should be checked$/ do |id|
  expect(find("##{id}")).to be_checked
end

When you use them in the Scenario, just pass in the id.

Then checkbox "categories_6" should be unchecked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.