简体   繁体   中英

Open new private window with Capybara and selenium-webdriver

How can I open 2 windows simultaneously but one in private mode?

The capybara's method ( link )

open_new_window

opens a new window but it doesn't accept any parameters.

Also tried:

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

But still the new window is not in private mode.

I am using:

  • capybara (2.11.0)
  • selenium-webdriver (2.53.4)
  • Firefox v46

Any ideas?

To have a private mode window and a non private mode window you need to have two sessions, and two driver registrations. In the current releases of Capybara, selenium-webdriver, and Firefox with manual session management this could be done as

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

and then in your test when you need a private window you would create a new sessions for the private window

# 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

Another option would be to use 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

The same is true for the obsolete versions you're using but the Selenium configuration in the driver registration would probably be different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM