我正在尝试使用pageobjects和我的Capybara规格,但似乎无法正确引用驱动程序。基本上我希望能够使用PageObjects来定义页面上的字段(这是login_page.rb
),但是当我尝试在规范中创建对象时,它会抛出错误,说对象是零。
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
答案 0 :(得分:1)
假设您正在谈论page-object
宝石 - https://github.com/cheezy/page-object - 它不支持Capybara,它支持watir-webdriver / watir和selenium-webdriver。另外Capybara::Selenium::Driver
是一个类而不是对象实例。如page-object
自述文件所示,您需要将对象实例传递给页面对象构造函数
@browser = Selenium::WebDriver.for :firefox
如果您想要一个支持Capybara的页面对象框架,您可能需要查看类似site-prism
的内容。