0

I know there's a simple fix for this but for the life of me I can't remember what it is.

My feature file is as following:

  Scenario: Editing locations
    When I edit "Western Australia"
      And fill in "Name" with "Tasmania"
      And I press "Save"
    Then I should see a form success message

And I've defined the 'Edit' step as the following:

When /^I edit "([^"]*)"$/ do |name|
  within(:xpath, "//tr[./td[contains(text(), '#{name}')]]") do
    find(:css, "a img[alt=Edit]").click
  end
end

The HTML for the index page it works on is as follows:

<tr>
    <td>Western Australia</td>
    <td>WA</td>
    <td>
        <a href="/admin/locations/2/edit"><img alt="Edit" src="/images/icons/pencil.png" title="Edit" /></a>
    </td>
</tr>

And then the form HTML:

<%= semantic_form_for [:admin, @location] do |f| %>
<%= f.inputs do %>
    <%= f.input :name %>
    <%= f.input :abbreviation %>
<% end %>

    <%= f.submit "Save" %></li> 
<% end %>

As it is, it doesn't work - I get the following error:

And fill in "Name" with "Tasmania"                           # features/step_definitions/web_steps.rb:39
  cannot fill in, no text field, text area or password field with id, name, or label 'Name' found (Capybara::ElementNotFound)

But the form element 'name' is clearly there on the page.

If I add 'Then show me the page' before the 'Fill in' then capybara saves the index page, leading me to think it isn't getting to the edit form at all.

... Yet if I add the '@javascript' tag to the feature, it works perfectly, even though there is no Javascript on this page.

I've solved this once before, but for the life of me I can't work out how...

2 Answers 2

1

Well I managed to solve the problem - the issue was with my CSS selector that was clicking the 'Edit' link. I don't know why it didn't work as-was, but I changed the find(:css, "a img[alt=Edit]").click to read click_link('Edit') and it worked perfectly.

Source: http://groups.google.com/group/ruby-capybara/browse_thread/thread/9c997395306d40e2/

Sign up to request clarification or add additional context in comments.

Comments

0

For starters, you need to actually "visit" the edit page using the Capybara visit method within your When block. Also, I don't believe you want to use fill_in for inserting text into your tags (at least according to the error message it is only for text fields/areas).

5 Comments

The click is clicking a link to go to the edit page. And I'm trying to fill in a text field on my form, yes.
Oh okay, I missed that part. Could you include the form html also?
Added to question, with some minor clarifications.
@Karpie I believe your code 'And fill in "Name" with "Tasmania"' is in corrent looking at the html form. There is no element with the id, name, or label of "Name". Check the generated html by viewing the source if you can. You can either add a 'f.label :name' or change your fill in text from "Name" to the correct value. I'm not familiar with semantic_form_for but it will be something like "location[name]" for a traditional rails form_for.
I've tried adding in a label manually, double-checking the label value to make sure it matches... no dice. I guess I'll do some more investigation...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.