oliyoung
oliyoung

Reputation: 141

Capybara: fill_in action locator

So I have this spec

describe "visit /signup" do
    before(:each) do
        get signup_path
    end
    it "has an email input field" do
        page.has_field?("#user_email")
    end
    it "accepts an email address" do
        page.fill_in('#user_email', :with=>Faker::Internet.email)
    end
end

The first test (has an email input) passes, the second fails with

Failure/Error: page.fill_in('#user_email', :with=>Faker::Internet.email)
Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label '#user_email' found

The input[type='text'] element exists on the page with that DOM ID, have tried locating with the ID with and without the hash, and using its input:name as a locator too.

What am I doing wrong?

Upvotes: 3

Views: 5054

Answers (3)

Ryan Bigg
Ryan Bigg

Reputation: 107728

It's because you're using get when you should be using visit inside the before block. This:

before(:each) do
  get signup_path
end

Should be this:

before(:each) do
    visit signup_path
end

Otherwise you're telling Rack::Test to visit that path, not Capybara! A small distinction that trips quite a few people up frequently!

Upvotes: 3

binarycode
binarycode

Reputation: 1806

maybe you should remove the #, e.g.

fill_in('user_email', :with=>Faker::Internet.email)

Upvotes: 2

Chris Barretto
Chris Barretto

Reputation: 9529

I think fill_in is not on page. Just use fill_in:

describe "visit /signup" do
    before(:each) do
        get signup_path
    end
    it "has an email input field" do
        page.has_field?("#user_email")
    end
    it "accepts an email address" do
        fill_in('#user_email', :with=>Faker::Internet.email)
    end
end

Upvotes: 0

Related Questions