1010import org .openqa .selenium .devtools .idealized .Events ;
1111import org .openqa .selenium .devtools .idealized .Javascript ;
1212import org .openqa .selenium .devtools .idealized .ScriptId ;
13+ import org .openqa .selenium .devtools .v85 .page .Page ;
14+ import org .openqa .selenium .devtools .v85 .runtime .Runtime ;
1315import org .openqa .selenium .logging .EventType ;
1416import org .openqa .selenium .logging .HasLogEvents ;
1517import org .openqa .selenium .remote .Augmenter ;
1618
17- import java .util .ArrayList ;
18- import java .util .HashSet ;
19- import java .util .List ;
20- import java .util .Set ;
19+ import java .util .*;
2120import java .util .function .Consumer ;
2221
2322import static org .openqa .selenium .devtools .events .CdpEventTypes .consoleEvent ;
@@ -33,6 +32,7 @@ public class JavaScriptHandling {
3332 private final Events <?, ?> events ;
3433 private final ILocalizedLogger logger = AqualityServices .getLocalizedLogger ();
3534 private final Set <String > bindings = new HashSet <>();
35+ private final Set <InitializationScript > initializationScripts = new HashSet <>();
3636
3737 /**
3838 * Initializes a new instance of the {@link JavaScriptHandling} class.
@@ -44,37 +44,95 @@ public JavaScriptHandling(DevToolsHandling tools) {
4444 this .events = tools .getDevToolsSession ().getDomains ().events ();
4545 }
4646
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+
4768 /**
4869 * Gets the read-only list of binding callbacks added for this JavaScript engine.
4970 * @return list of binding callbacks added for this JavaScript engine.
5071 */
51- public List <String > getBindings () {
72+ public List <String > getScriptCallbackBindings () {
5273 logger .info ("loc.browser.javascript.scriptcallbackbindings.get" );
5374 return new ArrayList <>(bindings );
5475 }
5576
5677 /**
57- * Removes all initialization scripts from being loaded for each document, and stops listening for events .
78+ * Removes all bindings to JavaScript callbacks .
5879 */
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 );
63123 }
64124
65125 /**
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.
72127 */
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+ });
78136 }
79137
80138 /**
@@ -119,16 +177,17 @@ public void addDomMutatedListener(Consumer<DomMutationEvent> listener) {
119177 * Adds a listener for events that occur when methods on the JavaScript console are called.
120178 * @param listener a listener to add, consuming a javascript exception.
121179 */
122- public void addConsoleEventListener (Consumer <ConsoleEvent > listener ) {
180+ public void addJavaScriptConsoleApiListener (Consumer <ConsoleEvent > listener ) {
123181 logger .info ("loc.browser.javascript.event.consoleapicalled.add" );
124182 getDriverThatHasLogEvents ().onLogEvent (consoleEvent (listener ));
125183 }
126184
127185 /**
128186 * Adds a listener for events that occur when methods on the JavaScript console are called.
187+ * Consider using a method {@link this.addJavaScriptConsoleApiListener} instead.
129188 * @param listener a listener to add, consuming a javascript exception.
130189 */
131- public void addJavaScriptConsoleApiListener (Consumer <ConsoleEvent > listener ) {
190+ public void addConsoleEventListener (Consumer <ConsoleEvent > listener ) {
132191 logger .info ("loc.browser.javascript.event.consoleapicalled.add" );
133192 events .addConsoleListener (listener );
134193 }
@@ -143,23 +202,30 @@ public void addJavaScriptExceptionThrownListener(Consumer<JavascriptException> l
143202 }
144203
145204 /**
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).
149206 */
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 ();
154210 }
155211
156212 /**
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.
159214 */
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 ();
164230 }
165231}
0 commit comments