How to check a checkbox in capybara?

前端 未结 13 1928
灰色年华
灰色年华 2020-11-30 23:34

I\'m using Rspec and Capybara.

How can I write a step to check a checkbox? I\'ve tried check by value but it can\'t find my checkbox<

13条回答
  •  旧巷少年郎
    2020-11-30 23:52

    Had some issues with custom checkbox which is hidden behind label element. Needed a allow_label_click: true.

    With reference to this blog post,

    check 'checkbox[name]', allow_label_click: true
    

    For cases where there is a link in your label like "I agree to terms and conditions", the above code will open the page, which is not what you want.

    Do this instead.

    find(:css, "#checkbox_id", visible: false).execute_script('this.checked = true')
    

提交回复
热议问题