我试图与Capybara规格一起使用PageObjects,但似乎无法正确地引用驱动程序。基本上,我希望能够使用PageObject来定义页面上的字段(这是login_page.rb
(,但是当我尝试在规格中创建对象时,它会丢弃错误,说该对象为nil。
spec_helper.rb :
# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'page-object'
# loading page object files
page_paths = File.join(Dir.pwd, 'spec', 'pages', '**', '*.rb')
puts 'foo'
Dir.glob(page_paths).each { |file| puts file}
Dir.glob(page_paths).each { |file| require file }
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.default_driver = :firefox
Capybara.app_host = *********** #redacted
Capybara.default_max_wait_time = 5
RSpec.configure do |config|
config.before(:all) do
@browser = Capybara::Selenium::Driver
end
config.before(:each) do
config.include Capybara::DSL
end
end
login_page.rb
class LoginPage
include Capybara::DSL
include PageObject
text_field(:username, id: 'username')
text_field(:password, id: 'password')
button(:login, id: 'loginButton')
def initialize(driver)
@driver = driver
end
end
login_spec.rb
require 'spec_helper'
describe 'On Login page' do
context 'using proper password' do
before(:each) do
visit('/')
end
it 'logs in as foo' do
login_page = LoginPage.new(@browser)
login_page.username = 'foo'
login_page.password = 'bar'
login_page.login
end
end
end
假设您在谈论page-object
GEM -https://github.com/cheezy/page-object-不支持辣椒硒 - 韦布驱动器。另外,Capybara::Selenium::Driver
是类而不是对象实例。如page-object
REDME所示,您需要将对象实例传递到页面对象constructor
@browser = Selenium::WebDriver.for :firefox
如果您想要一个支持Capybara的页面对象框架,则可能需要查看site-prism
之类的东西。