Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions atest/acceptance/keywords/cookies.robot
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Get Cookies
Should Match Regexp ${cookies}
... ^(test=seleniumlibrary; another=value)|(another=value; test=seleniumlibrary)$

Get Cookies As Dict
${cookies}= Get Cookies as_dict=True
${expected_cookies}= Create Dictionary test=seleniumlibrary another=value
Dictionaries Should Be Equal ${expected_cookies} ${cookies}

Get Cookie Value Set By Selenium
${value} = Get Cookie Value another
Should Be Equal ${value} value
Expand Down Expand Up @@ -66,6 +71,13 @@ Get Cookies When There Are None
${cookies} = Get Cookies
Should Be Equal ${cookies} ${EMPTY}

Get Cookies As Dict When There Are None
[Tags] Known Issue Safari
Delete All Cookies
${cookies} = Get Cookies as_dict=True
${expected_cookies}= Create Dictionary
Dictionaries Should Be Equal ${expected_cookies} ${cookies}

Test Get Cookie Object Expiry
${cookie} = Get Cookie another
Should Be Equal As Integers ${cookie.expiry.year} 2027
Expand Down
34 changes: 24 additions & 10 deletions src/SeleniumLibrary/keywords/cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
from datetime import datetime

from robot.libraries.DateTime import convert_date
from robot.utils import DotDict

from SeleniumLibrary.base import LibraryComponent, keyword
from SeleniumLibrary.errors import CookieNotFound
from SeleniumLibrary.utils import is_truthy, is_noney
from SeleniumLibrary.utils import is_truthy, is_noney, is_falsy


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

@keyword
def get_cookies(self):
def get_cookies(self, as_dict=False):
"""Returns all cookies of the current page.

The cookie information is returned as a single string in format
``name1=value1; name2=value2; name3=value3``. It can be used,
for example, for logging purposes or in headers when sending
HTTP requests.
If ``as_dict`` argument evaluates as false, see `Boolean arguments`
for more details, then cookie information is returned as
a single string in format ``name1=value1; name2=value2; name3=value3``.
When ``as_dict`` argument evaluates as true, cookie information
is returned as Robot Framework dictionary format. The string format
can be used, for example, for logging purposes or in headers when
sending HTTP requests. The dictionary format is helpful when
the result can be passed to requests library's Create Session
keyword's optional cookies parameter.

The `` as_dict`` argument is new in SeleniumLibrary 3.3
"""
pairs = []
for cookie in self.driver.get_cookies():
pairs.append(cookie['name'] + "=" + cookie['value'])
return '; '.join(pairs)
if is_falsy(as_dict):
pairs = []
for cookie in self.driver.get_cookies():
pairs.append(cookie['name'] + "=" + cookie['value'])
return '; '.join(pairs)
else:
pairs = DotDict()
for cookie in self.driver.get_cookies():
pairs[cookie['name']] = cookie['value']
return pairs

@keyword
def get_cookie_value(self, name):
Expand Down