Search code examples
ruby-on-railscapybarapoltergeist

Capybara doesn't want to select an input node


My HTML/ERB looks like this

<fieldset class="row notifications">
    <legend>
        <hr class="dash blue inline">
        <%= t :my_notifications %>
    </legend>

    <label>
        <%= f.check_box(:subscribed_to_news) %>
        <span></span>
        <span class="checkbox-text"><%= t :accepts_to_receive_news %></span>
        <br>
    </label>
</fieldset>

When I debug my Cucumber test with Capybara, I do find the notification checkbox f.check_box(:subscribed_to_news) in this mess

page.find('.notifications label')['innerHTML']
# => "\n\t\t<input name=\"user[subscribed_to_news]\" type=\"hidden\" value=\"0\"><input type=\"checkbox\" value=\"1\" checked=\"checked\" name=\"user[subscribed_to_news]\" id=\"user_subscribed_to_news\">\n\t\t<span></span>\n\t\t<span class=\"checkbox-text\">blahblahblah</span>\n\t\t<br>\n\t"

But for some reason I cannot find the nested inputs nor find them by ID

page.find('.notifications label input') 
# => Capybara::ElementNotFound Exception: Unable to find css ".notifications label input"
page.find('.notifications label #user_subscribed_to_news') # => Capybara::ElementNotFound Exception: Unable to find css ".notifications label #user_subscribed_to_news"

Selecting the label does work though

page.find('.notifications label')
# => #<Capybara::Node::Element tag="label" path="//HTML[1]/BODY[1]/DIV[1]/MAIN[1]/SECTION[1]/FORM[1]/FIELDSET[3]/LABEL[1]">

What am I doing wrong ? I just want to check the damn checkbox :'(


Solution

  • It would seem that the checkbox is unreachable via normal css /xpath...

    I got away using some javascript

    page.execute_script(%Q{document.querySelector('#{area} input##{selector}').click()})