将Capybara会话传递给页面对象

时间:2015-10-12 23:44:20

标签: ruby capybara rspec3

我试图使用capybara和rspec来简单实现页面对象模式。我需要从不同的页面对象实例方法初始化新的页面对象。但在初始化新页面对象后,系统会抱怨

  

未定义的局部变量或方法`page'

我想我应该以某种方式将驱动程序会话传递给新对象,但我不知道如何 这是我的第一堂课:

  class DashBoardLoginPage
  include Dashboard
  include Logging
  include Locator
  include Capybara::DSL

  def initialize
    super
    @locator = locators_for :dashboard_login_page_selectors
    log.info "Dashboard Login page initialized"
  end 

  #param user is a hash with name and pass keys 
  def login_user user
    visit '/'

    fill_in user_name_field, with: user[:name]
    fill_in user_password_field, with: user[:password]
    click_button login_button
    page.save_screenshot "#{Time.now.getutc}_screenshot.png"

    dashboard_landing_page = DashboardLandingPage.new
  end
end

和我的第二个对象:

require_relative "dashboard_base"

class DashboardLandingPage < DashboardBasePage

  def initialize
    super
    @locator = locators_for :dashboard_login_page_selectors
    log.info "Dashboard Landing page initialized"
  end

  def is_open?
    page.has_title? "Apps - Fyber Ad Monetization Dashboard"
  end
end

2 个答案:

答案 0 :(得分:1)

您需要在DashboardLandingPage中包含Capybara :: DSL

答案 1 :(得分:0)

欢迎使用stackoverflow!

您可以通过page访问Capybara.current_session

另外,您是否考虑过使用已有的页面对象宝石?他们可以简化你正在做的很多事情。

https://github.com/natritmeyer/site_prism https://github.com/andyw8/capybara-page-object