Skip to content

Commit 7b338ec

Browse files
Add: Same site cookie feature in python bindings (#8114)
* Add: Adding sameSite cookie attribute to python bindings Co-authored-by: David Burns <[email protected]>
1 parent d38dfb3 commit 7b338ec

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

py/selenium/webdriver/remote/webdriver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,15 +947,20 @@ def add_cookie(self, cookie_dict):
947947
948948
:Args:
949949
- cookie_dict: A dictionary object, with required keys - "name" and "value";
950-
optional keys - "path", "domain", "secure", "expiry"
950+
optional keys - "path", "domain", "secure", "expiry", "sameSite"
951951
952952
Usage:
953953
driver.add_cookie({'name' : 'foo', 'value' : 'bar'})
954954
driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/'})
955955
driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/', 'secure':True})
956+
driver.add_cookie({'name': 'foo', 'value': 'bar', 'sameSite': 'Strict'})
956957
957958
"""
958-
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
959+
if 'sameSite' in cookie_dict:
960+
assert cookie_dict['sameSite'] in ['Strict', 'Lax']
961+
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
962+
else:
963+
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
959964

960965
# Timeouts
961966
def implicitly_wait(self, time_to_wait):

py/test/selenium/webdriver/common/cookie_tests.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import pytest
2323

24+
from selenium.common.exceptions import WebDriverException
2425

2526
@pytest.fixture
2627
def cookie(webserver):
@@ -32,19 +33,52 @@ def cookie(webserver):
3233
'secure': False}
3334
return cookie
3435

36+
@pytest.fixture
37+
def same_site_cookie_strict(webserver):
38+
same_site_cookie_strict = {
39+
'name': 'foo',
40+
'value': 'bar',
41+
'path': '/',
42+
'domain': webserver.host,
43+
'sameSite': 'Strict',
44+
'secure': False}
45+
return same_site_cookie_strict
46+
47+
@pytest.fixture
48+
def same_site_cookie_lax(webserver):
49+
same_site_cookie_lax = {
50+
'name': 'foo',
51+
'value': 'bar',
52+
'path': '/',
53+
'domain': webserver.host,
54+
'sameSite': 'Lax',
55+
'secure': False}
56+
return same_site_cookie_lax
3557

3658
@pytest.fixture(autouse=True)
3759
def pages(request, driver, pages):
3860
pages.load('simpleTest.html')
3961
yield pages
4062
driver.delete_all_cookies()
4163

42-
4364
def testAddCookie(cookie, driver):
4465
driver.add_cookie(cookie)
4566
returned = driver.execute_script('return document.cookie')
4667
assert cookie['name'] in returned
4768

69+
@pytest.mark.xfail_firefox(raises=WebDriverException,
70+
reason='sameSite cookie attribute not implemented')
71+
def testAddCookieSameSiteStrict(same_site_cookie_strict, driver):
72+
driver.add_cookie(same_site_cookie_strict)
73+
returned = driver.get_cookie('foo')
74+
assert returned['sameSite'] == 'Strict'
75+
76+
@pytest.mark.xfail_firefox(raises=WebDriverException,
77+
reason='sameSite cookie attribute not implemented')
78+
def testAddCookieSameSiteLax(same_site_cookie_lax, driver):
79+
driver.add_cookie(same_site_cookie_lax)
80+
returned = driver.get_cookie('foo')
81+
assert returned['sameSite'] == 'Lax'
4882

4983
@pytest.mark.xfail_ie
5084
def testAddingACookieThatExpiredInThePast(cookie, driver):

0 commit comments

Comments
 (0)