Last active
December 10, 2015 02:28
-
-
Save bilus/4367281 to your computer and use it in GitHub Desktop.
Here's a slightly cleaned-up version that also supports Webkit. DISCLAIMER: It's based on the code above so it's supposed to work with Selenium & RackTest *but* I've only tested it against Webkit. It does not support the @announce tag.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CookieStepHelper | |
class Cookies | |
def Cookies.for(session) | |
case session.driver | |
when Capybara::Driver::Selenium | |
SeleniumCookies.new(session.driver) | |
when Capybara::Driver::RackTest | |
RackTestCookies.new(session.driver) | |
when Capybara::Webkit::Driver | |
WebkitCookies.new(session.driver) | |
else | |
raise "Unsupported driver." | |
end | |
end | |
def all_cookies | |
end | |
def delete_cookie(cookie_name) | |
end | |
def to_s | |
all_cookies.map(&:inspect).join("\n") | |
end | |
end | |
class SeleniumCookies < Cookies | |
def initialize(driver) | |
@browser = driver.browser | |
end | |
def all_cookies | |
@browser.manage.all_cookies | |
end | |
def delete_cookie(cookie_name) | |
@browser.manage.delete_cookie(cookie_name) | |
end | |
end | |
class RackTestCookies < Cookies | |
def initialize(driver) | |
@driver = driver | |
end | |
def all_cookies | |
cookie_jar.instance_variable_get(:@cookies) | |
end | |
def delete_cookie(cookie_name) | |
all_cookies.reject! do |existing_cookie| | |
existing_cookie.name.downcase == cookie_name | |
end | |
end | |
private | |
def cookie_jar | |
@driver.current_session.instance_variable_get(:@rack_mock_session).cookie_jar | |
end | |
end | |
class WebkitCookies < Cookies | |
def initialize(driver) | |
@browser = driver.browser | |
end | |
def all_cookies | |
@browser.get_cookies | |
end | |
def delete_cookie(cookie_name) | |
cookies = @browser.get_cookies.reject {|cookie| cookie[/#{cookie_name}=/]} | |
@browser.clear_cookies | |
cookies.each {|cookie| browser.set_cookie(cookie)} | |
end | |
end | |
end | |
Then /^show me the cookies!$/ do | |
cookies = CookieStepHelper::Cookies.for(Capybara.current_session).to_s | |
puts "Cookies: #{cookies}" | |
end | |
Given /^([a-zA-Z]+) closes (?:his|her) browser$/ do |user| | |
Capybara.session_name = user | |
session_name = .... # Provide session name, read it from Rails config etc. | |
CookieStepHelper::Cookies.for(Capybara.current_session).delete_cookie(session_name) # Criticue-specific. | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment