Skip to content
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

Neyberson/app devtools #3

Merged
merged 15 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 19 additions & 52 deletions app/app_devtools.py
Original file line number Diff line number Diff line change
@@ -1,100 +1,67 @@
import devtools
import os
from pathlib import Path

## Create Variables
r1 = {
"id": 0,
"method": "Browser.setDownloadBehavior",
"params": {
"behavior": "allowAndName",
"downloadPath": "'+str(cwd)+'",
"downloadPath": "str(cwd)",
"eventsEnabled": True,
},
}
r2 = {"id": 1, "method": "Target.getTargets"}
r2 = {"method": "Target.getTargets"}
r3 = {
"id": 2,
"method": "Target.createTarget",
"params": {"url": "file://'+str(cwd / test.html)+'"},
"params": {"url": "file://str(cwd / test.html)"},
}
r4 = {
"id": 3,
"method": "Target.attachToTarget",
"params": {"flatten": True, "targetId": "+tId+"},
"params": {"flatten": True, "targetId": "tId"},
}
r5 = {"sessionId": "+sId+", "id": 0, "method": "Page.enable"}
r6 = {"sessionId": "+sId+", "id": 2, "method": "Page.reload"}
r5 = {"method": "Page.enable"}
r6 = {"method": "Page.reload"}
r7 = {
"sessionId": "+sId+",
"id": 3,
"method": "Browser.setDownloadBehavior",
"params": {
"behavior": "allow",
"downloadPath": "'+str(cwd)+'",
"downloadPath": "str(cwd)",
"eventsEnabled": True,
},
}
r8 = {
"sessionId": "+sId+",
"id": 13,
"method": "Page.setDownloadBehavior",
"params": {
"behavior": "allow",
"downloadPath": "'+str(cwd)+'",
"downloadPath": "str(cwd)",
"eventsEnabled": True,
},
}
r9 = {
"sessionId": "+sId+",
"id": 23,
"method": "Page.setInterceptFileChooserDialog",
"params": {"enabled": True},
}
r10 = {"sessionId": "+sId+", "id": 4, "method": "Runtime.enable"}
r10 = {"method": "Runtime.enable"}
r11 = {
"sessionId": "+sId+",
"id": 5,
"method": "Runtime.compileScript",
"params": {
"expression": "console.log(document.getElementsByTagName(\\'body\\')[0].innerHTML); 10; let goose = document.getElementById(\\'agoose2\\'); goose.download=\\'goose.jpg\\';goose.click();', 'sourceURL':'+str( cwd / 'test.html' )+', 'persistScript':true"
"expression": "console.log(document.getElementsByTagName(\\'body\\')[0].innerHTML); 10; let goose = document.getElementById(\\'agoose2\\'); goose.download=\\'goose.jpg\\';goose.click();', 'sourceURL':'str( cwd / 'test.html' )', 'persistScript':true"
},
}
r12 = {
"sessionId": "+sId+",
"id": 6,
"method": "Runtime.runScript",
"params": {"scriptId": "+scriptId+"},
"params": {"scriptId": "scriptId"},
}

list_r = [r1, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12]

list_r = [r1, r3, r4]
list_r_session_id = [r7, r8, r9, r11, r12]
## Create Connection
browser = devtools.Connection()
browser.list_tabs()

session_1 = devtools.Session()
## Print Commands
for r in list_r:
print(session_1.send_command(command=r["method"], params=r["params"]))

print(session_1.send_command(command=r2["method"], params=None))

print(
devtools.Session(str(r5["sessionId"])).send_command(
command=r5["method"], params=None
)
)
print(
devtools.Session(str(r6["sessionId"])).send_command(
command=r6["method"], params=None
)
)
print(
devtools.Session(str(r10["sessionId"])).send_command(
command=r10["method"], params=None
)
)

for r in list_r_session_id:
print(
devtools.Session(str(r["sessionId"])).send_command(
command=r["method"], params=r["params"]
browser.browser_session.send_command(
command=r["method"], params=r["params"] if "params" in r else None
)
)
3 changes: 3 additions & 0 deletions devtools/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ def list_tabs(self):
def close_tab(self, session_obj):
del self.tab_sessions[id(session_obj)]
print(f"The following session was deleted: {session_obj.session_id}")

def send_command(self, command, params=None, cb=None):
return self.browser_session.send_command(self, command, params, cb)
6 changes: 4 additions & 2 deletions devtools/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, parent, session_id=""):
self.message_id = 0
self.parent_connection = parent

def send_command(self, command, params, cb=None):
def send_command(self, command, params=None, cb=None):
if cb and not callable(cb):
raise TypeError("The arg that you use, is not able at cb")
if not isinstance(command, str):
Expand All @@ -25,9 +25,11 @@ def send_command(self, command, params, cb=None):
json_command = {
"id": self.message_id,
"method": command,
"params": params,
}

if params:
json_command["params"] = params

if self.session_id != "":
json_command["session_id"] = self.session_id

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ version = "0.0.1"
dev = [
"pytest",
]

[tool.ruff.lint]
ignore = ["E701"]