forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaScriptEngine.cs
452 lines (404 loc) · 19 KB
/
JavaScriptEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// <copyright file="JavaScriptEngine.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.Internal;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace OpenQA.Selenium
{
/// <summary>
/// Provides methods allowing the user to manage settings in the browser's JavaScript engine.
/// </summary>
public class JavaScriptEngine : IJavaScriptEngine
{
private readonly string MonitorBindingName = "__webdriver_attribute";
private IWebDriver driver;
private Lazy<DevToolsSession> session;
private Dictionary<string, InitializationScript> initializationScripts = new Dictionary<string, InitializationScript>();
private Dictionary<string, PinnedScript> pinnedScripts = new Dictionary<string, PinnedScript>();
private HashSet<string> bindings = new HashSet<string>();
private bool isEnabled = false;
private bool isDisposed = false;
/// <summary>
/// Initializes a new instance of the <see cref="JavaScriptEngine"/> class.
/// </summary>
/// <param name="driver">The <see cref="IWebDriver"/> instance in which the JavaScript engine is executing.</param>
public JavaScriptEngine(IWebDriver driver)
{
// Use of Lazy<T> means this exception won't be thrown until the user first
// attempts to access the DevTools session, probably on the first call to
// StartEventMonitoring() or in adding scripts to the instance.
this.driver = driver;
this.session = new Lazy<DevToolsSession>(() =>
{
IDevTools devToolsDriver = driver as IDevTools;
if (devToolsDriver == null)
{
throw new WebDriverException("Driver must implement IDevTools to use these features");
}
return devToolsDriver.GetDevToolsSession();
});
}
/// <summary>
/// Occurs when a JavaScript callback with a named binding is executed.
/// </summary>
public event EventHandler<JavaScriptCallbackExecutedEventArgs> JavaScriptCallbackExecuted;
/// <summary>
/// Occurs when an exeception is thrown by JavaScript being executed in the browser.
/// </summary>
public event EventHandler<JavaScriptExceptionThrownEventArgs> JavaScriptExceptionThrown;
/// <summary>
/// Occurs when methods on the JavaScript console are called.
/// </summary>
public event EventHandler<JavaScriptConsoleApiCalledEventArgs> JavaScriptConsoleApiCalled;
/// <summary>
/// Occurs when a value of an attribute in an element is being changed.
/// </summary>
public event EventHandler<DomMutatedEventArgs> DomMutated;
/// <summary>
/// Gets the read-only list of initialization scripts added for this JavaScript engine.
/// </summary>
public IReadOnlyList<InitializationScript> InitializationScripts
{
get
{
// Return a copy.
return new List<InitializationScript>(this.initializationScripts.Values);
}
}
/// <summary>
/// Gets the read-only list of bindings added for this JavaScript engine.
/// </summary>
public IReadOnlyList<string> ScriptCallbackBindings
{
get
{
// Return a copy.
return new List<string>(this.bindings);
}
}
/// <summary>
/// Asynchronously starts monitoring for events from the browser's JavaScript engine.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task StartEventMonitoring()
{
this.session.Value.Domains.JavaScript.BindingCalled += OnScriptBindingCalled;
this.session.Value.Domains.JavaScript.ExceptionThrown += OnJavaScriptExceptionThrown;
this.session.Value.Domains.JavaScript.ConsoleApiCalled += OnConsoleApiCalled;
await this.EnableDomains().ConfigureAwait(false);
}
/// <summary>
/// Stops monitoring for events from the browser's JavaScript engine.
/// </summary>
public void StopEventMonitoring()
{
this.session.Value.Domains.JavaScript.ConsoleApiCalled -= OnConsoleApiCalled;
this.session.Value.Domains.JavaScript.ExceptionThrown -= OnJavaScriptExceptionThrown;
this.session.Value.Domains.JavaScript.BindingCalled -= OnScriptBindingCalled;
}
/// <summary>
/// Enables monitoring for DOM changes.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task EnableDomMutationMonitoring()
{
// Execute the script to have it enabled on the currently loaded page.
string script = GetMutationListenerScript();
await this.session.Value.Domains.JavaScript.Evaluate(script).ConfigureAwait(false);
await this.AddScriptCallbackBinding(MonitorBindingName).ConfigureAwait(false);
await this.AddInitializationScript(MonitorBindingName, script).ConfigureAwait(false);
}
/// <summary>
/// Disables monitoring for DOM changes.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task DisableDomMutationMonitoring()
{
await this.RemoveScriptCallbackBinding(MonitorBindingName).ConfigureAwait(false);
await this.RemoveInitializationScript(MonitorBindingName).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously adds JavaScript to be loaded on every document load.
/// </summary>
/// <param name="scriptName">The friendly name by which to refer to this initialization script.</param>
/// <param name="script">The JavaScript to be loaded on every page.</param>
/// <returns>A task containing an <see cref="InitializationScript"/> object representing the script to be loaded on each page.</returns>
public async Task<InitializationScript> AddInitializationScript(string scriptName, string script)
{
if (this.initializationScripts.ContainsKey(scriptName))
{
return this.initializationScripts[scriptName];
}
await this.EnableDomains().ConfigureAwait(false);
string scriptId = await this.session.Value.Domains.JavaScript.AddScriptToEvaluateOnNewDocument(script).ConfigureAwait(false);
InitializationScript initializationScript = new InitializationScript()
{
ScriptId = scriptId,
ScriptName = scriptName,
ScriptSource = script
};
this.initializationScripts[scriptName] = initializationScript;
return initializationScript;
}
/// <summary>
/// Asynchronously removes JavaScript from being loaded on every document load.
/// </summary>
/// <param name="scriptName">The friendly name of the initialization script to be removed.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task RemoveInitializationScript(string scriptName)
{
if (this.initializationScripts.ContainsKey(scriptName))
{
string scriptId = this.initializationScripts[scriptName].ScriptId;
await this.session.Value.Domains.JavaScript.RemoveScriptToEvaluateOnNewDocument(scriptId).ConfigureAwait(false);
this.initializationScripts.Remove(scriptName);
}
}
/// <summary>
/// Asynchronously removes all intialization scripts from being loaded on every document load.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task ClearInitializationScripts()
{
// Use a copy of the list to prevent the iterator from becoming invalid
// when we modify the collection.
List<string> scriptNames = new List<string>(this.initializationScripts.Keys);
foreach (string scriptName in scriptNames)
{
await this.RemoveInitializationScript(scriptName).ConfigureAwait(false);
}
}
/// <summary>
/// Pins a JavaScript snippet for execution in the browser without transmitting the
/// entire script across the wire for every execution.
/// </summary>
/// <param name="script">The JavaScript to pin</param>
/// <returns>A task containing a <see cref="PinnedScript"/> object to use to execute the script.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="script"/> is <see langword="null"/>.</exception>
public async Task<PinnedScript> PinScript(string script)
{
if (script == null)
{
throw new ArgumentNullException(nameof(script));
}
string newScriptHandle = Guid.NewGuid().ToString("N");
// We do an "Evaluate" first so as to immediately create the script on the loaded
// page, then will add it to the initialization of future pages.
await this.EnableDomains().ConfigureAwait(false);
string creationScript = PinnedScript.MakeCreationScript(newScriptHandle, script);
await this.session.Value.Domains.JavaScript.Evaluate(creationScript).ConfigureAwait(false);
string scriptId = await this.session.Value.Domains.JavaScript.AddScriptToEvaluateOnNewDocument(creationScript).ConfigureAwait(false);
PinnedScript pinnedScript = new PinnedScript(script, newScriptHandle, scriptId);
this.pinnedScripts[pinnedScript.Handle] = pinnedScript;
return pinnedScript;
}
/// <summary>
/// Unpins a previously pinned script from the browser.
/// </summary>
/// <param name="script">The <see cref="PinnedScript"/> object to unpin.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="script"/> is <see langword="null"/>.</exception>
public async Task UnpinScript(PinnedScript script)
{
if (script == null)
{
throw new ArgumentNullException(nameof(script));
}
if (this.pinnedScripts.ContainsKey(script.Handle))
{
await this.session.Value.Domains.JavaScript.Evaluate(script.MakeRemovalScript()).ConfigureAwait(false);
await this.session.Value.Domains.JavaScript.RemoveScriptToEvaluateOnNewDocument(script.ScriptId).ConfigureAwait(false);
this.pinnedScripts.Remove(script.Handle);
}
}
/// <summary>
/// Asynchronously adds a binding to a callback method that will raise an event when the named
/// binding is called by JavaScript executing in the browser.
/// </summary>
/// <param name="bindingName">The name of the callback that will trigger events when called by JavaScript executing in the browser.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task AddScriptCallbackBinding(string bindingName)
{
if (!this.bindings.Add(bindingName))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "A binding named {0} has already been added", bindingName));
}
await this.EnableDomains().ConfigureAwait(false);
await this.session.Value.Domains.JavaScript.AddBinding(bindingName).ConfigureAwait(false);
}
/// <summary>
/// Asynchronously removes a binding to a JavaScript callback.
/// </summary>
/// <param name="bindingName">The name of the callback to be removed.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task RemoveScriptCallbackBinding(string bindingName)
{
await this.session.Value.Domains.JavaScript.RemoveBinding(bindingName).ConfigureAwait(false);
_ = this.bindings.Remove(bindingName);
}
/// <summary>
/// Asynchronously removes all bindings to JavaScript callbacks.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task ClearScriptCallbackBindings()
{
// Use a copy of the list to prevent the iterator from becoming invalid
// when we modify the collection.
List<string> bindingList = new List<string>(this.bindings);
foreach (string binding in bindingList)
{
await this.RemoveScriptCallbackBinding(binding).ConfigureAwait(false);
}
}
/// <summary>
/// Asynchronously removes all bindings to JavaScript callbacks, all
/// initialization scripts from being loaded for each document, and unpins
/// all pinned scripts.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task ClearAll()
{
await this.ClearPinnedScripts().ConfigureAwait(false);
await this.ClearInitializationScripts().ConfigureAwait(false);
await this.ClearScriptCallbackBindings().ConfigureAwait(false);
}
/// <summary>
/// Asynchronously removes all bindings to JavaScript callbacks, all
/// initialization scripts from being loaded for each document, all
/// pinned scripts, and stops listening for events.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task Reset()
{
this.StopEventMonitoring();
await ClearAll().ConfigureAwait(false);
}
/// <summary>
/// Releases all resources associated with this <see cref="JavaScriptEngine"/>.
/// </summary>
public void Dispose()
{
this.Dispose(true);
}
/// <summary>
/// Releases all resources associated with this <see cref="JavaScriptEngine"/>.
/// </summary>
/// <param name="disposing"><see langword="true"/> if the Dispose method was explicitly called; otherwise, <see langword="false"/>.</param>
protected virtual void Dispose(bool disposing)
{
if (!this.isDisposed)
{
if (disposing)
{
if (this.session.IsValueCreated)
{
this.session.Value.Dispose();
}
}
this.isDisposed = true;
}
}
private async Task ClearPinnedScripts()
{
// Use a copy of the list to prevent the iterator from becoming invalid
// when we modify the collection.
List<string> scriptHandles = new List<string>(this.pinnedScripts.Keys);
foreach (string scriptHandle in scriptHandles)
{
await this.UnpinScript(this.pinnedScripts[scriptHandle]).ConfigureAwait(false);
}
}
private async Task EnableDomains()
{
if (!this.isEnabled)
{
await this.session.Value.Domains.JavaScript.EnablePage().ConfigureAwait(false);
await this.session.Value.Domains.JavaScript.EnableRuntime().ConfigureAwait(false);
this.isEnabled = true;
}
}
private string GetMutationListenerScript()
{
string listenerScript = string.Empty;
using (Stream resourceStream = ResourceUtilities.GetResourceStream("mutation-listener.js", "mutation-listener.js"))
{
using (StreamReader resourceReader = new StreamReader(resourceStream))
{
listenerScript = resourceReader.ReadToEnd();
}
}
return listenerScript;
}
private void OnScriptBindingCalled(object sender, BindingCalledEventArgs e)
{
if (e.Name == MonitorBindingName)
{
DomMutationData valueChangeData = JsonSerializer.Deserialize<DomMutationData>(e.Payload);
var locator = By.CssSelector($"*[data-__webdriver_id='{valueChangeData.TargetId}']");
valueChangeData.Element = driver.FindElements(locator).FirstOrDefault();
if (this.DomMutated != null)
{
this.DomMutated(this, new DomMutatedEventArgs()
{
AttributeData = valueChangeData
});
}
}
if (this.JavaScriptCallbackExecuted != null)
{
this.JavaScriptCallbackExecuted(this, new JavaScriptCallbackExecutedEventArgs()
{
ScriptPayload = e.Payload,
BindingName = e.Name
});
}
}
private void OnJavaScriptExceptionThrown(object sender, ExceptionThrownEventArgs e)
{
if (this.JavaScriptExceptionThrown != null)
{
this.JavaScriptExceptionThrown(this, new JavaScriptExceptionThrownEventArgs()
{
Message = e.Message
});
}
}
private void OnConsoleApiCalled(object sender, ConsoleApiCalledEventArgs e)
{
if (this.JavaScriptConsoleApiCalled != null)
{
for (int i = 0; i < e.Arguments.Count; i++)
{
this.JavaScriptConsoleApiCalled(this, new JavaScriptConsoleApiCalledEventArgs()
{
MessageContent = e.Arguments[i].Value,
MessageTimeStamp = e.Timestamp,
MessageType = e.Type
});
}
}
}
}
}