Skip to content

Commit 4088ef7

Browse files
committed
[.NET] Add ability to listen for JavaScript exceptions and callbacks
1 parent 9246b42 commit 4088ef7

6 files changed

+586
-0
lines changed
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// <copyright file="IJavaScriptEngineManager.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Threading.Tasks;
22+
23+
namespace OpenQA.Selenium
24+
{
25+
/// <summary>
26+
/// Defines an interface allowing the user to manage settings in the browser's JavaScript engine.
27+
/// </summary>
28+
public interface IJavaScriptEngine
29+
{
30+
/// <summary>
31+
/// Occurs when a JavaScript callback with a named binding is executed.
32+
/// </summary>
33+
event EventHandler<JavaScriptCallbackExecutedEventArgs> JavaScriptCallbackExecuted;
34+
35+
/// <summary>
36+
/// Occurs when an exeception is thrown by JavaScript being executed in the browser.
37+
/// </summary>
38+
event EventHandler<JavaScriptExceptionThrownEventArgs> JavaScriptExceptionThrown;
39+
40+
/// <summary>
41+
/// Occurs when methods on the JavaScript console are called.
42+
/// </summary>
43+
event EventHandler<JavaScriptConsoleApiCalledEventArgs> JavaScriptConsoleApiCalled;
44+
45+
/// <summary>
46+
/// Gets the read-only list of initialization scripts added for this JavaScript engine.
47+
/// </summary>
48+
IReadOnlyList<InitializationScript> InitializationScripts { get; }
49+
50+
/// <summary>
51+
/// Gets the read-only list of binding callbacks added for this JavaScript engine.
52+
/// </summary>
53+
IReadOnlyList<string> ScriptCallbackBindings { get; }
54+
55+
/// <summary>
56+
/// Asynchronously starts monitoring for events from the browser's JavaScript engine.
57+
/// </summary>
58+
/// <returns>A task that represents the asynchronous operation.</returns>
59+
Task StartEventMonitoring();
60+
61+
/// <summary>
62+
/// Stops monitoring for events from the browser's JavaScript engine.
63+
/// </summary>
64+
void StopEventMonitoring();
65+
66+
/// <summary>
67+
/// Asynchronously adds JavaScript to be loaded on every document load.
68+
/// </summary>
69+
/// <param name="scriptName">The friendly name by which to refer to this initialization script.</param>
70+
/// <param name="script">The JavaScript to be loaded on every page.</param>
71+
/// <returns>A task containing an <see cref="InitializationScript"/> object representing the script to be loaded on each page.</returns>
72+
Task<InitializationScript> AddInitializationScript(string scriptName, string script);
73+
74+
/// <summary>
75+
/// Asynchronously removes JavaScript from being loaded on every document load.
76+
/// </summary>
77+
/// <param name="scriptName">The friendly name of the initialization script to be removed.</param>
78+
/// <returns>A task that represents the asynchronous operation.</returns>
79+
Task RemoveInitializationScript(string scriptName);
80+
81+
/// <summary>
82+
/// Asynchronously removes all intialization scripts from being
83+
/// loaded on every document load.
84+
/// </summary>
85+
/// <returns>A task that represents the asynchronous operation.</returns>
86+
Task ClearInitializationScripts();
87+
88+
/// <summary>
89+
/// Asynchronously adds a binding to a callback method that will raise
90+
/// an event when the named binding is called by JavaScript executing
91+
/// in the browser.
92+
/// </summary>
93+
/// <param name="bindingName">The name of the callback that will trigger events when called by JavaScript executing in the browser.</param>
94+
/// <returns>A task that represents the asynchronous operation.</returns>
95+
Task AddScriptCallbackBinding(string bindingName);
96+
97+
/// <summary>
98+
/// Asynchronously removes a binding to a JavaScript callback.
99+
/// </summary>
100+
/// <param name="bindingName">The name of the callback to be removed.</param>
101+
/// <returns>A task that represents the asynchronous operation.</returns>
102+
Task RemoveScriptCallbackBinding(string bindingName);
103+
104+
/// <summary>
105+
/// Asynchronously removes all bindings to JavaScript callbacks.
106+
/// </summary>
107+
/// <returns>A task that represents the asynchronous operation.</returns>
108+
Task ClearScriptCallbackBindings();
109+
110+
/// <summary>
111+
/// Asynchronously removes all bindings to JavaScript callbacks and all
112+
/// initialization scripts from being loaded for each document.
113+
/// </summary>
114+
/// <returns>A task that represents the asynchronous operation.</returns>
115+
Task ClearAll();
116+
117+
/// <summary>
118+
/// Asynchronously removes all bindings to JavaScript callbacks, all
119+
/// initialization scripts from being loaded for each document, and
120+
/// stops listening for events.
121+
/// </summary>
122+
/// <returns>A task that represents the asynchronous operation.</returns>
123+
Task Reset();
124+
}
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// <copyright file="InitializationScript.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System.Globalization;
20+
21+
namespace OpenQA.Selenium
22+
{
23+
/// <summary>
24+
/// Represents a JavaScript script that is loaded and run on every document load.
25+
/// </summary>
26+
public class InitializationScript
27+
{
28+
/// <summary>
29+
/// Gets the internal ID of the initialization script.
30+
/// </summary>
31+
public string ScriptId { get; internal set; }
32+
33+
/// <summary>
34+
/// Gets the friendly name of the initialization script.
35+
/// </summary>
36+
public string ScriptName { get; internal set; }
37+
38+
/// <summary>
39+
/// Gets the JavaScript source of the initialization script.
40+
/// </summary>
41+
public string ScriptSource { get; internal set; }
42+
43+
/// <summary>
44+
/// Returns a string that represents the current object.
45+
/// </summary>
46+
/// <returns>A string that represents the current object.</returns>
47+
public override string ToString()
48+
{
49+
return string.Format(CultureInfo.InvariantCulture, "Initialization Script '{0}'\nInternal ID: {1}\nSource:{2}", this.ScriptName, this.ScriptId, this.ScriptSource);
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// <copyright file="JavaScriptCallbackExecutedEventArgs.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
21+
namespace OpenQA.Selenium
22+
{
23+
/// <summary>
24+
/// Provides data for the JavaScriptCallbackExecuted event.
25+
/// </summary>
26+
public class JavaScriptCallbackExecutedEventArgs : EventArgs
27+
{
28+
private string bindingName;
29+
private string payload;
30+
31+
/// <summary>
32+
/// Gets or sets the binding name of the JavaScript callback that was execute.
33+
/// </summary>
34+
public string BindingName { get => bindingName; set => bindingName = value; }
35+
36+
/// <summary>
37+
/// Gets or sets the payload sent from the JavaScript callback.
38+
/// </summary>
39+
public string ScriptPayload { get => payload; set => payload = value; }
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <copyright file="JavaScriptConsoleApiCalledEventArgs.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
21+
namespace OpenQA.Selenium
22+
{
23+
/// <summary>
24+
/// Provides data for the JavaScriptConsoleApiCalled event.
25+
/// </summary>
26+
public class JavaScriptConsoleApiCalledEventArgs : EventArgs
27+
{
28+
private string messageContent;
29+
private DateTime messageTimeStamp;
30+
private string messageType;
31+
32+
/// <summary>
33+
/// Gets or sets the content of the message written to the JavaScript console
34+
/// </summary>
35+
public string MessageContent { get => messageContent; set => messageContent = value; }
36+
37+
/// <summary>
38+
/// Gets or sets the time stamp of the message written to the JavaScript console.
39+
/// </summary>
40+
public DateTime MessageTimeStamp { get => messageTimeStamp; set => messageTimeStamp = value; }
41+
42+
/// <summary>
43+
/// Gets or sets the type of message written to the JavaScript console.
44+
/// </summary>
45+
public string MessageType { get => messageType; set => messageType = value; }
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// <copyright file="JavaScriptExceptionThrownEventArgs.cs" company="WebDriver Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
21+
namespace OpenQA.Selenium
22+
{
23+
/// <summary>
24+
/// Provides data for the JavaScriptExceptionThrown event.
25+
/// </summary>
26+
public class JavaScriptExceptionThrownEventArgs : EventArgs
27+
{
28+
private string message;
29+
30+
/// <summary>
31+
/// Gets or sets the message of the exception thrown by JavaScript executing in the browser.
32+
/// </summary>
33+
public string Message { get => message; set => message = value; }
34+
}
35+
}

0 commit comments

Comments
 (0)