Capybara with headless chrome doesn't clear session between test cases which use different subdomains

本小妞迷上赌 提交于 2020-06-16 20:50:08

问题


I switched my rails tests from capybara-webkit to headless chrome. When I run a test which visits not the default Capybara host the first case passes but the second one fails because the user are already logged in when they try to login

I use chromedriver v2.45, selenium-webdriver (3.141.0) and capybara (2.18.0)

I have the following setup:

require 'selenium-webdriver'

Capybara.register_driver :chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new(
    args: %w[headless disable-gpu no-sandbox]
  )
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.javascript_driver = :chrome

I tried to change the app host to the default domain after visiting another domain

using_app_host("http://another.lvh.me") do
  visit '/'

  # do something
end

where using_app_host is

def using_app_host(host)
  original_host = Capybara.app_host
  Capybara.app_host = host
  yield
ensure
  Capybara.app_host = original_host
end

but it didn't help.

The spec structure looks the following way:

feature "Use another subdomain", js: true do
  before { login } # use default Capybara app host http://root.lvh.me

  scenario "case 1" do
    using_app_host("http://another.lvh.me") do
      # do something
    end
  end

  scenario "case 2" do
    using_app_host("http://another.lvh.me") do
      # do something else
    end
  end
end

Any ideas why capybara/headless chrome doesn't clean the user session between the test cases when navigating to another domain?


回答1:


Are you storing session information in the browsers window.localStorage and/or window.sessionStorage? If so you can set those to be cleared via options passed to the driver (Note: these settings are the default for the selenium driver in Capybara 3.12+)

Capybara.register_driver :chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new(args: %w[no-sandbox])
  options.headless!
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options, clear_local_storage: true, clear_session_storage: true)
end  



回答2:


Even I was facing same issue.

After adding the steps to clear the cookies, session it is not working either. I added below code in env.rb to start a new session every time for a new test

May be you can try this.

Before do
  Capybara.session_name = ":session_#{Time.zone.now.to_i}" 
end

After do
  Capybara.current_session.driver.quit
end

Also, you can add in chrome options to open the session in incognito window




回答3:


I found this thread useful in a reverse context. I have a test setup wherein I'm storing session credentials in local storage. And so upgrading from capybara v3.11 to v3.12 broke the suite such that only the first scenario would pass and the rest of the scenarios would fail on the login page every time. That's because the local storage was getting cleared based on the default behavior of capybara 3.12

I updated my suite to set clear_local_storage and clear_session_storage to false explicitly at time of registering the driver.

Capybara.register_driver :selenium_chrome do |app|
    Capybara::Selenium::Driver.new(app, 
                                   browser: :chrome, 
                                   clear_local_storage: false, 
                                   clear_session_storage: false) 


来源:https://stackoverflow.com/questions/53390321/capybara-with-headless-chrome-doesnt-clear-session-between-test-cases-which-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!