Skip to content

Commit 2264ac1

Browse files
authored
Allow extra arguments for CookieInformation (robotframework#1310)
Allow extra arguments for CookieInformation
1 parent b504f1e commit 2264ac1

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/SeleniumLibrary/keywords/cookie.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,22 @@ def get_cookie(self, name):
9090
| secure | When true, cookie is only used with HTTPS connections. |
9191
| httpOnly | When true, cookie is not accessible via JavaScript. |
9292
| expiry | Python datetime object indicating when the cookie expires. |
93+
| extra | Possible attributes outside of the WebDriver specification |
9394
9495
See the
95-
[https://w3c.github.io/webdriver/webdriver-spec.html#cookies|WebDriver specification]
96+
[https://w3c.github.io/webdriver/#cookies|WebDriver specification]
9697
for details about the cookie information.
9798
Notice that ``expiry`` is specified as a
9899
[https://docs.python.org/3/library/datetime.html#datetime.datetime|datetime object],
99100
not as seconds since Unix Epoch like WebDriver natively does.
100101
102+
In some cases, example when running browser in the cloud, it is possible that
103+
cookie contains other attributes than is defined in the
104+
[https://w3c.github.io/webdriver/#cookies|WebDriver specification].
105+
These other attributes are available in a ``extra`` attribute in the cookie
106+
object and it contains a dictionary of the other attributes. The ``extra``
107+
attribute is new in SeleniumLibrary 4.0.
108+
101109
Example:
102110
| `Add Cookie` | foo | bar |
103111
| ${cookie} = | `Get Cookie` | foo |
@@ -152,16 +160,20 @@ def _expiry(self, expiry):
152160
class CookieInformation(object):
153161

154162
def __init__(self, name, value, path=None, domain=None, secure=False,
155-
httpOnly=False, expiry=None):
163+
httpOnly=False, expiry=None, **extra):
156164
self.name = name
157165
self.value = value
158166
self.path = path
159167
self.domain = domain
160168
self.secure = secure
161169
self.httpOnly = httpOnly
162170
self.expiry = datetime.fromtimestamp(expiry) if expiry else None
171+
self.extra = extra
163172

164173
def __str__(self):
165174
items = 'name value path domain secure httpOnly expiry'.split()
166-
return '\n'.join('%s=%s' % (item, getattr(self, item))
167-
for item in items)
175+
string = '\n'.join('%s=%s' % (item, getattr(self, item))
176+
for item in items)
177+
if self.extra:
178+
string = '%s%s=%s\n' % (string, 'extra', self.extra)
179+
return string

utest/test/keywords/test_cookie.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from datetime import datetime
12
import unittest
23

34
from mockito import mock, unstub, verify
45

56
from SeleniumLibrary.keywords import CookieKeywords
7+
from SeleniumLibrary.keywords.cookie import CookieInformation
68

79

810
class KeywordArgumentsCookieTest(unittest.TestCase):
@@ -72,3 +74,41 @@ def test_add_cookie_path_false(self):
7274
self.cookie.add_cookie('name', 'value', path='None', domain=None,
7375
secure=None)
7476
verify(self.driver).add_cookie(self.default_cookie)
77+
78+
79+
class CookieObjecttest(unittest.TestCase):
80+
81+
all_args = {'name': 'foo', 'value': '123', 'path': '/', 'domain': 'not.Here',
82+
'secure': True, 'httpOnly': True, 'expiry': 123}
83+
84+
def test_name_value_only(self):
85+
cookie = CookieInformation(name='foo', value='bar')
86+
self.assertEqual(cookie.name, 'foo')
87+
self.assertEqual(cookie.value, 'bar')
88+
89+
def test_all_args(self):
90+
cookie = CookieInformation(**self.all_args)
91+
self.assertEqual(cookie.name, 'foo')
92+
self.assertEqual(cookie.value, '123')
93+
self.assertEqual(cookie.path, '/')
94+
self.assertEqual(cookie.domain, 'not.Here')
95+
self.assertEqual(cookie.secure, True)
96+
self.assertEqual(cookie.httpOnly, True)
97+
self.assertEqual(cookie.expiry, datetime.fromtimestamp(123))
98+
self.assertEqual(cookie.extra, {})
99+
100+
def test_extra_args(self):
101+
cookie_dict = self.all_args.copy()
102+
cookie_dict['class_name'] = 'seleniumLibary'
103+
cookie = CookieInformation(**cookie_dict)
104+
self.assertEqual(cookie.name, 'foo')
105+
self.assertEqual(cookie.value, '123')
106+
self.assertEqual(cookie.extra, {'class_name': 'seleniumLibary'})
107+
string = str(cookie)
108+
self.assertIn("extra={'class_name': 'seleniumLibary'}", string)
109+
110+
def test_no_mandatory_args(self):
111+
cookie_dict = self.all_args.copy()
112+
del cookie_dict['name']
113+
with self.assertRaises(TypeError):
114+
CookieInformation(**cookie_dict)

0 commit comments

Comments
 (0)