Skip to content

Commit 68d51cf

Browse files
lmtierneytitusfortner
authored andcommitted
rb: add new w3c get cookie and delete all cookies functionality
Signed-off-by: Titus Fortner <titusfortner@gmail.com>
1 parent 8b47d0a commit 68d51cf

8 files changed

Lines changed: 86 additions & 21 deletions

File tree

rb/lib/selenium/webdriver/common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
require 'selenium/webdriver/common/window'
4040
require 'selenium/webdriver/common/logs'
4141
require 'selenium/webdriver/common/options'
42+
require 'selenium/webdriver/common/w3c_options'
4243
require 'selenium/webdriver/common/search_context'
4344
require 'selenium/webdriver/common/action_builder'
4445
require 'selenium/webdriver/common/touch_action_builder'

rb/lib/selenium/webdriver/common/driver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def switch_to
121121
#
122122

123123
def manage
124-
@manage ||= WebDriver::Options.new(bridge)
124+
bridge.options
125125
end
126126

127127
#

rb/lib/selenium/webdriver/common/options.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,7 @@ def delete_all_cookies
9090
#
9191

9292
def all_cookies
93-
@bridge.cookies.map do |cookie|
94-
{
95-
name: cookie['name'],
96-
value: cookie['value'],
97-
path: cookie['path'],
98-
domain: cookie['domain'] && strip_port(cookie['domain']),
99-
expires: cookie['expiry'] && datetime_at(cookie['expiry']),
100-
secure: cookie['secure']
101-
}
102-
end
93+
@bridge.cookies.map { |cookie| convert_cookie(cookie) }
10394
end
10495

10596
def timeouts
@@ -146,6 +137,17 @@ def seconds_from(obj)
146137
def strip_port(str)
147138
str.split(':', 2).first
148139
end
140+
141+
def convert_cookie(cookie)
142+
{
143+
name: cookie['name'],
144+
value: cookie['value'],
145+
path: cookie['path'],
146+
domain: cookie['domain'] && strip_port(cookie['domain']),
147+
expires: cookie['expiry'] && datetime_at(cookie['expiry']),
148+
secure: cookie['secure']
149+
}
150+
end
149151
end # Options
150152
end # WebDriver
151153
end # Selenium
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# encoding: utf-8
2+
#
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
class W3COptions < Options
23+
24+
#
25+
# Get the cookie with the given name
26+
#
27+
# @param [String] name the name of the cookie
28+
# @return [Hash, nil] the cookie, or nil if it wasn't found.
29+
#
30+
31+
def cookie_named(name)
32+
convert_cookie(@bridge.cookie(name))
33+
end
34+
35+
#
36+
# Delete all cookies
37+
#
38+
39+
def delete_all_cookies
40+
@bridge.delete_all_cookies
41+
end
42+
43+
end # WC3Options
44+
end # WebDriver
45+
end # Selenium

rb/lib/selenium/webdriver/remote/bridge.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ def execute_async_script(script, *args)
367367
# cookies
368368
#
369369

370+
def options
371+
@options ||= WebDriver::Options.new(self)
372+
end
373+
370374
def add_cookie(cookie)
371375
execute :addCookie, {}, {cookie: cookie}
372376
end

rb/lib/selenium/webdriver/remote/w3c_bridge.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ def execute_async_script(script, *args)
375375
# cookies
376376
#
377377

378+
def options
379+
@options ||= WebDriver::W3COptions.new(self)
380+
end
381+
378382
def add_cookie(cookie)
379383
execute :addCookie, {}, {cookie: cookie}
380384
end
@@ -383,7 +387,6 @@ def delete_cookie(name)
383387
execute :deleteCookie, name: name
384388
end
385389

386-
# TODO: - write specs
387390
def cookie(name)
388391
execute :getCookie, name: name
389392
end
@@ -393,7 +396,7 @@ def cookies
393396
end
394397

395398
def delete_all_cookies
396-
cookies.each { |cookie| delete_cookie(cookie['name']) }
399+
execute :deleteAllCookies
397400
end
398401

399402
#

rb/lib/selenium/webdriver/remote/w3c_commands.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class W3CBridge
9292
command :getCookie, :get, 'session/:session_id/cookie/:name'
9393
command :addCookie, :post, 'session/:session_id/cookie'
9494
command :deleteCookie, :delete, 'session/:session_id/cookie/:name'
95+
command :deleteAllCookies, :delete, 'session/:session_id/cookie'
9596

9697
#
9798
# timeouts

rb/spec/integration/selenium/webdriver/options_spec.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,36 @@ module WebDriver
7777
expect(cookies.first[:value]).to eq('bar')
7878
end
7979

80-
# Edge BUG - https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/5751773/
81-
not_compliant_on browser: :edge do
82-
it 'should delete one' do
80+
# Firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1282970
81+
not_compliant_on browser: :firefox do
82+
it 'should get named cookie' do
8383
driver.navigate.to url_for('xhtmlTest.html')
8484
driver.manage.add_cookie name: 'foo', value: 'bar'
8585

86-
driver.manage.delete_cookie('foo')
86+
expect(driver.manage.cookie_named('foo')[:value]).to eq('bar')
8787
end
8888
end
8989

9090
# Edge BUG - https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/5751773/
9191
not_compliant_on browser: :edge do
92-
it 'should delete all' do
92+
it 'should delete one' do
9393
driver.navigate.to url_for('xhtmlTest.html')
94-
9594
driver.manage.add_cookie name: 'foo', value: 'bar'
96-
driver.manage.delete_all_cookies
97-
expect(driver.manage.all_cookies).to be_empty
95+
96+
driver.manage.delete_cookie('foo')
97+
expect(driver.manage.all_cookies.find { |c| c[:name] == 'foo' }).to be_nil
9898
end
9999
end
100100

101+
it 'should delete all' do
102+
driver.navigate.to url_for('xhtmlTest.html')
103+
104+
driver.manage.add_cookie name: 'foo', value: 'bar'
105+
driver.manage.add_cookie name: 'bar', value: 'foo'
106+
driver.manage.delete_all_cookies
107+
expect(driver.manage.all_cookies).to be_empty
108+
end
109+
101110
# Firefox - https://bugzilla.mozilla.org/show_bug.cgi?id=1256007
102111
not_compliant_on browser: :firefox do
103112
it 'should use DateTime for expires' do

0 commit comments

Comments
 (0)