Execute a command on the server and wait for the result. Args: cmd: any CDP command Returns: a CDP result
(self, cmd: Generator[dict, T, Any])
| 199 | self.inflight_result = {} |
| 200 | |
| 201 | async def execute(self, cmd: Generator[dict, T, Any]) -> T: |
| 202 | """Execute a command on the server and wait for the result. |
| 203 | |
| 204 | Args: |
| 205 | cmd: any CDP command |
| 206 | |
| 207 | Returns: |
| 208 | a CDP result |
| 209 | """ |
| 210 | cmd_id = next(self.id_iter) |
| 211 | cmd_event = trio.Event() |
| 212 | self.inflight_cmd[cmd_id] = cmd, cmd_event |
| 213 | request = next(cmd) |
| 214 | request["id"] = cmd_id |
| 215 | if self.session_id: |
| 216 | request["sessionId"] = self.session_id |
| 217 | request_str = json.dumps(request) |
| 218 | if logger.isEnabledFor(logging.DEBUG): |
| 219 | logger.debug(f"Sending CDP message: {cmd_id} {cmd_event}: {request_str}") |
| 220 | try: |
| 221 | await self.ws.send_message(request_str) |
| 222 | except WsConnectionClosed as wcc: |
| 223 | raise CdpConnectionClosed(wcc.reason) from None |
| 224 | await cmd_event.wait() |
| 225 | response = self.inflight_result.pop(cmd_id) |
| 226 | if logger.isEnabledFor(logging.DEBUG): |
| 227 | logger.debug(f"Received CDP message: {response}") |
| 228 | if isinstance(response, Exception): |
| 229 | if logger.isEnabledFor(logging.DEBUG): |
| 230 | logger.debug(f"Exception raised by {cmd_event} message: {type(response).__name__}") |
| 231 | raise response |
| 232 | return response |
| 233 | |
| 234 | def listen(self, *event_types, buffer_size=10): |
| 235 | """Listen for events. |
nothing calls this directly
no test coverage detected