我在运行黄瓜时遇到了一个问题,以测试我的一些复选框的功能。复选框位于用户的编辑个人资料页面上,允许用户检查他们的兴趣。但是,每次我运行黄瓜时它都说无法找到复选框。但是对于页面上的所有复选框都不会发生这种情况,只有兴趣的复选框才会发生。最初我们认为这是因为复选框没有唯一的ID,但现在我们已经更新了复选框以具有唯一的ID,但仍然无法通过水豚和黄瓜找到它们。
视图代码如下所示:
<% @interests.each do |interest| %>
<div class="form-group">
<%= label_tag 'interests', interest.name, class: 'col-sm-2 control-label' %>
<div class="col-sm-6">
<%= check_box_tag 'interests[]', interest.id, resource.interest_ids.include?(interest.id), {class: 'form-control', id: "interest_#{interest.id}"} %>
</div>
</div>
<% end %>
步骤定义是:
When /^(?:|I )check "([^"]*)"$/ do |field|
check(field)
end
该功能如下所示:
Feature: Edit Profile as a User
As a BoardBank User
So I can keep my info up to date
I want to be able to edit my profile
Background:
Given I login as a User
And I am on the home page
Scenario: Edit profile
When I follow "Profile"
And I follow "Edit Profile"
And I fill in "user_firstname" with "User"
And I fill in "user_lastname" with "Joe"
And I fill in "user_address" with "123 userville"
And I fill in "user_city" with "Userton"
And I select "Alabama" from "user_state"
And I fill in "user_zipcode" with "90210"
And I fill in "user_phonenumber" with "555-555-5555"
And I select "Bachelors" from "user_education"
And I fill in "user_areaofstudy" with "Magic"
And I check "interest_2"
And I check "user_previous_experience"
And I fill in "user_current_password" with "password"
And I press "Update"
Then I should see "Your account has been updated successfully."
And I should be on the homepage
当我运行黄瓜时,会发生这种情况:
And I check "interest_2" #features/step_definitions/web_steps.rb:89
Unable to find checkbox "interest_2" (Capybara::ElementNotFound)
./features/step_definitions/web_steps.rb:90:in `/^(?:|I )check "([^"]*)"$/'
features/edit_profile.feature:23:in `And I check "interest_2"'
我们认为视图代码必须存在问题以及复选框如何显示在页面上,但我们在搜索时找不到任何解决此问题的方法。
答案 0 :(得分:1)
尝试以这种方式找到复选框:
within('.form-group') do
page.find('input[type=checkbox]').set(true)
end
答案 1 :(得分:0)
如果使用chrome,则可以检查元素并复制css或xpath。这对我很有帮助。另外,如果你使用pry gem,我有时会在capybara上设置js:true,所以我可以看到浏览器中发生了什么,然后一步一步地走,直到我得到我想要的东西。对我来说,这样做更容易。