开发者

Capybara: fill_in action locator

开发者 https://www.devze.com 2023-04-03 05:29 出处:网络
So I have this spec describe \"visit /signup\" do before(:each) do get signup_path end it \"has an email input field\" do

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开发者_如何学Go=>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?


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!


maybe you should remove the #, e.g.

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消