黄瓜点击仅在使用 @javascript 时才起作用?
我知道有一个简单的解决办法,但我一辈子都不记得它是什么。
我的功能文件如下:
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
我已将“编辑”步骤定义如下:
When /^I edit "([^"]*)"$/ do |name|
within(:xpath, "//tr[./td[contains(text(), '#{name}')]]") do
find(:css, "a img[alt=Edit]").click
end
end
它所处理的索引页的 HTML 如下:
<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>
然后是表单 HTML:
<%= semantic_form_for [:admin, @location] do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :abbreviation %>
<% end %>
<%= f.submit "Save" %></li>
<% end %>
事实上,它不起作用 -我收到以下错误:
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)
但是表单元素“名称”显然在页面上。
如果我在“填写”之前添加“然后向我显示页面”,那么水豚会保存索引页,导致我认为它根本没有进入编辑表单。
...然而,如果我将“@javascript”标签添加到该功能中,即使该页面上没有 Javascript,它也可以完美运行。
我以前曾经解决过这个问题,但我一生都不知道如何解决......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我设法解决了这个问题 - 问题在于我的 CSS 选择器点击了“编辑”链接。我不知道为什么它不能按原样工作,但我将
find(:css, "a img[alt=Edit]").click
更改为读取click_link( '编辑')
并且效果非常好。来源:http://groups.google.com/group/ruby-水豚/browse_thread/thread/9c997395306d40e2/
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 readclick_link('Edit')
and it worked perfectly.Source: http://groups.google.com/group/ruby-capybara/browse_thread/thread/9c997395306d40e2/
对于初学者,您需要在 When 块中使用 Capybara 访问方法实际“访问”编辑页面。另外,我不相信您想使用 fill_in 将文本插入标签中(至少根据错误消息,它仅适用于文本字段/区域)。
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).