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

org.openqa.selenium.devtools.v121.target.Target 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.19.1
Show newest version
package org.openqa.selenium.devtools.v121.target;

import org.openqa.selenium.Beta;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.ConverterFunctions;
import java.util.Map;
import java.util.LinkedHashMap;
import org.openqa.selenium.json.JsonInput;

/**
 * Supports additional targets discovery and allows to attach to them.
 */
public class Target {

    /**
     * Activates (focuses) the target.
     */
    public static Command activateTarget(org.openqa.selenium.devtools.v121.target.model.TargetID targetId) {
        java.util.Objects.requireNonNull(targetId, "targetId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("targetId", targetId);
        return new Command<>("Target.activateTarget", Map.copyOf(params));
    }

    /**
     * Attaches to the target with given id.
     */
    public static Command attachToTarget(org.openqa.selenium.devtools.v121.target.model.TargetID targetId, java.util.Optional flatten) {
        java.util.Objects.requireNonNull(targetId, "targetId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("targetId", targetId);
        flatten.ifPresent(p -> params.put("flatten", p));
        return new Command<>("Target.attachToTarget", Map.copyOf(params), ConverterFunctions.map("sessionId", org.openqa.selenium.devtools.v121.target.model.SessionID.class));
    }

    /**
     * Attaches to the browser target, only uses flat sessionId mode.
     */
    @Beta()
    public static Command attachToBrowserTarget() {
        LinkedHashMap params = new LinkedHashMap<>();
        return new Command<>("Target.attachToBrowserTarget", Map.copyOf(params), ConverterFunctions.map("sessionId", org.openqa.selenium.devtools.v121.target.model.SessionID.class));
    }

    /**
     * Closes the target. If the target is a page that gets closed too.
     */
    public static Command closeTarget(org.openqa.selenium.devtools.v121.target.model.TargetID targetId) {
        java.util.Objects.requireNonNull(targetId, "targetId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("targetId", targetId);
        return new Command<>("Target.closeTarget", Map.copyOf(params), ConverterFunctions.map("success", java.lang.Boolean.class));
    }

    /**
     * Inject object to the target's main frame that provides a communication
     * channel with browser target.
     *
     * Injected object will be available as `window[bindingName]`.
     *
     * The object has the follwing API:
     * - `binding.send(json)` - a method to send messages over the remote debugging protocol
     * - `binding.onmessage = json => handleMessage(json)` - a callback that will be called for the protocol notifications and command responses.
     */
    @Beta()
    public static Command exposeDevToolsProtocol(org.openqa.selenium.devtools.v121.target.model.TargetID targetId, java.util.Optional bindingName) {
        java.util.Objects.requireNonNull(targetId, "targetId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("targetId", targetId);
        bindingName.ifPresent(p -> params.put("bindingName", p));
        return new Command<>("Target.exposeDevToolsProtocol", Map.copyOf(params));
    }

    /**
     * Creates a new empty BrowserContext. Similar to an incognito profile but you can have more than
     * one.
     */
    @Beta()
    public static Command createBrowserContext(java.util.Optional disposeOnDetach, java.util.Optional proxyServer, java.util.Optional proxyBypassList, java.util.Optional> originsWithUniversalNetworkAccess) {
        LinkedHashMap params = new LinkedHashMap<>();
        disposeOnDetach.ifPresent(p -> params.put("disposeOnDetach", p));
        proxyServer.ifPresent(p -> params.put("proxyServer", p));
        proxyBypassList.ifPresent(p -> params.put("proxyBypassList", p));
        originsWithUniversalNetworkAccess.ifPresent(p -> params.put("originsWithUniversalNetworkAccess", p));
        return new Command<>("Target.createBrowserContext", Map.copyOf(params), ConverterFunctions.map("browserContextId", org.openqa.selenium.devtools.v121.browser.model.BrowserContextID.class));
    }

    /**
     * Returns all browser contexts created with `Target.createBrowserContext` method.
     */
    @Beta()
    public static Command> getBrowserContexts() {
        LinkedHashMap params = new LinkedHashMap<>();
        return new Command<>("Target.getBrowserContexts", Map.copyOf(params), ConverterFunctions.map("browserContextIds", input -> input.readArray(org.openqa.selenium.devtools.v121.browser.model.BrowserContextID.class)));
    }

    /**
     * Creates a new page.
     */
    public static Command createTarget(java.lang.String url, java.util.Optional width, java.util.Optional height, java.util.Optional browserContextId, java.util.Optional enableBeginFrameControl, java.util.Optional newWindow, java.util.Optional background, java.util.Optional forTab) {
        java.util.Objects.requireNonNull(url, "url is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("url", url);
        width.ifPresent(p -> params.put("width", p));
        height.ifPresent(p -> params.put("height", p));
        browserContextId.ifPresent(p -> params.put("browserContextId", p));
        enableBeginFrameControl.ifPresent(p -> params.put("enableBeginFrameControl", p));
        newWindow.ifPresent(p -> params.put("newWindow", p));
        background.ifPresent(p -> params.put("background", p));
        forTab.ifPresent(p -> params.put("forTab", p));
        return new Command<>("Target.createTarget", Map.copyOf(params), ConverterFunctions.map("targetId", org.openqa.selenium.devtools.v121.target.model.TargetID.class));
    }

    /**
     * Detaches session with given id.
     */
    public static Command detachFromTarget(java.util.Optional sessionId, java.util.Optional targetId) {
        LinkedHashMap params = new LinkedHashMap<>();
        sessionId.ifPresent(p -> params.put("sessionId", p));
        targetId.ifPresent(p -> params.put("targetId", p));
        return new Command<>("Target.detachFromTarget", Map.copyOf(params));
    }

    /**
     * Deletes a BrowserContext. All the belonging pages will be closed without calling their
     * beforeunload hooks.
     */
    @Beta()
    public static Command disposeBrowserContext(org.openqa.selenium.devtools.v121.browser.model.BrowserContextID browserContextId) {
        java.util.Objects.requireNonNull(browserContextId, "browserContextId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("browserContextId", browserContextId);
        return new Command<>("Target.disposeBrowserContext", Map.copyOf(params));
    }

    /**
     * Returns information about a target.
     */
    @Beta()
    public static Command getTargetInfo(java.util.Optional targetId) {
        LinkedHashMap params = new LinkedHashMap<>();
        targetId.ifPresent(p -> params.put("targetId", p));
        return new Command<>("Target.getTargetInfo", Map.copyOf(params), ConverterFunctions.map("targetInfo", org.openqa.selenium.devtools.v121.target.model.TargetInfo.class));
    }

    /**
     * Retrieves a list of available targets.
     */
    public static Command> getTargets(java.util.Optional filter) {
        LinkedHashMap params = new LinkedHashMap<>();
        filter.ifPresent(p -> params.put("filter", p));
        return new Command<>("Target.getTargets", Map.copyOf(params), ConverterFunctions.map("targetInfos", input -> input.readArray(org.openqa.selenium.devtools.v121.target.model.TargetInfo.class)));
    }

    /**
     * Sends protocol message over session with given id.
     * Consider using flat mode instead; see commands attachToTarget, setAutoAttach,
     * and crbug.com/991325.
     */
    @Deprecated()
    public static Command sendMessageToTarget(java.lang.String message, java.util.Optional sessionId, java.util.Optional targetId) {
        java.util.Objects.requireNonNull(message, "message is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("message", message);
        sessionId.ifPresent(p -> params.put("sessionId", p));
        targetId.ifPresent(p -> params.put("targetId", p));
        return new Command<>("Target.sendMessageToTarget", Map.copyOf(params));
    }

    /**
     * Controls whether to automatically attach to new targets which are considered to be related to
     * this one. When turned on, attaches to all existing related targets as well. When turned off,
     * automatically detaches from all currently attached targets.
     * This also clears all targets added by `autoAttachRelated` from the list of targets to watch
     * for creation of related targets.
     */
    @Beta()
    public static Command setAutoAttach(java.lang.Boolean autoAttach, java.lang.Boolean waitForDebuggerOnStart, java.util.Optional flatten, java.util.Optional filter) {
        java.util.Objects.requireNonNull(autoAttach, "autoAttach is required");
        java.util.Objects.requireNonNull(waitForDebuggerOnStart, "waitForDebuggerOnStart is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("autoAttach", autoAttach);
        params.put("waitForDebuggerOnStart", waitForDebuggerOnStart);
        flatten.ifPresent(p -> params.put("flatten", p));
        filter.ifPresent(p -> params.put("filter", p));
        return new Command<>("Target.setAutoAttach", Map.copyOf(params));
    }

    /**
     * Adds the specified target to the list of targets that will be monitored for any related target
     * creation (such as child frames, child workers and new versions of service worker) and reported
     * through `attachedToTarget`. The specified target is also auto-attached.
     * This cancels the effect of any previous `setAutoAttach` and is also cancelled by subsequent
     * `setAutoAttach`. Only available at the Browser target.
     */
    @Beta()
    public static Command autoAttachRelated(org.openqa.selenium.devtools.v121.target.model.TargetID targetId, java.lang.Boolean waitForDebuggerOnStart, java.util.Optional filter) {
        java.util.Objects.requireNonNull(targetId, "targetId is required");
        java.util.Objects.requireNonNull(waitForDebuggerOnStart, "waitForDebuggerOnStart is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("targetId", targetId);
        params.put("waitForDebuggerOnStart", waitForDebuggerOnStart);
        filter.ifPresent(p -> params.put("filter", p));
        return new Command<>("Target.autoAttachRelated", Map.copyOf(params));
    }

    /**
     * Controls whether to discover available targets and notify via
     * `targetCreated/targetInfoChanged/targetDestroyed` events.
     */
    public static Command setDiscoverTargets(java.lang.Boolean discover, java.util.Optional filter) {
        java.util.Objects.requireNonNull(discover, "discover is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("discover", discover);
        filter.ifPresent(p -> params.put("filter", p));
        return new Command<>("Target.setDiscoverTargets", Map.copyOf(params));
    }

    /**
     * Enables target discovery for the specified locations, when `setDiscoverTargets` was set to
     * `true`.
     */
    @Beta()
    public static Command setRemoteLocations(java.util.List locations) {
        java.util.Objects.requireNonNull(locations, "locations is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("locations", locations);
        return new Command<>("Target.setRemoteLocations", Map.copyOf(params));
    }

    public static Event attachedToTarget() {
        return new Event<>("Target.attachedToTarget", input -> input.read(org.openqa.selenium.devtools.v121.target.model.AttachedToTarget.class));
    }

    public static Event detachedFromTarget() {
        return new Event<>("Target.detachedFromTarget", input -> input.read(org.openqa.selenium.devtools.v121.target.model.DetachedFromTarget.class));
    }

    public static Event receivedMessageFromTarget() {
        return new Event<>("Target.receivedMessageFromTarget", input -> input.read(org.openqa.selenium.devtools.v121.target.model.ReceivedMessageFromTarget.class));
    }

    public static Event targetCreated() {
        return new Event<>("Target.targetCreated", ConverterFunctions.map("targetInfo", org.openqa.selenium.devtools.v121.target.model.TargetInfo.class));
    }

    public static Event targetDestroyed() {
        return new Event<>("Target.targetDestroyed", ConverterFunctions.map("targetId", org.openqa.selenium.devtools.v121.target.model.TargetID.class));
    }

    public static Event targetCrashed() {
        return new Event<>("Target.targetCrashed", input -> input.read(org.openqa.selenium.devtools.v121.target.model.TargetCrashed.class));
    }

    public static Event targetInfoChanged() {
        return new Event<>("Target.targetInfoChanged", ConverterFunctions.map("targetInfo", org.openqa.selenium.devtools.v121.target.model.TargetInfo.class));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy