Esempio n. 1
0
    def _dispatch(self, msg: ParsedMessagePayload) -> None:
        id = msg.get("id")
        if id:
            callback = self._callbacks.pop(id)
            error = msg.get("error")
            if error:
                parsed_error = parse_error(error["error"])  # type: ignore
                parsed_error.stack = callback.stack_trace
                callback.future.set_exception(parsed_error)
            else:
                result = self._replace_guids_with_channels(msg.get("result"))
                callback.future.set_result(result)
            return

        guid = msg["guid"]
        method = msg.get("method")
        params = msg["params"]
        if method == "__create__":
            parent = self._objects[guid]
            self._create_remote_object(parent, params["type"], params["guid"],
                                       params["initializer"])
            return
        if method == "__dispose__":
            self._objects[guid]._dispose()
            return

        object = self._objects[guid]
        try:
            if self._is_sync:
                for listener in object._channel.listeners(method):
                    g = greenlet(listener)
                    g.switch(self._replace_guids_with_channels(params))
            else:
                object._channel.emit(method,
                                     self._replace_guids_with_channels(params))
        except Exception:
            print(
                "Error dispatching the event",
                "".join(traceback.format_exception(*sys.exc_info())),
            )
Esempio n. 2
0
    def __init__(self, parent: ChannelOwner, type: str, guid: str,
                 initializer: Dict) -> None:
        super().__init__(parent, type, guid, initializer)
        self.accessibility = Accessibility(self._channel)
        self.keyboard = Keyboard(self._channel)
        self.mouse = Mouse(self._channel)
        self.touchscreen = Touchscreen(self._channel)

        self._main_frame: Frame = from_channel(initializer["mainFrame"])
        self._main_frame._page = self
        self._frames = [self._main_frame]
        vs = initializer.get("viewportSize")
        self._viewport_size: Optional[Tuple[int, int]] = ((vs["width"],
                                                           vs["height"])
                                                          if vs else None)
        self._is_closed = False
        self._workers: List["Worker"] = []
        self._bindings: Dict[str, Any] = {}
        self._pending_wait_for_events: List[PendingWaitEvent] = []
        self._routes: List[RouteHandlerEntry] = []
        self._owned_context: Optional["BrowserContext"] = None
        self._timeout_settings: TimeoutSettings = TimeoutSettings(None)
        self._video: Optional[Video] = None

        self._channel.on(
            "bindingCall",
            lambda params: self._on_binding(from_channel(params["binding"])),
        )
        self._channel.on("close", lambda _: self._on_close())
        self._channel.on(
            "console",
            lambda params: self.emit(Page.Events.Console,
                                     from_channel(params["message"])),
        )
        self._channel.on("crash", lambda _: self._on_crash())
        self._channel.on(
            "dialog",
            lambda params: self.emit(Page.Events.Dialog,
                                     from_channel(params["dialog"])),
        )
        self._channel.on("domcontentloaded",
                         lambda _: self.emit(Page.Events.DOMContentLoaded))
        self._channel.on(
            "download",
            lambda params: self.emit(Page.Events.Download,
                                     from_channel(params["download"])),
        )
        self._channel.on(
            "fileChooser",
            lambda params: self.emit(
                Page.Events.FileChooser,
                FileChooser(self, from_channel(params["element"]), params[
                    "isMultiple"]),
            ),
        )
        self._channel.on(
            "frameAttached",
            lambda params: self._on_frame_attached(
                from_channel(params["frame"])),
        )
        self._channel.on(
            "frameDetached",
            lambda params: self._on_frame_detached(
                from_channel(params["frame"])),
        )
        self._channel.on("load", lambda _: self.emit(Page.Events.Load))
        self._channel.on(
            "pageError",
            lambda params: self.emit(Page.Events.PageError,
                                     parse_error(params["error"]["error"])),
        )
        self._channel.on(
            "popup",
            lambda params: self.emit(Page.Events.Popup,
                                     from_channel(params["page"])),
        )
        self._channel.on(
            "request",
            lambda params: self.emit(Page.Events.Request,
                                     from_channel(params["request"])),
        )
        self._channel.on(
            "requestFailed",
            lambda params: self._on_request_failed(
                from_channel(params["request"]),
                params["responseEndTiming"],
                params["failureText"],
            ),
        )
        self._channel.on(
            "requestFinished",
            lambda params: self._on_request_finished(
                from_channel(params["request"]), params["responseEndTiming"]),
        )
        self._channel.on(
            "response",
            lambda params: self.emit(Page.Events.Response,
                                     from_channel(params["response"])),
        )
        self._channel.on(
            "route",
            lambda params: self._on_route(from_channel(params["route"]),
                                          from_channel(params["request"])),
        )
        self._channel.on(
            "video",
            lambda params: cast(Video, self.video)._set_relative_path(params[
                "relativePath"]),
        )
        self._channel.on(
            "webSocket",
            lambda params: self.emit(Page.Events.WebSocket,
                                     from_channel(params["webSocket"])),
        )
        self._channel.on(
            "worker",
            lambda params: self._on_worker(from_channel(params["worker"])))