带有无头铬的水豚无法清除使用不同子域的测试用例之间的会话 [英] Capybara with headless chrome doesn't clear session between test cases which use different subdomains

查看:81
本文介绍了带有无头铬的水豚无法清除使用不同子域的测试用例之间的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Rails测试从capybara-webkit切换到了无头Chrome。当我运行不访问默认Capybara主机的测试时,第一种情况通过了,但是第二种情况失败了,因为用户在尝试登录时已经登录了

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

chromedriver v2.45 selenium-webdriver(3.141.0) capybara(2.18。 0)

我有以下设置:

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

其中 using_app_host

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

但这没有帮助。

规范结构如下:

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

任何想法为什么在导航到另一个域时,capybara / headless chrome无法清理测试用例之间的用户会话吗?

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

推荐答案

您是否正在存储会话浏览器 window.localStorage 和/或 window.sessionStorage 中的信息?如果是这样,您可以通过传递给驱动程序的选项设置清除这些内容(注意:这些设置是Capybara 3.12+中硒驱动程序的默认设置)

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  

这篇关于带有无头铬的水豚无法清除使用不同子域的测试用例之间的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆