org.openqa.selenium.devtools.idealized.Javascript Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-remote-driver Show documentation
Show all versions of selenium-remote-driver Show documentation
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.devtools.idealized;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
import org.openqa.selenium.internal.Require;
public abstract class Javascript {
private final DevTools devtools;
private final Map> pinnedScripts = new HashMap<>();
private final Set bindings = new HashSet<>();
public Javascript(DevTools devtools) {
this.devtools = Require.nonNull("DevTools", devtools);
}
public void disable() {
devtools.send(disableRuntime());
devtools.send(disablePage());
pinnedScripts.forEach(
(sessionID, scriptIdMap) ->
scriptIdMap
.values()
.forEach(id -> removeScriptToEvaluateOnNewDocument(id.getActualId())));
pinnedScripts.clear();
}
protected abstract Command disablePage();
protected abstract Command disableRuntime();
public ScriptId pin(String exposeScriptAs, String script) {
Require.nonNull("Script name", exposeScriptAs);
Require.nonNull("Script", script);
if (pinnedScripts.containsKey(devtools.getCdpSession())) {
Map scripts = pinnedScripts.get(devtools.getCdpSession());
if (scripts.containsKey(script)) {
return scripts.get(script);
}
}
devtools.send(enableRuntime());
devtools.send(doAddJsBinding(exposeScriptAs));
devtools.send(enablePage());
SCRIPTID id = devtools.send(addScriptToEvaluateOnNewDocument(script));
ScriptId scriptId = new ScriptId(id);
Map scripts =
pinnedScripts.getOrDefault(devtools.getCdpSession(), new HashMap<>());
scripts.put(script, scriptId);
pinnedScripts.put(devtools.getCdpSession(), scripts);
return scriptId;
}
public void addBindingCalledListener(Consumer listener) {
Require.nonNull("Listener", listener);
devtools.send(enableRuntime());
devtools.addListener(
bindingCalledEvent(),
event -> {
String payload = extractPayload(event);
listener.accept(payload);
});
}
public void addJsBinding(String scriptName) {
Require.nonNull("Script name", scriptName);
bindings.add(scriptName);
doAddJsBinding(scriptName);
}
public void removeJsBinding(String scriptName) {
Require.nonNull("Script name", scriptName);
bindings.remove(scriptName);
doRemoveJsBinding(scriptName);
}
protected abstract Command enableRuntime();
protected abstract Command doAddJsBinding(String scriptName);
protected abstract Command doRemoveJsBinding(String scriptName);
protected abstract Command enablePage();
protected abstract Command addScriptToEvaluateOnNewDocument(String script);
protected abstract Command removeScriptToEvaluateOnNewDocument(SCRIPTID id);
protected abstract Event bindingCalledEvent();
protected abstract String extractPayload(BINDINGCALLED event);
}