使用Capybara和selenium-webdriver打开新的私人窗口

时间:2018-10-01 15:49:45

标签: ruby-on-rails selenium-webdriver capybara

我如何同时打开2个窗口,但以私人模式打开一个窗口?

水豚的方法(link

open_new_window

打开一个新窗口,但不接受任何参数。

也尝试过:

    page.execute_script 'window.open(link, "mywindow", "private=yes");'

但新窗口仍未处于私有模式。

我正在使用:

  • 水豚(2.11.0)
  • selenium-webdriver(2.53.4)
  • Firefox v46

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

要有一个私有模式窗口和一个非私有模式窗口,您需要有两个会话和两个驱动程序注册。在当前版本的Capybara,selenium-webdriver和具有手动会话管理功能的Firefox中,可以通过以下方式完成

Capybara.register_driver :selenium_firefox_private do |app|
  firefox_options = ::Selenium::WebDriver::Firefox::Options.new
  firefox_options.args << "-private"
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: firefox_options)
end

然后在测试中需要专用窗口时,将为该专用窗口创建一个新会话

# Assuming you already have a session going and are interacting with the page
# with `Capybara.javascript_driver = :selenium` or equivalent set
page.find(...) # everything called on page is happening in the original non-private session

private_session = Capybara::Session.new(:selenium_firefox_private, Capybara.app)
private_session.visit(...) # anything called on private_session will happen in the private firefox instance

另一种选择是使用Capybaara.using_driver

Capybara.using_driver(:selenium_chrome_private) do
  # everything in this block would happen in a private browsing session that is auto created by Capybara (if it hadn't already been created)
  page.visit('/')
end

您使用的过时版本也是如此,但是驱动程序注册中的Selenium配置可能会有所不同。