Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 0283611

Browse files
committed
chore(test): improve browser automator tests
1 parent 6c9a9a3 commit 0283611

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Used in tests
2+
DEBUG_TESTS=true
3+
DEBUG_CDP=true
4+
BAS_APP_NAME=PyBasFreeDev
5+
16
FINGERPRINT_KEY=
27
BRIGHTDATA_USERNAME=
38
BRIGHTDATA_PASSWORD=

tests/functional/browser_automator/test_browser_automator.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,71 @@ async def test_basic(self, browser_data: tuple[BrowserContext, DirectoryPath, in
4747

4848
# Use the automator to navigate to a specific webpage.
4949
await automator.page.goto("https://lumtest.com/echo.json")
50+
51+
import pytest
52+
53+
@pytest.mark.asyncio
54+
async def test_local_storage_with_cdp_and_js(self, browser_data: tuple[BrowserContext, DirectoryPath, int]) -> None:
55+
"""
56+
This test verifies the correct functionality of local storage interactions within a browser context using the
57+
BrowserAutomator. It navigates to specified URLs, performs local storage operations, and validates that these
58+
operations succeed. The test covers setting, retrieving, and clearing local storage items,
59+
as well as interacting with local storage through the Chrome DevTools Protocol.
60+
"""
61+
62+
# Extract the browser context, profile directory path, and remote debugging port from the provided tuple.
63+
_, profile_folder_path, remote_debugging_port = browser_data
64+
65+
# Initialize the browser profile with the specified directory path.
66+
browser_profile = BrowserProfile(profile_dir=profile_folder_path)
67+
68+
# Use the BrowserAutomator to establish a communication channel with the browser.
69+
async with BrowserAutomator(
70+
browser_profile=browser_profile, remote_debugging_port=remote_debugging_port
71+
) as automator:
72+
# Define a list of tuples containing URLs to visit and their associated domains.
73+
url_domain_pairs = [
74+
("https://playwright.dev/python/", "playwright.dev"),
75+
("https://lumtest.com/echo.json", "lumtest.com"),
76+
]
77+
78+
# Iterate through each URL and its associated domain.
79+
for url, domain in url_domain_pairs:
80+
# Navigate to the URL and wait until network activity is idle to ensure all resources are loaded.
81+
await automator.page.goto(url=url, wait_until="networkidle")
82+
83+
# Wait for the page to reach a 'loaded' state to ensure all DOM content is fully parsed.
84+
await automator.page.wait_for_load_state("domcontentloaded")
85+
86+
# Retrieve the security origin of the current frame for later use in Chrome DevTools Protocol commands.
87+
security_origin = await automator.page.evaluate("window.location.origin")
88+
assert security_origin is not None, "Failed to retrieve the security origin."
89+
90+
# Set a local storage item and verify its presence and value.
91+
await automator.page.evaluate(f"localStorage.setItem('key_{domain}', 'value_{domain}');")
92+
item = await automator.page.evaluate(f"localStorage.getItem('key_{domain}');")
93+
assert (
94+
item == f"value_{domain}"
95+
), f"Expected local storage item 'key_{domain}' to have value 'value_{domain}'."
96+
97+
# Clear all local storage items and verify the specified item is no longer present.
98+
await automator.page.evaluate("localStorage.clear();")
99+
item = await automator.page.evaluate(f"localStorage.getItem('key_{domain}');")
100+
assert item is None, "Expected local storage to be empty after clearing."
101+
102+
# Set a local storage item using Chrome DevTools Protocol and verify its presence and value.
103+
await automator.cdp_session.send(
104+
"DOMStorage.setDOMStorageItem",
105+
{
106+
"storageId": {
107+
"securityOrigin": security_origin,
108+
"isLocalStorage": True,
109+
},
110+
"key": f"new_key_{domain}",
111+
"value": f"new_value_{domain}",
112+
},
113+
)
114+
item = await automator.page.evaluate(f"localStorage.getItem('new_key_{domain}');")
115+
assert (
116+
item == f"new_value_{domain}"
117+
), f"Expected local storage item 'new_key_{domain}' to have value 'new_value_{domain}'."

0 commit comments

Comments
 (0)