我尝试使用cucumber / rspec / capybara测试一个简单的登录表单。但我在检测页面上的元素和进行任何类型的测试时遇到问题。
无法找到字段"用户名"没有禁用(Capybara :: ElementNotFound)
功能文件
Feature: Login
Scenario: correct login credentials
Given I am on the admin_login page
When I fill in "username" with "thomas"

ENV
require 'capybara'
require 'rspec'
require 'capybara/cucumber'
require 'rack'
require_relative '../../config'
ENV['RACK_ENV'] = 'test'
Capybara.ignore_hidden_elements = false
Capybara.app = Sinatra::Application
class Sinatra::ApplicationWorld
include RSpec::Expectations
include RSpec::Matchers
include Capybara::DSL
end
World do
Sinatra::ApplicationWorld.new
end

WEB STEP
When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)"(?: within "([^\"]*)")?$/ do |field, value, selector|
with_scope(selector) do
fill_in(field,visible: false, :with => value)
end
end

HTML
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Please Sign In To Pied Piper</h3>
</div>
<div class="panel-body">
<form role="form" action="/admin/login" method="post" label="form">
<fieldset label ="form" >
<div class="form-group">
<input label="username" class="form-control" placeholder="username" name="username" id="username" type="text" value="<%= params[:username] %>" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="<%= params[:password]%>">
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">Remember Me
</label>
</div>
<button type="submit"><a class="btn btn-lg btn-success btn-block">Login</a></button>
<button><a href="/" class="btn btn-lg btn-success btn-block">Return to Main Site</a></button>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:0)
我在使用Rails时遇到了同样的问题,而解决该问题的方法是为该字段提供一个ID
<%= f.text_field :name, class: 'form-control',id:"name" %>