I’m trying to create tkinter app with commands to control automated script replay on chrome.
Usecase:
- .bat launches autoamted replay where I get sessionid and pass it to debugger
- connect_debugger_async connects to the current chrome session based on sessionid and I see ‘debugger attached’ message in cmd prompt console.
- Similarly when I invoke pause_debugger action from python, I see command reaching command prompt and pause action does happen on debugger where script execution is paused.
- But, when disconnect_debugger is invoked, I see command reaching command prompt but it is not complete.
Expected: On disconnect_debugger, I expect cursor should exit from command prompt and point to current working directory
Example: C:/Temp
Actual: On disconnect_debugger, cursor is not exiting from command prompt and exits only when i close the tkinter app.
Below is my code snippet:
import subprocess
import asyncio
import websockets
import json
import tkinter as tk
from tkinter import messagebox
class DebuggerControlApp(tk.Tk):
def __init__(self):
super().__init__()
self.title("Debugger Control")
self.geometry("300x200")
self.launch_button = tk.Button(self, text="Launch Browser", command=self.launch_browser)
self.launch_button.pack(pady=10)
self.connect_button = tk.Button(self, text="Connect Debugger", command=self.connect_debugger)
self.connect_button.pack(pady=10)
self.pause_button = tk.Button(self, text="Pause Debugger", command=self.pause_debugger)
self.pause_button.pack(pady=10)
self.disconnect_button = tk.Button(self, text="Disconnect Debugger", command=self.disconnect_debugger)
self.disconnect_button.pack(pady=10)
def launch_browser(self):
subprocess.Popen(["launch_browser.bat"], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
messagebox.showinfo("Info", "Browser Launched")
async def send_debugger_command(self, command):
async with websockets.connect('ws://localhost:9222/<session_id>') as websocket:
await websocket.send(json.dumps(command))
response = await websocket.recv()
return json.loads(response)
async def connect_debugger_async(self):
command = {
"id": 1,
"method": "Debugger.enable"
}
response = await self.send_debugger_command(command)
messagebox.showinfo("Info", f"Connected to debugger: {response}")
def connect_debugger(self):
asyncio.run(self.connect_debugger_async())
async def pause_debugger_async(self):
command = {
"id": 2,
"method": "Debugger.pause"
}
response = await self.send_debugger_command(command)
messagebox.showinfo("Info", f"Debugger paused: {response}")
def pause_debugger(self):
asyncio.run(self.pause_debugger_async())
async def disconnect_debugger_async(self):
command = {
"id": 3,
"method": "Debugger.disable"
}
response = await self.send_debugger_command(command)
messagebox.showinfo("Info", f"Disconnected from debugger: {response}")
def disconnect_debugger(self):
asyncio.run(self.disconnect_debugger_async())
if __name__ == "__main__":
app = DebuggerControlApp()
app.mainloop()
Note: If I open subprocess in a new console then communicating between two command prompts was not possible, i.e., cmd prompt which launched py and cmd prompt which launched script execution, how to attach and control actions happening on second cmd?