summaryrefslogtreecommitdiff
path: root/spec/support/helpers/cookie_helper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/helpers/cookie_helper.rb')
-rw-r--r--spec/support/helpers/cookie_helper.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/spec/support/helpers/cookie_helper.rb b/spec/support/helpers/cookie_helper.rb
new file mode 100644
index 00000000000..5ff7b0b68c9
--- /dev/null
+++ b/spec/support/helpers/cookie_helper.rb
@@ -0,0 +1,34 @@
+# Helper for setting cookies in Selenium/WebDriver
+#
+module CookieHelper
+ def set_cookie(name, value, options = {})
+ 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 = {})
+ # 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
+
+ def rack_set_cookie(name, value)
+ page.driver.browser.set_cookie("#{name}=#{value}")
+ end
+
+ def get_cookie(name)
+ page.driver.browser.manage.cookie_named(name)
+ end
+
+ 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