10
10
import org .openqa .selenium .devtools .idealized .Events ;
11
11
import org .openqa .selenium .devtools .idealized .Javascript ;
12
12
import org .openqa .selenium .devtools .idealized .ScriptId ;
13
+ import org .openqa .selenium .devtools .v85 .page .Page ;
14
+ import org .openqa .selenium .devtools .v85 .runtime .Runtime ;
13
15
import org .openqa .selenium .logging .EventType ;
14
16
import org .openqa .selenium .logging .HasLogEvents ;
15
17
import org .openqa .selenium .remote .Augmenter ;
16
18
17
- import java .util .ArrayList ;
18
- import java .util .HashSet ;
19
- import java .util .List ;
20
- import java .util .Set ;
19
+ import java .util .*;
21
20
import java .util .function .Consumer ;
22
21
23
22
import static org .openqa .selenium .devtools .events .CdpEventTypes .consoleEvent ;
@@ -33,6 +32,7 @@ public class JavaScriptHandling {
33
32
private final Events <?, ?> events ;
34
33
private final ILocalizedLogger logger = AqualityServices .getLocalizedLogger ();
35
34
private final Set <String > bindings = new HashSet <>();
35
+ private final Set <InitializationScript > initializationScripts = new HashSet <>();
36
36
37
37
/**
38
38
* Initializes a new instance of the {@link JavaScriptHandling} class.
@@ -44,37 +44,95 @@ public JavaScriptHandling(DevToolsHandling tools) {
44
44
this .events = tools .getDevToolsSession ().getDomains ().events ();
45
45
}
46
46
47
+ /**
48
+ * Adds a binding to a callback method that will raise an event when the named binding is called by JavaScript
49
+ * executing in the browser.
50
+ * @param scriptName The name of the callback that will trigger events.
51
+ */
52
+ public void addScriptCallbackBinding (String scriptName ) {
53
+ logger .info ("loc.browser.javascript.scriptcallbackbinding.add" , scriptName );
54
+ bindings .add (scriptName );
55
+ tools .sendCommand (Runtime .addBinding (scriptName , Optional .empty ()));
56
+ }
57
+
58
+ /**
59
+ * Removes a binding to a JavaScript callback.
60
+ * @param scriptName The name of the callback to be removed.
61
+ */
62
+ public void removeScriptCallbackBinding (String scriptName ) {
63
+ logger .info ("loc.browser.javascript.scriptcallbackbinding.remove" , scriptName );
64
+ bindings .remove (scriptName );
65
+ tools .sendCommand (Runtime .removeBinding (scriptName ));
66
+ }
67
+
47
68
/**
48
69
* Gets the read-only list of binding callbacks added for this JavaScript engine.
49
70
* @return list of binding callbacks added for this JavaScript engine.
50
71
*/
51
- public List <String > getBindings () {
72
+ public List <String > getScriptCallbackBindings () {
52
73
logger .info ("loc.browser.javascript.scriptcallbackbindings.get" );
53
74
return new ArrayList <>(bindings );
54
75
}
55
76
56
77
/**
57
- * Removes all initialization scripts from being loaded for each document, and stops listening for events .
78
+ * Removes all bindings to JavaScript callbacks .
58
79
*/
59
- public void disable () {
60
- logger .info ("loc.browser.javascript.reset" );
61
- engine .disable ();
62
- bindings .clear ();
80
+ public void clearScriptCallbackBindings () {
81
+ logger .info ("loc.browser.javascript.scriptcallbackbindings.clear" );
82
+ bindings .forEach (scriptName -> {
83
+ bindings .remove (scriptName );
84
+ tools .sendCommand (Runtime .removeBinding (scriptName ));
85
+ });
86
+ }
87
+
88
+ /**
89
+ * Adds JavaScript to be loaded on every document load, and adds a binding to a callback method
90
+ * that will raise an event when the script with that name is called.
91
+ * @param scriptName The friendly name by which to refer to this initialization script.
92
+ * @param script The JavaScript to be loaded on every page.
93
+ * @return Initialization script.
94
+ */
95
+ public InitializationScript addInitializationScript (String scriptName , String script ) {
96
+ logger .info ("loc.browser.javascript.initializationscript.add" , scriptName );
97
+ logger .info ("loc.browser.javascript.scriptcallbackbinding.add" , scriptName );
98
+ ScriptId scriptId = engine .pin (scriptName , script );
99
+ InitializationScript initializationScript = new InitializationScript (scriptId , scriptName , script );
100
+ bindings .add (scriptName );
101
+ initializationScripts .add (initializationScript );
102
+ return initializationScript ;
103
+ }
104
+
105
+ /**
106
+ * Removes JavaScript from being loaded on every document load, and removes a callback binding for it.
107
+ * @param script an instance of script to be removed.
108
+ */
109
+ public void removeInitializationScript (InitializationScript script ) {
110
+ logger .info ("loc.browser.javascript.initializationscript.remove" , script .getScriptName ());
111
+ tools .sendCommand (Page .removeScriptToEvaluateOnNewDocument (script .getScriptId ().getActualId ()));
112
+ initializationScripts .remove (script );
113
+ removeScriptCallbackBinding (script .getScriptName ());
114
+ }
115
+
116
+ /**
117
+ * Gets the read-only list of initialization scripts added for this JavaScript engine.
118
+ * @return the list of added initialization scripts.
119
+ */
120
+ public List <InitializationScript > getInitializationScripts () {
121
+ logger .info ("loc.browser.javascript.initializationscripts.get" );
122
+ return new ArrayList <>(initializationScripts );
63
123
}
64
124
65
125
/**
66
- * Pins a JavaScript snippet for execution in the browser without transmitting the entire script across the wire
67
- * for every execution.
68
- * @param exposeScriptAs The name of the callback that will trigger events when the named binding is called by
69
- * JavaScript executing in the browser.
70
- * @param script The JavaScript to pin.
71
- * @return a {@link ScriptId} object to use to execute the script.
126
+ * Removes all initialization scripts from being loaded on every document load.
72
127
*/
73
- public ScriptId pin (String exposeScriptAs , String script ) {
74
- logger .info ("loc.browser.javascript.snippet.pin" );
75
- logger .info ("loc.browser.javascript.scriptcallbackbinding.add" , exposeScriptAs );
76
- bindings .add (exposeScriptAs );
77
- return engine .pin (exposeScriptAs , script );
128
+ public void clearInitializationScripts () {
129
+ logger .info ("loc.browser.javascript.initializationscripts.clear" );
130
+ initializationScripts .forEach (script -> {
131
+ Page .removeScriptToEvaluateOnNewDocument (script .getScriptId ().getActualId ());
132
+ initializationScripts .remove (script );
133
+ tools .sendCommand (Runtime .removeBinding (script .getScriptName ()));
134
+ bindings .remove (script .getScriptName ());
135
+ });
78
136
}
79
137
80
138
/**
@@ -119,16 +177,17 @@ public void addDomMutatedListener(Consumer<DomMutationEvent> listener) {
119
177
* Adds a listener for events that occur when methods on the JavaScript console are called.
120
178
* @param listener a listener to add, consuming a javascript exception.
121
179
*/
122
- public void addConsoleEventListener (Consumer <ConsoleEvent > listener ) {
180
+ public void addJavaScriptConsoleApiListener (Consumer <ConsoleEvent > listener ) {
123
181
logger .info ("loc.browser.javascript.event.consoleapicalled.add" );
124
182
getDriverThatHasLogEvents ().onLogEvent (consoleEvent (listener ));
125
183
}
126
184
127
185
/**
128
186
* Adds a listener for events that occur when methods on the JavaScript console are called.
187
+ * Consider using a method {@link this.addJavaScriptConsoleApiListener} instead.
129
188
* @param listener a listener to add, consuming a javascript exception.
130
189
*/
131
- public void addJavaScriptConsoleApiListener (Consumer <ConsoleEvent > listener ) {
190
+ public void addConsoleEventListener (Consumer <ConsoleEvent > listener ) {
132
191
logger .info ("loc.browser.javascript.event.consoleapicalled.add" );
133
192
events .addConsoleListener (listener );
134
193
}
@@ -143,23 +202,30 @@ public void addJavaScriptExceptionThrownListener(Consumer<JavascriptException> l
143
202
}
144
203
145
204
/**
146
- * Adds a binding to a callback method that will raise an event when the named binding is called by JavaScript
147
- * executing in the browser.
148
- * @param scriptName The name of the callback that will trigger events.
205
+ * Disables console event listener and JavaScript event listener (disables the runtime).
149
206
*/
150
- public void addJsBinding (String scriptName ) {
151
- logger .info ("loc.browser.javascript.scriptcallbackbinding.add" , scriptName );
152
- engine .addJsBinding (scriptName );
153
- bindings .add (scriptName );
207
+ public void disableConsoleEventListeners () {
208
+ logger .info ("loc.browser.javascript.event.consoleapicalled.disable" );
209
+ events .disable ();
154
210
}
155
211
156
212
/**
157
- * Removes a binding to a JavaScript callback.
158
- * @param scriptName The name of the callback to be removed.
213
+ * Removes all bindings to JavaScript callbacks and all initialization scripts from being loaded for each document.
159
214
*/
160
- public void removeJsBinding (String scriptName ) {
161
- logger .info ("loc.browser.javascript.scriptcallbackbinding.remove" , scriptName );
162
- engine .removeJsBinding (scriptName );
163
- bindings .remove (scriptName );
215
+ public void clearAll () {
216
+ logger .info ("loc.browser.javascript.clearall" );
217
+ clearInitializationScripts ();
218
+ clearScriptCallbackBindings ();
219
+ }
220
+
221
+ /**
222
+ * Removes all bindings to JavaScript callbacks and all initialization scripts from being loaded for each document,
223
+ * and stops listening for events.
224
+ */
225
+ public void reset () {
226
+ logger .info ("loc.browser.javascript.reset" );
227
+ engine .disable ();
228
+ clearInitializationScripts ();
229
+ clearScriptCallbackBindings ();
164
230
}
165
231
}
0 commit comments