加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Capybara不会填写我的登录字段

发布时间:2020-12-17 03:02:24 所属栏目:百科 来源:网络整理
导读:我无法让Capybara在我的登录字段中填写值.这是login_spec.rb require 'spec_helper'describe "the login process" do it "signs me in" do visit '/login' fill_in 'user_username',:with = 'Admin' fill_in 'user_password',:with = 'Password' endend 当我
我无法让Capybara在我的登录字段中填写值.这是login_spec.rb

require 'spec_helper'

describe "the login process" do
  it "signs me in" do
    visit '/login'    
    fill_in 'user_username',:with => 'Admin'
    fill_in 'user_password',:with => 'Password'
  end
end

当我运行测试时,它失败并出现以下错误:

1) the login process signs me in
    Failure/Error: fill_in 'user_username',:with => 'Admin'
    Capybara::ElementNotFound:
    no text field,text area or password field with id,name,or label 'user_username' found
    # (eval):2:in `fill_in'
    # ./spec/integration/login_spec.rb:7:in `block (2 levels) in <top (required)>'

但是当我使用打印页面的内容时

print page.html

我可以清楚地看到元素ID是正确的

<h2>Already Have an Account?</h2>
<form accept-charset="UTF-8" action="/login" id="sidebar_login" method="post">
<div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="?"><input name="authenticity_token" type="hidden" value="DccrBxhdS7WTAM/iEd08L4z9zt8MjBKTxQ0s6lMVIAA=">
</div>
<div class="form"></div>
<div class="login_form_widget">
Username
<div class="inputdiv_widget">
    <input class="textfield" id="user_username" name="user[username]" type="text">
</div>
Password
<div class="inputdiv_widget">
    <input class="textfield" id="user_password" name="user[password]" type="password">
</div>

我已经尝试使用元素的id(user_username)和name(用户[username]),所有这些都具有相同的结果.这里有什么我想念的吗?

解决方法

如果您尝试添加如下标签,该怎么办?

<label for="user_username">Username</label>

并在您的规范中使用:

within ".login_form_widget" do
  fill_in 'Username',:with => 'Admin'
end

找到标签特别有用,但除此之外,我没有看到您在上面使用的语法有任何问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读