1717from datetime import datetime
1818
1919from robot .libraries .DateTime import convert_date
20+ from robot .utils import DotDict
2021
2122from SeleniumLibrary .base import LibraryComponent , keyword
2223from 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
2627class 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