Skip to content

Commit

Permalink
Added Cuprite adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
espen committed Nov 18, 2020
1 parent f9ec1e2 commit f172929
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ group :test do
gem 'rspec', '~> 3.0'
gem 'poltergeist'
gem 'sinatra'
gem 'cuprite'
gem 'selenium-webdriver'
gem 'webdrivers', '~> 4.0' # chromedriver installer
end
12 changes: 11 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ GEM
xpath (~> 3.2)
childprocess (3.0.0)
cliver (0.3.2)
concurrent-ruby (1.1.7)
cuprite (0.11)
capybara (>= 2.1, < 4)
ferrum (~> 0.9.0)
diff-lcs (1.3)
ferrum (0.9)
addressable (~> 2.5)
cliver (~> 0.3)
concurrent-ruby (~> 1.1)
websocket-driver (>= 0.6, < 0.8)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
mustermann (1.1.1)
Expand Down Expand Up @@ -76,6 +85,7 @@ PLATFORMS
ruby

DEPENDENCIES
cuprite
poltergeist
rake
rspec (~> 3.0)
Expand All @@ -85,4 +95,4 @@ DEPENDENCIES
webdrivers (~> 4.0)

BUNDLED WITH
2.0.2
2.1.4
2 changes: 2 additions & 0 deletions lib/show_me_the_cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module ShowMeTheCookies
require 'show_me_the_cookies/adapters/poltergeist'
require 'show_me_the_cookies/adapters/selenium'
require 'show_me_the_cookies/adapters/selenium_chrome'
require 'show_me_the_cookies/adapters/cuprite'

@adapters = {}
class << self
Expand All @@ -23,6 +24,7 @@ def register_adapter(driver, adapter)
register_adapter(:selenium_chrome_headless, ShowMeTheCookies::SeleniumChrome)
register_adapter(:rack_test, ShowMeTheCookies::RackTest)
register_adapter(:poltergeist, ShowMeTheCookies::Poltergeist)
register_adapter(:cuprite, ShowMeTheCookies::Cuprite)

# puts a string summary of the cookie
def show_me_the_cookie(cookie_name)
Expand Down
50 changes: 50 additions & 0 deletions lib/show_me_the_cookies/adapters/cuprite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module ShowMeTheCookies
class Cuprite
def initialize(driver)
@browser = driver.browser
@driver = driver
end

def get_me_the_cookie(name)
cookie = cookies_hash[name.to_s]
translate(cookie) unless cookie.nil?
end

def get_me_the_cookies
cookies_hash.map {|name, cookie| translate(cookie) }
end

def expire_cookies
cookies_hash.each do |name, cookie|
delete_cookie(name) if (cookie.expires rescue nil).nil?
end
end

def delete_cookie(name)
@driver.remove_cookie(name.to_s)
end

def create_cookie(name, value, options)
# see: https://github.com/rubycdp/cuprite#manipulating-cookies
@driver.set_cookie(name, value, options)
end

private

def cookies_hash
@driver.cookies
end

def translate(cookie)
{
:name => cookie.name,
:domain => cookie.domain,
:value => cookie.value,
:expires => (cookie.expires rescue nil),
:path => cookie.path,
:secure => cookie.secure?,
:httponly => cookie.httponly?
}
end
end
end
27 changes: 27 additions & 0 deletions spec/drivers/cuprite_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'
require 'shared_examples_for_api'
require 'capybara/cuprite'

RSpec.describe 'Cuprite', type: :feature do
before(:each) do
Capybara.current_driver = :cuprite
end

describe 'the testing rig' do
it 'should load the sinatra app' do
visit '/'
expect(page).to have_content('Cookie setter ready')
end
end

describe 'get_me_the_cookie' do
it 'reads httponly option' do
visit '/set_httponly/foo/bar'
expect(get_me_the_cookie('foo')).to include(
name: 'foo', value: 'bar', httponly: true
)
end
end

it_behaves_like 'the API'
end

0 comments on commit f172929

Please sign in to comment.