Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Same site cookie feature in python bindings #8114

Merged
merged 9 commits into from
Mar 17, 2020
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:
harsha509 marked this conversation as resolved.
Show resolved Hide resolved
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