-
Notifications
You must be signed in to change notification settings - Fork 763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'expiry' to 'add cookie' keyword and add 'get cookie' keyword. #930
Changes from 5 commits
a242eae
3938b59
1e9b12a
7f556b6
52f4db4
56730c3
63f0209
6b57846
dfa3fab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
from SeleniumLibrary.base import LibraryComponent, keyword | ||
from SeleniumLibrary.utils import is_truthy | ||
from datetime import datetime | ||
|
||
|
||
class CookieKeywords(LibraryComponent): | ||
|
@@ -53,11 +54,24 @@ 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 is not None: | ||
cookie_information = CookieInformation(cookie['name'], cookie['value'], cookie['domain'], cookie['path'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow pep8 in line length recommendation in new code. Only the keyword examples can normally exceed the line length. |
||
cookie['expiry'], cookie['httpOnly'], cookie['secure']) | ||
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 must be in datetime format for UTC time zone (Example: '2027-09-28 16:21:35'). | ||
""" | ||
new_cookie = {'name': name, 'value': value} | ||
if is_truthy(path): | ||
|
@@ -67,4 +81,22 @@ 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 = datetime.strptime(expiry, '%Y-%m-%d %H:%M:%S') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to use Robot Framework DataTime library for better support different time formats. Example something like this could work:
Then the keyword documentation could say that |
||
expiry_epoch = (expiry_datetime - datetime(1970, 1, 1)).total_seconds() | ||
new_cookie['expiry'] = int(expiry_epoch) | ||
self.browser.add_cookie(new_cookie) | ||
|
||
|
||
class CookieInformation(object): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better ro write CookieInformation class like this:
And write line 64 and 65 like this: Because selenium cookie is a dictionary and at least in Windows There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First of all, such a generic def __init__(self, data):
self.__dict__.update(data) I'm don't think that is a good idea in this context, though:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then something like this could work better:
|
||
def __init__(self, name, value, domain, path, expiry, http, secure): | ||
self.name = name | ||
self.value = value | ||
self.domain = domain | ||
self.path = path | ||
self.expiry = expiry | ||
self.http = http | ||
self.secure = secure | ||
|
||
def __str__(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If actually is. Robot automatically logs return values from kws and without this method the message is useless. Should be discussed what info to show, though. Could only show name and value and keep the message short or return all the info. I'd probably keep
if needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not know that it is needed. Most of the time, I am interested about the |
||
return '{}={}'.format(self.name, self.value) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FF indeed seems to have a problem with cookies, please add a tag to the test Did you happen to try with other browsers, like IE, Edge or Safari, that do they suffer from the same problem? |
||
[Documentation] Get Cookie Value Set By Selenium | ||
[Tags] Known Issue Firefox | ||
${cookie_dict}= Get Cookie another | ||
${expiry} = Convert To Integer 1822148495 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One can not trust that expiry value is constant. Please set the cookie expiry first and then read it. |
||
should be equal ${cookie_dict.expiry} ${expiry} | ||
|
||
*** 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Write:
if cookie: