Skip to content

Commit

Permalink
Merge pull request #3240 from seleniumbase/cdp-mode-patch-6
Browse files Browse the repository at this point in the history
CDP Mode - Patch 6
  • Loading branch information
mdmintz authored Nov 1, 2024
2 parents d0625e8 + ab3198a commit 57df468
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 15 deletions.
5 changes: 5 additions & 0 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ sb.cdp.find_all(selector)
sb.cdp.find_elements_by_text(text, tag_name=None)
sb.cdp.select(selector)
sb.cdp.select_all(selector)
sb.cdp.find_elements(selector)
sb.cdp.click_link(link_text)
sb.cdp.tile_windows(windows=None, max_columns=0)
sb.cdp.get_all_cookies(*args, **kwargs)
Expand Down Expand Up @@ -290,13 +291,17 @@ sb.cdp.get_element_attributes(selector)
sb.cdp.get_element_html(selector)
sb.cdp.set_locale(locale)
sb.cdp.set_attributes(selector, attribute, value)
sb.cdp.gui_click_x_y(x, y)
sb.cdp.gui_click_element(selector)
sb.cdp.internalize_links()
sb.cdp.is_element_present(selector)
sb.cdp.is_element_visible(selector)
sb.cdp.assert_element(selector)
sb.cdp.assert_element_present(selector)
sb.cdp.assert_text(text, selector="html")
sb.cdp.assert_exact_text(text, selector="html")
sb.cdp.scroll_down(amount=25)
sb.cdp.scroll_up(amount=25)
sb.cdp.save_screenshot(name, folder=None, selector=None)
```

Expand Down
42 changes: 42 additions & 0 deletions examples/cdp_mode/raw_easyjet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
url = "https://www.easyjet.com/en/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.cdp.click_if_visible('button#ensRejectAll')
sb.sleep(1.2)
sb.cdp.click('input[name="from"]')
sb.sleep(1.2)
sb.cdp.type('input[name="from"]', "London")
sb.sleep(1.2)
sb.cdp.click('span[data-testid="airport-name"]')
sb.sleep(1.2)
sb.cdp.type('input[name="to"]', "Venice")
sb.sleep(1.2)
sb.cdp.click('span[data-testid="airport-name"]')
sb.sleep(1.2)
sb.cdp.click('input[name="when"]')
sb.sleep(1.2)
sb.cdp.click('[data-testid="month"] button[aria-disabled="false"]')
sb.sleep(1.2)
sb.cdp.click('[data-testid="month"]:last-of-type [aria-disabled="false"]')
sb.sleep(1.2)
sb.cdp.click('button[data-testid="submit"]')
sb.sleep(3.5)
sb.connect()
sb.sleep(0.5)
if "easyjet.com" not in sb.get_current_url():
sb.driver.close()
sb.switch_to_newest_window()
days = sb.find_elements("div.flight-grid-day")
for day in days:
print("**** " + " ".join(day.text.split("\n")[0:2]) + " ****")
fares = day.find_elements("css selector", "button.selectable")
if not fares:
print("No flights today!")
for fare in fares:
info = fare.text
info = info.replace("LOWEST FARE\n", "")
info = info.replace("\n", " ")
print(info)
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_req_async.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Using CDP.fetch.RequestPaused to filter content in real time."""
"""Using CDP.fetch.RequestPaused to filter content in real-time."""
import asyncio
import mycdp
from seleniumbase import decorators
Expand Down
2 changes: 1 addition & 1 deletion examples/cdp_mode/raw_req_sb.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Using CDP.fetch.RequestPaused to filter content in real time."""
"""Using CDP.fetch.RequestPaused to filter content in real-time."""
import mycdp
from seleniumbase import SB

Expand Down
34 changes: 34 additions & 0 deletions examples/cdp_mode/raw_res_sb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Using CDP.network.ResponseReceived and CDP.network.RequestWillBeSent."""
import colorama
import mycdp
import sys
from seleniumbase import SB

c1 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
cr = colorama.Style.RESET_ALL
if "linux" in sys.platform:
c1 = c2 = cr = ""


async def send_handler(event: mycdp.network.RequestWillBeSent):
r = event.request
s = f"{r.method} {r.url}"
for k, v in r.headers.items():
s += f"\n\t{k} : {v}"
print(c1 + "*** ==> RequestWillBeSent <== ***" + cr)
print(s)


async def receive_handler(event: mycdp.network.ResponseReceived):
print(c2 + "*** ==> ResponseReceived <== ***" + cr)
print(event.response)


with SB(uc=True, test=True, locale_code="en") as sb:
sb.activate_cdp_mode("about:blank")
sb.cdp.add_handler(mycdp.network.RequestWillBeSent, send_handler)
sb.cdp.add_handler(mycdp.network.ResponseReceived, receive_handler)
url = "https://seleniumbase.io/apps/calculator"
sb.cdp.open(url)
sb.sleep(1)
31 changes: 31 additions & 0 deletions examples/cdp_mode/raw_walmart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from seleniumbase import SB

with SB(uc=True, test=True, locale_code="en") as sb:
url = "https://www.walmart.com/"
sb.activate_cdp_mode(url)
sb.sleep(2.5)
sb.cdp.mouse_click('input[aria-label="Search"]')
sb.sleep(1.2)
search = "Settlers of Catan Board Game"
required_text = "Catan"
sb.cdp.press_keys('input[aria-label="Search"]', search + "\n")
sb.sleep(3.8)
items = sb.cdp.find_elements('div[data-testid="list-view"]')
print('*** Walmart Search for "%s":' % search)
print(' (Results must contain "%s".)' % required_text)
for item in items:
if required_text in item.text:
description = item.querySelector(
'[data-automation-id="product-price"] + span'
)
if description:
print("* " + description.text)
price = item.querySelector(
'[data-automation-id="product-price"]'
)
if price:
price_text = price.text
price_text = price_text.split("current price Now ")[-1]
price_text = price_text.split("current price ")[-1]
price_text = price_text.split(" ")[0]
print(" (" + price_text + ")")
1 change: 1 addition & 0 deletions examples/raw_pixelscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

with SB(uc=True, incognito=True, test=True) as sb:
sb.driver.uc_open_with_reconnect("https://pixelscan.net/", 10)
sb.remove_elements("div.banner") # Remove the banner
sb.remove_elements("jdiv") # Remove chat widgets
no_automation_detected = "No automation framework detected"
sb.assert_text(no_automation_detected, "pxlscn-bot-detection")
Expand Down
4 changes: 2 additions & 2 deletions help_docs/customizing_test_runs.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pytest test_suite.py

<h3><img src="https://seleniumbase.github.io/img/green_logo.png" title="SeleniumBase" width="32" /> Demo Mode:</h3>

🔵 If any test is moving too fast for your eyes to see what's going on, you can run it in **Demo Mode** by adding ``--demo`` on the command line, which pauses the browser briefly between actions, highlights page elements being acted on, and lets you know what test assertions are happening in real time:
🔵 If any test is moving too fast for your eyes to see what's going on, you can run it in **Demo Mode** by adding ``--demo`` on the command line, which pauses the browser briefly between actions, highlights page elements being acted on, and lets you know what test assertions are happening in real-time:

```bash
pytest my_first_test.py --demo
Expand Down Expand Up @@ -335,7 +335,7 @@ class Test:
pytest --headless -n8 --dashboard --html=report.html -v --rs --crumbs
```

The above not only runs tests in parallel processes, but it also tells tests in the same process to share the same browser session, runs the tests in headless mode, displays the full name of each test on a separate line, creates a realtime dashboard of the test results, and creates a full report after all tests complete.
The above not only runs tests in parallel processes, but it also tells tests in the same process to share the same browser session, runs the tests in headless mode, displays the full name of each test on a separate line, creates a real-time dashboard of the test results, and creates a full report after all tests complete.

--------

Expand Down
8 changes: 4 additions & 4 deletions mkdocs_build/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# mkdocs dependencies for generating the seleniumbase.io website
# Minimum Python version: 3.8 (for generating docs only)
# Minimum Python version: 3.9 (for generating docs only)

regex>=2024.9.11
pymdown-extensions>=10.11.2
pymdown-extensions>=10.12
pipdeptree>=2.23.4
python-dateutil>=2.8.2
Markdown==3.7
Expand All @@ -11,7 +11,7 @@ MarkupSafe==3.0.2
Jinja2==3.1.4
click==8.1.7
ghp-import==2.1.0
watchdog==5.0.3
watchdog==6.0.0
cairocffi==1.7.1
pathspec==0.12.1
Babel==2.16.0
Expand All @@ -20,7 +20,7 @@ lxml==5.3.0
pyquery==2.0.1
readtime==3.0.0
mkdocs==1.6.1
mkdocs-material==9.5.42
mkdocs-material==9.5.43
mkdocs-exclude-search==0.6.6
mkdocs-simple-hooks==0.1.5
mkdocs-material-extensions==1.3.1
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trio==0.27.0
trio-websocket==0.11.1
wsproto==1.2.0
websocket-client==1.8.0
selenium==4.25.0
selenium==4.26.1
cssselect==1.2.0
sortedcontainers==2.4.0
execnet==2.1.1
Expand Down Expand Up @@ -64,7 +64,8 @@ rich==13.9.3

coverage>=7.6.1;python_version<"3.9"
coverage>=7.6.4;python_version>="3.9"
pytest-cov>=5.0.0
pytest-cov>=5.0.0;python_version<"3.9"
pytest-cov>=6.0.0;python_version>="3.9"
flake8==5.0.4;python_version<"3.9"
flake8==7.1.1;python_version>="3.9"
mccabe==0.7.0
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.32.5"
__version__ = "4.32.6"
6 changes: 5 additions & 1 deletion seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ def uc_open_with_cdp_mode(driver, url=None):
cdp.find_elements_by_text = CDPM.find_elements_by_text
cdp.select = CDPM.select
cdp.select_all = CDPM.select_all
cdp.find_elements = CDPM.find_elements
cdp.click_link = CDPM.click_link
cdp.tile_windows = CDPM.tile_windows
cdp.get_all_cookies = CDPM.get_all_cookies
Expand Down Expand Up @@ -619,6 +620,8 @@ def uc_open_with_cdp_mode(driver, url=None):
cdp.reset_window_size = CDPM.reset_window_size
cdp.set_locale = CDPM.set_locale
cdp.set_attributes = CDPM.set_attributes
cdp.gui_click_x_y = CDPM.gui_click_x_y
cdp.gui_click_element = CDPM.gui_click_element
cdp.internalize_links = CDPM.internalize_links
cdp.get_window = CDPM.get_window
cdp.get_element_attributes = CDPM.get_element_attributes
Expand Down Expand Up @@ -655,6 +658,8 @@ def uc_open_with_cdp_mode(driver, url=None):
cdp.assert_element_visible = CDPM.assert_element
cdp.assert_text = CDPM.assert_text
cdp.assert_exact_text = CDPM.assert_exact_text
cdp.scroll_down = CDPM.scroll_down
cdp.scroll_up = CDPM.scroll_up
cdp.save_screenshot = CDPM.save_screenshot
cdp.page = page # async world
cdp.driver = driver.cdp_base # async world
Expand Down Expand Up @@ -2218,7 +2223,6 @@ def _set_chrome_options(
)
):
chrome_options.add_argument("--no-pings")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--homepage=chrome://version/")
chrome_options.add_argument("--animation-duration-scale=0")
chrome_options.add_argument("--wm-window-animations-disabled")
Expand Down
Loading

0 comments on commit 57df468

Please sign in to comment.