Skip to content

Commit

Permalink
Add: Same site cookie feature in python bindings (#8114)
Browse files Browse the repository at this point in the history
* Add: Adding sameSite cookie attribute to python bindings

Co-authored-by: David Burns <[email protected]>
  • Loading branch information
harsha509 and AutomatedTester authored Mar 17, 2020
1 parent d38dfb3 commit 7b338ec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
9 changes: 7 additions & 2 deletions py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,15 +947,20 @@ def add_cookie(self, cookie_dict):
:Args:
- cookie_dict: A dictionary object, with required keys - "name" and "value";
optional keys - "path", "domain", "secure", "expiry"
optional keys - "path", "domain", "secure", "expiry", "sameSite"
Usage:
driver.add_cookie({'name' : 'foo', 'value' : 'bar'})
driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/'})
driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/', 'secure':True})
driver.add_cookie({'name': 'foo', 'value': 'bar', 'sameSite': 'Strict'})
"""
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
if 'sameSite' in cookie_dict:
assert cookie_dict['sameSite'] in ['Strict', 'Lax']
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
else:
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})

# Timeouts
def implicitly_wait(self, time_to_wait):
Expand Down
36 changes: 35 additions & 1 deletion py/test/selenium/webdriver/common/cookie_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import pytest

from selenium.common.exceptions import WebDriverException

@pytest.fixture
def cookie(webserver):
Expand All @@ -32,19 +33,52 @@ def cookie(webserver):
'secure': False}
return cookie

@pytest.fixture
def same_site_cookie_strict(webserver):
same_site_cookie_strict = {
'name': 'foo',
'value': 'bar',
'path': '/',
'domain': webserver.host,
'sameSite': 'Strict',
'secure': False}
return same_site_cookie_strict

@pytest.fixture
def same_site_cookie_lax(webserver):
same_site_cookie_lax = {
'name': 'foo',
'value': 'bar',
'path': '/',
'domain': webserver.host,
'sameSite': 'Lax',
'secure': False}
return same_site_cookie_lax

@pytest.fixture(autouse=True)
def pages(request, driver, pages):
pages.load('simpleTest.html')
yield pages
driver.delete_all_cookies()


def testAddCookie(cookie, driver):
driver.add_cookie(cookie)
returned = driver.execute_script('return document.cookie')
assert cookie['name'] in returned

@pytest.mark.xfail_firefox(raises=WebDriverException,
reason='sameSite cookie attribute not implemented')
def testAddCookieSameSiteStrict(same_site_cookie_strict, driver):
driver.add_cookie(same_site_cookie_strict)
returned = driver.get_cookie('foo')
assert returned['sameSite'] == 'Strict'

@pytest.mark.xfail_firefox(raises=WebDriverException,
reason='sameSite cookie attribute not implemented')
def testAddCookieSameSiteLax(same_site_cookie_lax, driver):
driver.add_cookie(same_site_cookie_lax)
returned = driver.get_cookie('foo')
assert returned['sameSite'] == 'Lax'

@pytest.mark.xfail_ie
def testAddingACookieThatExpiredInThePast(cookie, driver):
Expand Down

0 comments on commit 7b338ec

Please sign in to comment.