我正在将功能规范重写为页面对象,但fill_in
方法有问题。
代码如下:
页面对象:
class LoginPage < SitePrism::Page
set_url '/login'
section :navbar, NavbarSection, '.navbar-collapse'
section :drop_menu, DropMenuSection, '.popover-content'
element :email, 'input[name=email]'
element :password, 'input[type=password]'
element :submit, 'button[type=submit]'
element :help, 'a', text: "Help! I can't sign in"
end
规格:
require 'spec_helper'
feature 'Login', js: true do
let(:app) { App.new }
let(:login_page) { app.login }
scenario 'with invalid credentials results in error message' do
login_page.load
login_page.fill_in login_page.email, with: 'wrong@mail.com'
login_page.fill_in login_page.password, with: 'bad password'
login_page.submit.click
expect(login_page).to have_content 'Invalid credentials'
end
输入字段定义如下:
<input class="form-control input-lg ng-pristine ng-untouched
ng-invalid ng-invalid-required" name="email"
ng-model="login.credentials.email" placeholder="Email"
required="" tabindex="0" aria-required="true"
aria-invalid="true">`
当我运行测试时,我收到以下错误:
[2] pry(#<RSpec::ExampleGroups::Login>)> login_page.fill_in login_page.email,
with: 'wrong@mail.com'
Capybara::ElementNotFound: Unable to find field #<Capybara::Node::Element
tag="input" path="/html/body/div[1]/div/div/div/div/div[2]/div/form/div[1]/div/input">
但是,可以找到此字段:
[1] pry(#<RSpec::ExampleGroups::Login>)> login_page.email
=> #<Capybara::Node::Element tag="input"
path="/html/body/div[1]/div/div/div/div/div[2]/div/form/div[1]/div/input">
我想这可能与fill_in
使用find
CSS 时的使用方式有关。
有没有办法尽可能接近最终用户与表单的交互方式来处理它?我不想使用login_page.email.set 'wrong@mail.com'
,因为set
不关心该字段是否实际上是可点击/可填充的并且未被某些元素隐藏。