Exemple #1
0
    def send_command(self, method, url, body=None):
        """
        Send a command to the remote end and validate its success.

        :param method: HTTP method to use in request.
        :param uri: "Command part" of the HTTP request URL,
            e.g. `window/rect`.
        :param body: Optional body of the HTTP request.

        :return: `None` if the HTTP response body was empty, otherwise
            the result of parsing the body as JSON.

        :raises error.WebDriverException: If the remote end returns
            an error.
        """
        response = self.transport.send(method, url, body)

        if "value" in response.body:
            value = response.body["value"]
        else:
            raise error.UnknownErrorException(
                "No 'value' key in response body:\n%s" %
                json.dumps(response.body))

        if response.status != 200:
            cls = error.get(value.get("error"))
            raise cls(value.get("message"))

        return value
Exemple #2
0
    def from_http_response(cls, http_response):
        status = http_response.status
        body = http_response.read()

        # SpecID: dfn-send-a-response
        #
        # > 3. Set the response's header with name and value with the following
        # >    values:
        # >
        # >    "Content-Type"
        # >       "application/json; charset=utf-8"
        # >    "cache-control"
        # >       "no-cache"

        if body:
            try:
                body = json.loads(body)
            except:
                raise error.UnknownErrorException("Failed to decode body as json:\n%s" % body)

        return cls(status, body)