diff --git a/atest/acceptance/keywords/cookies.robot b/atest/acceptance/keywords/cookies.robot index 4cfa9a476..db723307f 100644 --- a/atest/acceptance/keywords/cookies.robot +++ b/atest/acceptance/keywords/cookies.robot @@ -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 @@ -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 diff --git a/src/SeleniumLibrary/keywords/cookie.py b/src/SeleniumLibrary/keywords/cookie.py index 892110da9..1f85948f6 100644 --- a/src/SeleniumLibrary/keywords/cookie.py +++ b/src/SeleniumLibrary/keywords/cookie.py @@ -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): @@ -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):