开发者

Cucumber click only working when @javascript is used?

开发者 https://www.devze.com 2023-02-05 08:57 出处:网络
I know there\'s a simple fix for this but for the life of me I can\'t remember what it is. 开发者_如何学GoMy feature file is as following:

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

开发者_如何学GoMy 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...


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/


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

0

精彩评论

暂无评论...
验证码 换一张
取 消