20
20
import org .openqa .selenium .devtools .Command ;
21
21
import org .openqa .selenium .devtools .DevTools ;
22
22
import org .openqa .selenium .devtools .Event ;
23
+ import org .openqa .selenium .devtools .idealized .target .model .SessionID ;
23
24
import org .openqa .selenium .internal .Require ;
24
25
25
26
import java .util .HashMap ;
31
32
public abstract class Javascript <SCRIPTID , BINDINGCALLED > {
32
33
33
34
private final DevTools devtools ;
34
- private final Map <String , ScriptId > pinnedScripts = new HashMap <>();
35
+ private final Map <SessionID , Map < String , ScriptId > > pinnedScripts = new HashMap <>();
35
36
private final Set <String > bindings = new HashSet <>();
36
37
37
38
public Javascript (DevTools devtools ) {
@@ -42,7 +43,8 @@ public void disable() {
42
43
devtools .send (disableRuntime ());
43
44
devtools .send (disablePage ());
44
45
45
- pinnedScripts .values ().forEach (id -> removeScriptToEvaluateOnNewDocument (id .getActualId ()));
46
+ pinnedScripts .forEach ((sessionID , scriptIdMap ) -> scriptIdMap .values ()
47
+ .forEach (id -> removeScriptToEvaluateOnNewDocument (id .getActualId ())));
46
48
47
49
pinnedScripts .clear ();
48
50
}
@@ -55,8 +57,11 @@ public ScriptId pin(String exposeScriptAs, String script) {
55
57
Require .nonNull ("Script name" , exposeScriptAs );
56
58
Require .nonNull ("Script" , script );
57
59
58
- if (pinnedScripts .containsKey (script )) {
59
- return pinnedScripts .get (script );
60
+ if (pinnedScripts .containsKey (devtools .getCdpSession ())) {
61
+ Map <String , ScriptId > scripts = pinnedScripts .get (devtools .getCdpSession ());
62
+ if (scripts .containsKey (script )) {
63
+ return scripts .get (script );
64
+ }
60
65
}
61
66
62
67
devtools .send (enableRuntime ());
@@ -68,7 +73,9 @@ public ScriptId pin(String exposeScriptAs, String script) {
68
73
SCRIPTID id = devtools .send (addScriptToEvaluateOnNewDocument (script ));
69
74
ScriptId scriptId = new ScriptId (id );
70
75
71
- pinnedScripts .put (script , scriptId );
76
+ Map <String , ScriptId > scripts = pinnedScripts .getOrDefault (devtools .getCdpSession (), new HashMap <>());
77
+ scripts .put (script , scriptId );
78
+ pinnedScripts .put (devtools .getCdpSession (), scripts );
72
79
73
80
return scriptId ;
74
81
}
0 commit comments