无法在Capybara中使用id选择复选框

时间:2013-11-29 10:50:38

标签: ruby-on-rails rspec capybara

有以下RSpec代码:

describe 'with valid information' do
    it 'should create a new restaurant' do
      visit new_restaurant_path

    fill_in I18n.translate(:name),          with: "Some restaurant"
      fill_in I18n.translate(:address),       with: "Some address of the restaurant"
      fill_in I18n.translate(:average_check), with: 100

    check('place_restaurant_food_types_1')

    expect { click_button :submit }.to change(Restaurant, :count).by(1)
    end
end

但我总是得到错误“无法检查字段,没有带有id,名称或标签的复选框'place_restaurant_food_types_1'找到了”。我试图将id替换为name,但它仍然是相同的。我绝对相信有需要id的项目! (我从页面源复制了它)。我该如何解决?

HTML:

<form accept-charset="UTF-8" action="/restaurants" class="new_place_restaurant" id="new_place_restaurant" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="7yQCirO7MLO6e+Nj46eCSNUjQWd67MJVDIHmUV6/5Y0=" /></div>
        <div class="row">
  <label for="place_restaurant_name">Название</label> 
  <input id="place_restaurant_name" name="place_restaurant[name]" size="30" type="text" />
</div>
<div class="row">
  <label for="place_restaurant_address">Адрес</label> 
  <input id="place_restaurant_address" name="place_restaurant[address]" size="30" type="text" />
</div>

        <div class="row">
            <label for="place_restaurant_average_check">Средний чек</label>
            <input id="place_restaurant_average_check" name="place_restaurant[average_check]" size="30" type="text" />
        </div>
            <div class="row">
                <input id="place_restaurant_food_types_1" name="place_restaurant[food_types][1]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_1">Восточная кухня</label>      
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_2" name="place_restaurant[food_types][2]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_2">Национальная кухня</label>       
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_3" name="place_restaurant[food_types][3]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_3">Японская кухня</label>       
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_4" name="place_restaurant[food_types][4]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_4">Китайская кухня</label>      
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_5" name="place_restaurant[food_types][5]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_5">Европейская кухня</label>        
            </div>
            <div class="row">
                <input id="place_restaurant_food_types_6" name="place_restaurant[food_types][6]" type="checkbox" value="1" />
                <label for="place_restaurant_food_types_6">Кавказская кухня</label>     
            </div>
        <div id="map_canvas"></div>
<input id="latitude" name="place_restaurant[latitude]" type="hidden" value="54.352271" />
<input id="longitude" name="place_restaurant[longitude]" type="hidden" value="48.52652" />


        <div class="row">
            <input id="submit" name="commit" type="submit" value="Сохранить" />
        </div>
</form>

3 个答案:

答案 0 :(得分:1)

你可以使用

check("place_restaurant_food_types_1")

docs所示:

  

可以通过姓名,ID或标签文本找到该复选框。

答案 1 :(得分:0)

像这样尝试Xpath

find(:xpath, "//*[@id='place_restaurant_food_types_1']").click

答案 2 :(得分:0)

你可以这样使用

find(:css,"input[id='place_restaurant_food_types_1']").set true