如何引用 Capybara 驱动程序来创建我的 pageobjects/specs。

How do I reference the Capybara driver for creating my pageobjects/specs.

我正在尝试将页面对象与我的 Capybara 规范一起使用,但似乎无法正确引用驱动程序。基本上我希望能够使用 PageObjects 来定义页面上的字段(这是 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 - 它不支持水豚,它支持 watir-webdriver/watir 和 selenium-webdriver。另外 Capybara::Selenium::Driver 是 class 而不是对象实例。如 page-object 自述文件所示,您需要将一个对象实例传递到您的页面对象构造函数中

@browser = Selenium::WebDriver.for :firefox

如果您想要一个支持 Capybara 的页面对象框架,您可能需要查看类似 site-prism 的内容。