Skip to content

Commit

Permalink
Fixes cookie object and Get Cookie keyword documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
aaltat committed Oct 5, 2017
1 parent 20108dd commit 8e589ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 34 deletions.
61 changes: 38 additions & 23 deletions src/SeleniumLibrary/keywords/cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def delete_all_cookies(self):

@keyword
def delete_cookie(self, name):
"""Deletes cookie matching `name`.
"""Deletes cookie matching ``name``.
If the cookie is not found, nothing happens.
"""
Expand All @@ -44,9 +44,9 @@ def get_cookies(self):

@keyword
def get_cookie_value(self, name):
"""Returns value of cookie found with `name`.
"""Returns value of cookie found with ``name``.
If no cookie is found with `name`, this keyword fails.
If no cookie is found with ``name``, this keyword fails.
"""
cookie = self.browser.get_cookie(name)
if cookie is not None:
Expand All @@ -55,24 +55,40 @@ def get_cookie_value(self, name):

@keyword
def get_cookie(self, name):
"""Returns the cookie found with `name` in extended variable format.
If no cookie is found with `name`, this keyword fails.
"""Returns a cookie object found with ``name``.
The cookie object contains details about the cookie.
Attributes available in the object are documented in the table below.
| = Attribute = | = Explanation = |
| domain | Specifies hosts to which the cookie will be sent |
| expiry | The maximum lifetime of the cookie as EPOCH |
| httpOnly | HttpOnly cookie cannot be accessed by client-side APIs |
| name | The name of a cookie |
| path | Indicates a URL path, usually / |
| secure | Cookie will be send only by using secure connection |
| value | Value of the cookie |
| full_info | All the above attributes joined as string |
If no cookie is found with ``name``, this keyword fails.
"""
cookie = self.browser.get_cookie(name)
if cookie:
cookie_information = CookieInformation(cookie)
return cookie_information
return CookieInformation(
cookie.get('domain', None), cookie.get('expiry', None),
cookie.get('httpOnly', None), cookie['name'],
cookie.get('path', None), cookie.get('secure', None),
cookie['value'])
raise ValueError("Cookie with name %s not found." % name)

@keyword
def add_cookie(self, name, value, path=None, domain=None, secure=None,
expiry=None):
"""Adds a cookie to your current session.
"name" and "value" are required, "path", "domain", "secure" and
"expiry" are optional. Expiry supports the same formats as
the DateTime library.
``name`` and ``value`` are required, ``path``, ``domain``, ``secure``
and ``expiry`` are optional. Expiry supports the same formats as
the DateTime library and is converted to EPOCH which is supported
by the Selenium.
"""
new_cookie = {'name': name, 'value': value}
if is_truthy(path):
Expand All @@ -89,20 +105,19 @@ def add_cookie(self, name, value, path=None, domain=None, secure=None,


class CookieInformation(object):
def __init__(self, cookie):
self.domain = cookie['domain'] if 'domain' in cookie else None
self.expiry = cookie['expiry'] if 'expiry' in cookie else None
self.httpOnly = cookie['httpOnly'] if 'httpOnly' in cookie else None
self.name = cookie['name'] if 'name' in cookie else None
self.path = cookie['path'] if 'path' in cookie else None
self.secure = cookie['secure'] if 'secure' in cookie else None
self.value = cookie['value'] if 'value' in cookie else None
def __init__(self, domain, expiry, httpOnly, name, path, secure, value):
self.domain = domain
self.expiry = expiry
self.httpOnly = httpOnly
self.name = name
self.path = path
self.secure = secure
self.value = value

@property
def full_info(self):
cookie_info = ', '.join("{}={}".format(key, value) for
(key, value) in self.__dict__.items())
return cookie_info
return ', '.join("{}={}".format(key, value) for
(key, value) in self.__dict__.items())

def __str__(self):
return '{}={}'.format(self.name, self.value)
return self.full_info
32 changes: 21 additions & 11 deletions test/acceptance/keywords/cookies.robot
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,67 @@ Resource ../resource.robot

*** Test Cases ***
Get Cookies
[Documentation] Get Cookies
${cookies}= Get Cookies
Should Match Regexp ${cookies}
... ^(test=seleniumlibrary; another=value)|(another=value; test=seleniumlibrary)$

Get Cookie Value Set By Selenium
[Documentation] Get Cookie Value Set By Selenium
${value}= Get Cookie Value another
Should Be Equal ${value} value

Get Cookie Value Set By App
[Documentation] Get Cookie Value Set By App
Click Link Add cookie
${cookie}= Get Cookie Value spam
Should Be Equal ${cookie} eggs

App Sees Cookie Set By Selenium
[Documentation] App Sees Cookie Set By Selenium
Add Cookie setbyselenium true
Click Link Check cookie
Element Text Should Be output Cookie found with value 'true'!

Delete Cookie
[Documentation] Delete Cookie
[Tags] Known Issue Safari
Delete Cookie test
${cookies} = Get Cookies
Should Be Equal ${cookies} another=value

Non-existent Cookie
[Documentation] Non-existent Cookie
Run Keyword And Expect Error ValueError: Cookie with name missing not found.
... Get Cookie Value missing

Get Cookies When There Are None
[Documentation] Get Cookies When There Are None
[Tags] Known Issue Safari
Delete All Cookies
${cookies}= Get Cookies
Should Be Equal ${cookies} ${EMPTY}

Get Cookie Expiry Set By Selenium
[Documentation] Get Cookie Expiry Set By Selenium
[Tags] Known Issue Firefox
${cookie}= Get Cookie another
${date}= Convert Date 2027-09-28 16:21:35 epoch
${cookie} = Get Cookie another
${date} = Convert Date 2027-09-28 16:21:35 epoch
Should Be Equal As Integers ${cookie.expiry} ${date}
[Teardown] Delete All Cookies

Test Get Cookie Object
${cookie} = Get Cookie another
${date} = Convert Date 2027-09-28 16:21:35 epoch
Should Be Equal ${cookie.domain} localhost
Should Be Equal ${cookie.expiry} ${date}
Should Be Equal ${cookie.httpOnly} ${False}
Should Be Equal ${cookie.name} another
Should Be Equal ${cookie.path} /
Should Be Equal ${cookie.secure} ${False}
Should Be Equal ${cookie.value} value
Should Contain ${cookie.full_info} domain=localhost
Should Contain ${cookie.full_info} secure=False
Should Contain ${cookie.full_info} value=value
Should Contain ${cookie.full_info} expiry=1822137695
Should Contain ${cookie.full_info} path=/
Should Contain ${cookie.full_info} httpOnly=False
Should Contain ${cookie.full_info} name=another

*** Keyword ***
Add Cookies
[Documentation] Add Cookies
Delete All Cookies
Add Cookie test seleniumlibrary
Add Cookie another value expiry=2027-09-28 16:21:35

0 comments on commit 8e589ac

Please sign in to comment.