diff --git a/devtools/browser.py b/devtools/browser.py index cac14128..38314614 100644 --- a/devtools/browser.py +++ b/devtools/browser.py @@ -126,7 +126,7 @@ def __init__( # Initializing super().__init__("0", self) # NOTE: 0 can't really be used externally - self.add_session(Session(self, "")) + self._add_session(Session(self, "")) if not self.loop: self._open() @@ -407,7 +407,9 @@ async def create_tab(self, url="", width=None, height=None): response = await self.browser.send_command("Target.createTarget", params=params) if "error" in response: - raise RuntimeError("Could not create tab") from Exception(response["error"]) + raise RuntimeError("Could not create tab") from DevtoolsProtocolError( + response + ) target_id = response["result"]["targetId"] new_tab = Tab(target_id, self) self._add_tab(new_tab) @@ -429,7 +431,9 @@ async def close_tab(self, target_id): ) self._remove_tab(target_id) if "error" in response: - raise RuntimeError("Could not close tab") from Exception(response["error"]) + raise RuntimeError("Could not close tab") from DevtoolsProtocolError( + response + ) return response async def create_session(self): @@ -442,12 +446,12 @@ async def create_session(self): ) response = await self.browser.send_command("Target.attachToBrowserTarget") if "error" in response: - raise RuntimeError("Could not create session") from Exception( - response["error"] + raise RuntimeError("Could not create session") from DevtoolsProtocolError( + response ) session_id = response["result"]["sessionId"] new_session = Session(self, session_id) - self.add_session(new_session) + self._add_session(new_session) return new_session async def populate_targets(self): @@ -522,7 +526,7 @@ async def read_loop(): error = self.protocol.get_error(response) key = self.protocol.calculate_key(response) if not self.protocol.has_id(response) and error: - raise RuntimeError(error) + raise DevtoolsProtocolError(response) elif self.protocol.is_event(response): ### INFORMATION WE NEED FOR EVERY EVENT event_session_id = response.get("sessionId", "") # GET THE SESSION THAT THE EVENT CAME IN ON @@ -569,7 +573,7 @@ async def read_loop(): if session_closed == "": continue # not actually possible to close browser session this way... target_closed = self._get_target_for_session(session_closed) if target_closed: - target_closed.remove_session(session_closed) + target_closed._remove_session(session_closed) _ = self.protocol.sessions.pop(session_closed, None) if self.debug: print( diff --git a/devtools/target.py b/devtools/target.py index 9e55c491..617e8782 100644 --- a/devtools/target.py +++ b/devtools/target.py @@ -17,13 +17,13 @@ def __init__(self, target_id, browser): self.sessions = OrderedDict() self.target_id = target_id - def add_session(self, session): + def _add_session(self, session): if not isinstance(session, Session): raise TypeError("session must be an object of class Session") self.sessions[session.session_id] = session self.browser.protocol.sessions[session.session_id] = session - def remove_session(self, session_id): + def _remove_session(self, session_id): if isinstance(session_id, Session): session_id = session_id.session_id _ = self.sessions.pop(session_id, None) @@ -43,7 +43,7 @@ async def create_session(self): ) session_id = response["result"]["sessionId"] new_session = Session(self.browser, session_id) - self.add_session(new_session) + self._add_session(new_session) return new_session async def close_session(self, session_id): @@ -57,10 +57,10 @@ async def close_session(self, session_id): command="Target.detachFromTarget", params={"sessionId": session_id}, ) - self.remove_session(session_id) + self._remove_session(session_id) if "error" in response: - raise RuntimeError("Could not close session") from Exception( - response["error"] + raise RuntimeError("Could not close session") from DevtoolsProtocolError( + response ) print(f"The session {session_id} has been closed", file=sys.stderr) return response