Skip to content

Commit 14ed29d

Browse files
rasjaniaaltat
authored andcommitted
Get Cookies keyword able to return dict now (#1263)
* Get Cookies keyword able to return dict now This is usefull as Requests Library's Create Session keywords expects cookies parameter to be a dict, not a string that Get Cookies returns by default. Fixes #979
1 parent ca66dfa commit 14ed29d

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

atest/acceptance/keywords/cookies.robot

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Get Cookies
1111
Should Match Regexp ${cookies}
1212
... ^(test=seleniumlibrary; another=value)|(another=value; test=seleniumlibrary)$
1313

14+
Get Cookies As Dict
15+
${cookies}= Get Cookies as_dict=True
16+
${expected_cookies}= Create Dictionary test=seleniumlibrary another=value
17+
Dictionaries Should Be Equal ${expected_cookies} ${cookies}
18+
1419
Get Cookie Value Set By Selenium
1520
${value} = Get Cookie Value another
1621
Should Be Equal ${value} value
@@ -66,6 +71,13 @@ Get Cookies When There Are None
6671
${cookies} = Get Cookies
6772
Should Be Equal ${cookies} ${EMPTY}
6873

74+
Get Cookies As Dict When There Are None
75+
[Tags] Known Issue Safari
76+
Delete All Cookies
77+
${cookies} = Get Cookies as_dict=True
78+
${expected_cookies}= Create Dictionary
79+
Dictionaries Should Be Equal ${expected_cookies} ${cookies}
80+
6981
Test Get Cookie Object Expiry
7082
${cookie} = Get Cookie another
7183
Should Be Equal As Integers ${cookie.expiry.year} 2027

src/SeleniumLibrary/keywords/cookie.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
from datetime import datetime
1818

1919
from robot.libraries.DateTime import convert_date
20+
from robot.utils import DotDict
2021

2122
from SeleniumLibrary.base import LibraryComponent, keyword
2223
from SeleniumLibrary.errors import CookieNotFound
23-
from SeleniumLibrary.utils import is_truthy, is_noney
24+
from SeleniumLibrary.utils import is_truthy, is_noney, is_falsy
2425

2526

2627
class CookieKeywords(LibraryComponent):
@@ -39,18 +40,31 @@ def delete_cookie(self, name):
3940
self.driver.delete_cookie(name)
4041

4142
@keyword
42-
def get_cookies(self):
43+
def get_cookies(self, as_dict=False):
4344
"""Returns all cookies of the current page.
4445
45-
The cookie information is returned as a single string in format
46-
``name1=value1; name2=value2; name3=value3``. It can be used,
47-
for example, for logging purposes or in headers when sending
48-
HTTP requests.
46+
If ``as_dict`` argument evaluates as false, see `Boolean arguments`
47+
for more details, then cookie information is returned as
48+
a single string in format ``name1=value1; name2=value2; name3=value3``.
49+
When ``as_dict`` argument evaluates as true, cookie information
50+
is returned as Robot Framework dictionary format. The string format
51+
can be used, for example, for logging purposes or in headers when
52+
sending HTTP requests. The dictionary format is helpful when
53+
the result can be passed to requests library's Create Session
54+
keyword's optional cookies parameter.
55+
56+
The `` as_dict`` argument is new in SeleniumLibrary 3.3
4957
"""
50-
pairs = []
51-
for cookie in self.driver.get_cookies():
52-
pairs.append(cookie['name'] + "=" + cookie['value'])
53-
return '; '.join(pairs)
58+
if is_falsy(as_dict):
59+
pairs = []
60+
for cookie in self.driver.get_cookies():
61+
pairs.append(cookie['name'] + "=" + cookie['value'])
62+
return '; '.join(pairs)
63+
else:
64+
pairs = DotDict()
65+
for cookie in self.driver.get_cookies():
66+
pairs[cookie['name']] = cookie['value']
67+
return pairs
5468

5569
@keyword
5670
def get_cookie_value(self, name):

0 commit comments

Comments
 (0)