水豚:fill_in 动作定位器

发布于 2024-12-04 01:56:02 字数 694 浏览 2 评论 0原文

所以我有这个规范

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

第一个测试(有电子邮件输入)通过,第二个测试失败,

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

输入[type ='text']元素存在于具有该DOM ID的页面上,已尝试使用带或不带散列的ID进行定位,并使用其 input:name 作为定位器。

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他夏了夏天 2024-12-11 01:56:02

这是因为当您应该在 before 块内使用 visit 时,您却使用了 get 。这:

before(:each) do
  get signup_path
end

应该是这样:

before(:each) do
    visit signup_path
end

否则你是在告诉Rack::Test访问该路径,而不是水豚!一个小小的区别经常让很多人绊倒!

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!

又怨 2024-12-11 01:56:02

也许你应该删除 #,例如

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

maybe you should remove the #, e.g.

fill_in('user_email', :with=>Faker::Internet.email)
你的心境我的脸 2024-12-11 01:56:02

我认为 fill_in 不在页面上。只需使用 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文