diff --git a/CHANGES.rst b/CHANGES.rst index ad8b57726..755ec8228 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,8 @@ Release Notes - Added 'Get Locations' keyword [thaffenden] - Fix getting window information and switching windows on browsers that do not support javascript +- Added 'Get Cookie' keyword [wappowers] +- Added 'expiry' as value that can be set with 'Add Cookie' keyword [wappowers] 1.8.0 ----- diff --git a/src/SeleniumLibrary/keywords/cookie.py b/src/SeleniumLibrary/keywords/cookie.py index eeff2de7a..b5531e0a5 100644 --- a/src/SeleniumLibrary/keywords/cookie.py +++ b/src/SeleniumLibrary/keywords/cookie.py @@ -16,6 +16,7 @@ from SeleniumLibrary.base import LibraryComponent, keyword from SeleniumLibrary.utils import is_truthy +from robot.libraries.DateTime import convert_date class CookieKeywords(LibraryComponent): @@ -53,11 +54,25 @@ def get_cookie_value(self, name): raise ValueError("Cookie with name %s not found." % name) @keyword - def add_cookie(self, name, value, path=None, domain=None, secure=None): + 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. + """ + cookie = self.browser.get_cookie(name) + if cookie: + cookie_information = CookieInformation(cookie) + return cookie_information + 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" and "secure" are - optional + "name" and "value" are required, "path", "domain", "secure" and + "expiry" are optional. Expiry supports the same formats as + the DateTime library. """ new_cookie = {'name': name, 'value': value} if is_truthy(path): @@ -67,4 +82,27 @@ def add_cookie(self, name, value, path=None, domain=None, secure=None): # Secure should be True or False if is_truthy(secure): new_cookie['secure'] = secure + if is_truthy(expiry): + expiry_datetime = int(convert_date(expiry, result_format='epoch')) + new_cookie['expiry'] = expiry_datetime self.browser.add_cookie(new_cookie) + + +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 + + @property + def full_info(self): + cookie_info = ', '.join("{}={}".format(key, value) for + (key, value) in self.__dict__.items()) + return cookie_info + + def __str__(self): + return '{}={}'.format(self.name, self.value) diff --git a/test/acceptance/keywords/cookies.robot b/test/acceptance/keywords/cookies.robot index e3fb40dcc..2992ca11f 100644 --- a/test/acceptance/keywords/cookies.robot +++ b/test/acceptance/keywords/cookies.robot @@ -5,6 +5,7 @@ Suite Teardown Delete All Cookies Test Setup Add Cookies Resource ../resource.robot + *** Test Cases *** Get Cookies [Documentation] Get Cookies @@ -48,9 +49,16 @@ Get Cookies When There Are None ${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 + Should Be Equal As Integers ${cookie.expiry} ${date} + *** Keyword *** Add Cookies [Documentation] Add Cookies Delete All Cookies Add Cookie test seleniumlibrary - Add Cookie another value + Add Cookie another value expiry=2027-09-28 16:21:35 diff --git a/test/acceptance/resource.robot b/test/acceptance/resource.robot index ce76d1ac2..93364e0e1 100644 --- a/test/acceptance/resource.robot +++ b/test/acceptance/resource.robot @@ -2,6 +2,7 @@ Library SeleniumLibrary run_on_failure=Nothing implicit_wait=0 Library Collections Library OperatingSystem +Library DateTime *** Variable *** ${SERVER}= localhost:7000