Skip to content

Commit

Permalink
[java][cdp] Keep track of pinned scripts per CDP session (#11598)
Browse files Browse the repository at this point in the history
Related to #11573
  • Loading branch information
pujagani authored Jan 31, 2023
1 parent 775d6bb commit 20d68b3
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions java/src/org/openqa/selenium/devtools/idealized/Javascript.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;

import java.util.HashMap;
Expand All @@ -31,7 +32,7 @@
public abstract class Javascript<SCRIPTID, BINDINGCALLED> {

private final DevTools devtools;
private final Map<String, ScriptId> pinnedScripts = new HashMap<>();
private final Map<SessionID, Map<String, ScriptId>> pinnedScripts = new HashMap<>();
private final Set<String> bindings = new HashSet<>();

public Javascript(DevTools devtools) {
Expand All @@ -42,7 +43,8 @@ public void disable() {
devtools.send(disableRuntime());
devtools.send(disablePage());

pinnedScripts.values().forEach(id -> removeScriptToEvaluateOnNewDocument(id.getActualId()));
pinnedScripts.forEach((sessionID, scriptIdMap) -> scriptIdMap.values()
.forEach(id -> removeScriptToEvaluateOnNewDocument(id.getActualId())));

pinnedScripts.clear();
}
Expand All @@ -55,8 +57,11 @@ public ScriptId pin(String exposeScriptAs, String script) {
Require.nonNull("Script name", exposeScriptAs);
Require.nonNull("Script", script);

if (pinnedScripts.containsKey(script)) {
return pinnedScripts.get(script);
if (pinnedScripts.containsKey(devtools.getCdpSession())) {
Map<String, ScriptId> scripts = pinnedScripts.get(devtools.getCdpSession());
if (scripts.containsKey(script)) {
return scripts.get(script);
}
}

devtools.send(enableRuntime());
Expand All @@ -68,7 +73,9 @@ public ScriptId pin(String exposeScriptAs, String script) {
SCRIPTID id = devtools.send(addScriptToEvaluateOnNewDocument(script));
ScriptId scriptId = new ScriptId(id);

pinnedScripts.put(script, scriptId);
Map<String, ScriptId> scripts = pinnedScripts.getOrDefault(devtools.getCdpSession(), new HashMap<>());
scripts.put(script, scriptId);
pinnedScripts.put(devtools.getCdpSession(), scripts);

return scriptId;
}
Expand Down

0 comments on commit 20d68b3

Please sign in to comment.