如何从 Capybara::Session 对象获取页面对象

How to get the page object from a Capybara::Session object

我正在尝试使用水豚的方法page.should have_content("some txt").

我通过获取水豚会话来使用水豚,但不知道如何获取页面对象

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.current_driver = :selenium
capdriver = Capybara::Session.new(:selenium)
capdriver.visit ("https://google.com")

如何使用上面的活动会话(capdriver)来使用它:

page.should have_content("some google text")

我可能设置错了水豚(我习惯直接使用Selenium::Webdriver)。

page 只是在 Capybara::DSL 上定义的一种方法,即 returns Capybara.current_session 当您将 Capybara::DSL 包含在测试中时 class 使用 Capybaras 自动会话管理时。由于您是手动管理会话,因此您已经可以访问要针对其断言的 Capybara::Session 对象,因此

capdriver.should have_content("some google text")

请注意,这假设您已将 Capybara::RSpecMatchers 包含在您 运行 所在的任何对象范围中(因此 have_content 可用)