Skip to content

Commit

Permalink
update debugger demo
Browse files Browse the repository at this point in the history
  • Loading branch information
lidong committed Nov 7, 2023
1 parent f72b234 commit d3abd89
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
1 change: 0 additions & 1 deletion examples_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ def test_all():
time.sleep(1)
finally:
loop.close()
AsyncChromeDaemon.clear_dir(AsyncChromeDaemon.DEFAULT_USER_DIR_PATH)


if __name__ == "__main__":
Expand Down
60 changes: 30 additions & 30 deletions examples_debug.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
import time

from ichrome import AsyncTab
from ichrome.debugger import get_a_tab, network_sniffer, launch
from ichrome.debugger import Chrome, Daemon

# There are 3 ways to create a daemon
# 1. get_a_tab: auto find the existing Chrome (launched before like python -m ichrome), or create a new daemon
# 2. daemon = Daemon()
# 3. daemon = launch()

# type hints for autocomplete in the IDE
daemon = launch()
tab: AsyncTab = get_a_tab()


def test_set_ua():
def test_set_ua(tab: AsyncTab):
# check the UA changed
tab.set_ua("No UA.")
tab.set_ua("Custom UA.")
tab.set_url("http://httpbin.org/user-agent")
assert "No UA." in tab.html, tab.html
assert "Custom UA." in tab.html, tab.html


def test_mouse_keyboard():
def test_mouse_keyboard(tab: AsyncTab):
# click the input element and send some string
tab.set_url("http://httpbin.org/forms/post", timeout=5)
rect: dict = tab.get_bounding_client_rect('[type="email"]')
tab.mouse_click(rect["left"], rect["top"], count=1)
tab.keyboard_send(string="[email protected]")
# click the submit button
tab.click("button")
tab.wait_loading(2)
tab.wait_loading(3)
assert '"custemail": "[email protected]"' in tab.html, tab.html


def test_js():
tab.set_url("https://postman-echo.com/ip")
tag = tab.querySelectorAll("html")[0]
print(tag, tag.text)
img_b64 = tab.screenshot_element("html")
def test_js(tab: AsyncTab):
tab.set_url("https://help.tom.com/")
tag = tab.querySelectorAll(".pr_tit")[0]
assert tag and tag.text
from html import escape

tab.run_js_snippets("add_tip", f"got the .pr_tit tag[0]: {escape(tag.outerHTML)}")
time.sleep(3)
tab.run_js_snippets("clear_tip")
img_b64 = tab.screenshot_element("#contact", captureBeyondViewport=True)
tab.set_html(
f'<h1>Here is the screenshot image by css selector ".shader_left"</h1><img src="data:image/png;base64, {img_b64}" alt="Red dot" /><h1>Test js finished</h1>'
f'<h1>Here is the screenshot image by css selector "#contact"</h1><img style="width:80%;" src="data:image/png;base64, {img_b64}" alt="Red dot" /><h1>Test js finished after 5 secs</h1>'
)
tab.alert('Here is the screenshot image by css selector "#contact"')
for i in range(5):
tab.run_js_snippets("add_tip", f"{5 - i}s")
time.sleep(1)


def main():
test_set_ua()
test_mouse_keyboard()
test_js()
for _ in range(3):
tab.set_html(
f"<h1>Start network_sniffer in %s seconds. Refresh this page and read network logs in terminal.</h1>"
% (3 - _)
)
time.sleep(1)
network_sniffer()
with Daemon(
user_data_dir="./debug_cache", clear_after_shutdown=True, headless=0
) as daemon:
with Chrome(host=daemon.host, port=daemon.port) as chrome:
tab = chrome.get_tab()
test_set_ua(tab)
test_mouse_keyboard(tab)
test_js(tab)


if __name__ == "__main__":
try:
main()
finally:
daemon.stop_running()
main()
5 changes: 3 additions & 2 deletions ichrome/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from functools import wraps
from inspect import isawaitable
from typing import Set

from torequests.utils import quote_plus
from urllib.parse import quote_plus

from .async_utils import AsyncChrome, AsyncTab
from .daemon import AsyncChromeDaemon, ChromeDaemon
Expand Down Expand Up @@ -115,6 +114,7 @@ def __init__(
proc_check_interval=5,
on_startup=None,
on_shutdown=quit_while_daemon_missing,
**_kwargs,
):
self._self = AsyncChromeDaemon(
chrome_path=chrome_path,
Expand All @@ -135,6 +135,7 @@ def __init__(
proc_check_interval=proc_check_interval,
on_startup=on_startup,
on_shutdown=on_shutdown,
**_kwargs,
)
self.start_running()

Expand Down

0 comments on commit d3abd89

Please sign in to comment.