2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# Helper for setting cookies in Selenium/WebDriver
|
|
|
|
#
|
|
|
|
module CookieHelper
|
|
|
|
def set_cookie(name, value, options = {})
|
2018-05-09 12:01:36 +05:30
|
|
|
case page.driver
|
|
|
|
when Capybara::RackTest::Driver
|
|
|
|
rack_set_cookie(name, value)
|
|
|
|
else
|
|
|
|
selenium_set_cookie(name, value, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def selenium_set_cookie(name, value, options = {})
|
2018-03-17 18:26:18 +05:30
|
|
|
# Selenium driver will not set cookies for a given domain when the browser is at `about:blank`.
|
|
|
|
# It also doesn't appear to allow overriding the cookie path. loading `/` is the most inclusive.
|
|
|
|
visit options.fetch(:path, '/') unless on_a_page?
|
|
|
|
page.driver.browser.manage.add_cookie(name: name, value: value, **options)
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def rack_set_cookie(name, value)
|
|
|
|
page.driver.browser.set_cookie("#{name}=#{value}")
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def get_cookie(name)
|
|
|
|
page.driver.browser.manage.cookie_named(name)
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def wait_for_cookie_set(name)
|
|
|
|
wait_for("Complete setting cookie") do
|
|
|
|
get_cookie(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def on_a_page?
|
|
|
|
current_url = Capybara.current_session.driver.browser.current_url
|
|
|
|
current_url && current_url != '' && current_url != 'about:blank' && current_url != 'data:,'
|
|
|
|
end
|
|
|
|
end
|