Merge pull request #2660 from seleniumbase/fun-with-cookies

Add more cookie methods
This commit is contained in:
Michael Mintz 2024-04-03 21:50:24 -04:00 committed by GitHub
commit b80c1ce88e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 75 additions and 32 deletions

View File

@ -9,9 +9,9 @@
<h1>SeleniumBase</h1>
<p align="center"><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.github.io/cdn/img/sb_logo_10t.png" alt="SeleniumBase" title="SeleniumBase" width="266" /></a></p>
<p align="center"><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.github.io/cdn/img/mac_sb_logo_b.png" alt="SeleniumBase" title="SeleniumBase" width="440" /></a></p>
<p align="center" class="hero__title"><b>All-in-one Browser Automation Framework:<br />Web Crawling / Scraping / Testing / Reporting</b></p>
<h3 align="center" class="hero__title"><b>Web Automation and Testing with Python have evolved.</h3>
<p align="center"><a href="https://pypi.python.org/pypi/seleniumbase" target="_blank"><img src="https://img.shields.io/pypi/v/seleniumbase.svg?color=3399EE" alt="PyPI version" /></a> <a href="https://github.com/seleniumbase/SeleniumBase/releases" target="_blank"><img src="https://img.shields.io/github/v/release/seleniumbase/SeleniumBase.svg?color=22AAEE" alt="GitHub version" /></a> <a href="https://seleniumbase.io"><img src="https://img.shields.io/badge/docs-seleniumbase.io-11BBAA.svg" alt="SeleniumBase Docs" /></a> <a href="https://github.com/seleniumbase/SeleniumBase/actions" target="_blank"><img src="https://github.com/seleniumbase/SeleniumBase/workflows/CI%20build/badge.svg" alt="SeleniumBase GitHub Actions" /></a> <a href="https://gitter.im/seleniumbase/SeleniumBase" target="_blank"><img src="https://img.shields.io/gitter/room/seleniumbase/SeleniumBase.svg" alt="Gitter chat"/></a></p>
@ -57,7 +57,7 @@
📚 Learn from [**over 200 examples** in the **SeleniumBase/examples/**](https://github.com/seleniumbase/SeleniumBase/tree/master/examples) folder.
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/my_first_test.py">my_first_test.py</a>, which covers login, shopping, and checkout:</p>
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/my_first_test.py">my_first_test.py</a>, which tests login, shopping, and checkout:</p>
```bash
pytest my_first_test.py

View File

@ -336,6 +336,16 @@ self.delete_all_cookies()
self.delete_saved_cookies(name="cookies.txt")
self.get_saved_cookies(name="cookies.txt")
self.get_cookie(name)
self.get_cookies()
self.add_cookie(cookie_dict)
self.add_cookies(cookies)
self.wait_for_ready_state_complete(timeout=None)
self.wait_for_angularjs(timeout=None)
@ -596,6 +606,7 @@ self.get_local_storage_item(key)
self.remove_local_storage_item(key)
self.clear_local_storage()
# Duplicates: delete_local_storage()
self.get_local_storage_keys()
@ -608,6 +619,7 @@ self.get_session_storage_item(key)
self.remove_session_storage_item(key)
self.clear_session_storage()
# Duplicates: delete_session_storage()
self.get_session_storage_keys()

View File

@ -16,11 +16,11 @@ cairocffi==1.6.1
pathspec==0.12.1
Babel==2.14.0
paginate==0.5.6
lxml==5.1.1
lxml==5.2.1
pyquery==2.0.0
readtime==3.0.0
mkdocs==1.5.3
mkdocs-material==9.5.15
mkdocs-material==9.5.17
mkdocs-exclude-search==0.6.6
mkdocs-simple-hooks==0.1.5
mkdocs-material-extensions==1.3.1

View File

@ -70,9 +70,9 @@ rich==13.7.1
# ("pip install -r requirements.txt" also installs this, but "pip install -e ." won't.)
coverage==7.2.7;python_version<"3.8"
coverage==7.4.4;python_version>="3.8"
coverage>=7.4.4;python_version>="3.8"
pytest-cov==4.1.0;python_version<"3.8"
pytest-cov==5.0.0;python_version>="3.8"
pytest-cov>=5.0.0;python_version>="3.8"
flake8==5.0.4;python_version<"3.9"
flake8==7.0.0;python_version>="3.9"
mccabe==0.7.0

View File

@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.25.0"
__version__ = "4.25.1"

View File

@ -4308,25 +4308,8 @@ class BaseCase(unittest.TestCase):
def load_cookies(self, name="cookies.txt"):
"""Loads the page cookies from the "saved_cookies" folder."""
cookies = self.get_saved_cookies(name)
self.wait_for_ready_state_complete()
if name.endswith("/"):
raise Exception("Invalid filename for Cookies!")
if "/" in name:
name = name.split("/")[-1]
if "\\" in name:
name = name.split("\\")[-1]
if len(name) < 1:
raise Exception("Filename for Cookies is too short!")
if not name.endswith(".txt"):
name = name + ".txt"
folder = constants.SavedCookies.STORAGE_FOLDER
abs_path = os.path.abspath(".")
file_path = os.path.join(abs_path, folder)
cookies_file_path = os.path.join(file_path, name)
json_cookies = None
with open(cookies_file_path, "r") as f:
json_cookies = f.read().strip()
cookies = json.loads(json_cookies)
for cookie in cookies:
if "expiry" in cookie:
del cookie["expiry"]
@ -4363,6 +4346,46 @@ class BaseCase(unittest.TestCase):
if cookies_file_path.endswith(".txt"):
os.remove(cookies_file_path)
def get_saved_cookies(self, name="cookies.txt"):
"""Gets the page cookies from the "saved_cookies" folder."""
if name.endswith("/"):
raise Exception("Invalid filename for Cookies!")
if "/" in name:
name = name.split("/")[-1]
if "\\" in name:
name = name.split("\\")[-1]
if len(name) < 1:
raise Exception("Filename for Cookies is too short!")
if not name.endswith(".txt"):
name = name + ".txt"
folder = constants.SavedCookies.STORAGE_FOLDER
abs_path = os.path.abspath(".")
file_path = os.path.join(abs_path, folder)
cookies_file_path = os.path.join(file_path, name)
json_cookies = None
with open(cookies_file_path, "r") as f:
json_cookies = f.read().strip()
return json.loads(json_cookies)
def get_cookie(self, name):
return self.driver.get_cookie(name)
def get_cookies(self):
return self.driver.get_cookies()
def add_cookie(self, cookie_dict):
"""Usage examples:
self.add_cookie({'name': 'foo', 'value': 'bar'})
self.add_cookie({'name': 'foo', 'value': 'bar', 'path': '/'})
self.add_cookie({'name': 'foo', 'value': 'bar', 'secure': True})
self.add_cookie({'name': 'foo', 'value': 'bar', 'sameSite': 'Strict'})
"""
self.driver.add_cookie(cookie_dict)
def add_cookies(self, cookies):
for cookie_dict in cookies:
self.driver.add_cookie(cookie_dict)
def wait_for_ready_state_complete(self, timeout=None):
"""Waits for the "readyState" of the page to be "complete".
Returns True when the method completes."""
@ -8718,6 +8741,14 @@ class BaseCase(unittest.TestCase):
"""Same as self.delete_all_cookies()"""
self.delete_all_cookies()
def delete_local_storage(self):
"""Same as self.clear_local_storage()"""
self.clear_local_storage()
def delete_session_storage(self):
"""Same as clear_session_storage()"""
self.clear_session_storage()
def assert_no_broken_links(self, multithreaded=True):
"""Same as self.assert_no_404_errors()"""
self.assert_no_404_errors(multithreaded=multithreaded)

View File

@ -219,17 +219,17 @@ setup(
# Usage: pytest --alluredir=allure_results
# Serve: allure serve allure_results
"allure": [
'allure-pytest==2.13.4',
'allure-python-commons==2.13.4',
'allure-behave==2.13.4',
'allure-pytest>=2.13.5',
'allure-python-commons>=2.13.5',
'allure-behave>=2.13.5',
],
# pip install -e .[coverage]
# Usage: coverage run -m pytest; coverage html; coverage report
"coverage": [
'coverage==7.2.7;python_version<"3.8"',
'coverage==7.4.4;python_version>="3.8"',
'coverage>=7.4.4;python_version>="3.8"',
'pytest-cov==4.1.0;python_version<"3.8"',
'pytest-cov==5.0.0;python_version>="3.8"',
'pytest-cov>=5.0.0;python_version>="3.8"',
],
# pip install -e .[flake8]
# Usage: flake8
@ -263,7 +263,7 @@ setup(
# (An optional library for image-processing.)
"pillow": [
'Pillow==9.5.0;python_version<"3.8"',
'Pillow==10.2.0;python_version>="3.8"',
'Pillow>=10.3.0;python_version>="3.8"',
],
# pip install -e .[pip-system-certs]
# (If you see [SSL: CERTIFICATE_VERIFY_FAILED], then get this.)