diff --git a/java/src/org/openqa/selenium/devtools/idealized/Javascript.java b/java/src/org/openqa/selenium/devtools/idealized/Javascript.java index 061978e69c4b0..53babced82c30 100644 --- a/java/src/org/openqa/selenium/devtools/idealized/Javascript.java +++ b/java/src/org/openqa/selenium/devtools/idealized/Javascript.java @@ -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; @@ -31,7 +32,7 @@ public abstract class Javascript { private final DevTools devtools; - private final Map pinnedScripts = new HashMap<>(); + private final Map> pinnedScripts = new HashMap<>(); private final Set bindings = new HashSet<>(); public Javascript(DevTools devtools) { @@ -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(); } @@ -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 scripts = pinnedScripts.get(devtools.getCdpSession()); + if (scripts.containsKey(script)) { + return scripts.get(script); + } } devtools.send(enableRuntime()); @@ -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 scripts = pinnedScripts.getOrDefault(devtools.getCdpSession(), new HashMap<>()); + scripts.put(script, scriptId); + pinnedScripts.put(devtools.getCdpSession(), scripts); return scriptId; }