All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openqa.selenium.devtools.fetch.Fetch Maven / Gradle / Ivy

Go to download

Selenium automates browsers. That's it! What you do with that power is entirely up to you.

There is a newer version: 4.0.0-rc-1
Show newest version
package org.openqa.selenium.devtools.fetch;

import org.openqa.selenium.Beta;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.ConverterFunctions;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.json.JsonInput;

/**
 * A domain for letting clients substitute browser's network layer with client code.
 */
@Beta()
public class Fetch {

    /**
     * Disables the fetch domain.
     */
    public static Command disable() {
        ImmutableMap.Builder params = ImmutableMap.builder();
        return new Command<>("Fetch.disable", params.build());
    }

    /**
     * Enables issuing of requestPaused events. A request will be paused until client
     * calls one of failRequest, fulfillRequest or continueRequest/continueWithAuth.
     */
    public static Command enable(java.util.Optional> patterns, java.util.Optional handleAuthRequests) {
        ImmutableMap.Builder params = ImmutableMap.builder();
        patterns.ifPresent(p -> params.put("patterns", p));
        handleAuthRequests.ifPresent(p -> params.put("handleAuthRequests", p));
        return new Command<>("Fetch.enable", params.build());
    }

    /**
     * Causes the request to fail with specified reason.
     */
    public static Command failRequest(org.openqa.selenium.devtools.fetch.model.RequestId requestId, org.openqa.selenium.devtools.network.model.ErrorReason errorReason) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        java.util.Objects.requireNonNull(errorReason, "errorReason is required");
        ImmutableMap.Builder params = ImmutableMap.builder();
        params.put("requestId", requestId);
        params.put("errorReason", errorReason);
        return new Command<>("Fetch.failRequest", params.build());
    }

    /**
     * Provides response to the request.
     */
    public static Command fulfillRequest(org.openqa.selenium.devtools.fetch.model.RequestId requestId, java.lang.Integer responseCode, java.util.Optional> responseHeaders, java.util.Optional binaryResponseHeaders, java.util.Optional body, java.util.Optional responsePhrase) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        java.util.Objects.requireNonNull(responseCode, "responseCode is required");
        ImmutableMap.Builder params = ImmutableMap.builder();
        params.put("requestId", requestId);
        params.put("responseCode", responseCode);
        responseHeaders.ifPresent(p -> params.put("responseHeaders", p));
        binaryResponseHeaders.ifPresent(p -> params.put("binaryResponseHeaders", p));
        body.ifPresent(p -> params.put("body", p));
        responsePhrase.ifPresent(p -> params.put("responsePhrase", p));
        return new Command<>("Fetch.fulfillRequest", params.build());
    }

    /**
     * Continues the request, optionally modifying some of its parameters.
     */
    public static Command continueRequest(org.openqa.selenium.devtools.fetch.model.RequestId requestId, java.util.Optional url, java.util.Optional method, java.util.Optional postData, java.util.Optional> headers) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        ImmutableMap.Builder params = ImmutableMap.builder();
        params.put("requestId", requestId);
        url.ifPresent(p -> params.put("url", p));
        method.ifPresent(p -> params.put("method", p));
        postData.ifPresent(p -> params.put("postData", p));
        headers.ifPresent(p -> params.put("headers", p));
        return new Command<>("Fetch.continueRequest", params.build());
    }

    /**
     * Continues a request supplying authChallengeResponse following authRequired event.
     */
    public static Command continueWithAuth(org.openqa.selenium.devtools.fetch.model.RequestId requestId, org.openqa.selenium.devtools.fetch.model.AuthChallengeResponse authChallengeResponse) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        java.util.Objects.requireNonNull(authChallengeResponse, "authChallengeResponse is required");
        ImmutableMap.Builder params = ImmutableMap.builder();
        params.put("requestId", requestId);
        params.put("authChallengeResponse", authChallengeResponse);
        return new Command<>("Fetch.continueWithAuth", params.build());
    }

    public static class GetResponseBodyResponse {

        private final java.lang.String body;

        private final java.lang.Boolean base64Encoded;

        public GetResponseBodyResponse(java.lang.String body, java.lang.Boolean base64Encoded) {
            this.body = java.util.Objects.requireNonNull(body, "body is required");
            this.base64Encoded = java.util.Objects.requireNonNull(base64Encoded, "base64Encoded is required");
        }

        /**
         * Response body.
         */
        public java.lang.String getBody() {
            return body;
        }

        /**
         * True, if content was sent as base64.
         */
        public java.lang.Boolean getBase64Encoded() {
            return base64Encoded;
        }

        private static GetResponseBodyResponse fromJson(JsonInput input) {
            java.lang.String body = null;
            java.lang.Boolean base64Encoded = null;
            input.beginObject();
            while (input.hasNext()) {
                switch(input.nextName()) {
                    case "body":
                        body = input.nextString();
                        break;
                    case "base64Encoded":
                        base64Encoded = input.nextBoolean();
                        break;
                    default:
                        input.skipValue();
                        break;
                }
            }
            input.endObject();
            return new GetResponseBodyResponse(body, base64Encoded);
        }
    }

    /**
     * Causes the body of the response to be received from the server and
     * returned as a single string. May only be issued for a request that
     * is paused in the Response stage and is mutually exclusive with
     * takeResponseBodyForInterceptionAsStream. Calling other methods that
     * affect the request or disabling fetch domain before body is received
     * results in an undefined behavior.
     */
    public static Command getResponseBody(org.openqa.selenium.devtools.fetch.model.RequestId requestId) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        ImmutableMap.Builder params = ImmutableMap.builder();
        params.put("requestId", requestId);
        return new Command<>("Fetch.getResponseBody", params.build(), input -> input.read(org.openqa.selenium.devtools.fetch.Fetch.GetResponseBodyResponse.class));
    }

    /**
     * Returns a handle to the stream representing the response body.
     * The request must be paused in the HeadersReceived stage.
     * Note that after this command the request can't be continued
     * as is -- client either needs to cancel it or to provide the
     * response body.
     * The stream only supports sequential read, IO.read will fail if the position
     * is specified.
     * This method is mutually exclusive with getResponseBody.
     * Calling other methods that affect the request or disabling fetch
     * domain before body is received results in an undefined behavior.
     */
    public static Command takeResponseBodyAsStream(org.openqa.selenium.devtools.fetch.model.RequestId requestId) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        ImmutableMap.Builder params = ImmutableMap.builder();
        params.put("requestId", requestId);
        return new Command<>("Fetch.takeResponseBodyAsStream", params.build(), ConverterFunctions.map("stream", org.openqa.selenium.devtools.io.model.StreamHandle.class));
    }

    public static Event requestPaused() {
        return new Event<>("Fetch.requestPaused", input -> input.read(org.openqa.selenium.devtools.fetch.model.RequestPaused.class));
    }

    public static Event authRequired() {
        return new Event<>("Fetch.authRequired", input -> input.read(org.openqa.selenium.devtools.fetch.model.AuthRequired.class));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy