Search code examples
javascriptruby-on-railscheckboxrspeccapybara-webkit

Capybara with js: true not finding checkbox


I need to test if Javascript alert is being displayed in validation proccess after checking one checkbox. I'm using RSpec, Capybara with Webkit and Database Cleaner.

My test without JS: true :

  it "alerts to choose two players" do
    set_and_visit
    first("input[type='checkbox']").set(true)
    expect(page).to have_content('Remember that you must choose two players to start a game.')
  end

Finds a checkbox but cannot find the alert message.

When I add JS - "alerts to choose two players", js: true do it returns an error

NoMethodError:
       undefined method `set' for nil:NilClass

I've tried using check 'John Doe' and first("input[type='checkbox']").check but it still didn't work.


Solution

  • So, silly as it sounds the mistake I made was in a url - in my set_and_visit method I was visiting 'matches/new' instead of '/matches/new'. When tested, puts page.body was returning an empty string because it couldn't find that url. Still I'm not sure why test including check 'John Doe' wasn't returning any error like the one with js: true.