|
| 1 | +// <copyright file="V106JavaScript.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 | + |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using OpenQA.Selenium.DevTools.V106.Page; |
| 22 | +using OpenQA.Selenium.DevTools.V106.Runtime; |
| 23 | + |
| 24 | +namespace OpenQA.Selenium.DevTools.V106 |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// Class containing the JavaScript implementation for version 106 of the DevTools Protocol. |
| 28 | + /// </summary> |
| 29 | + public class V106JavaScript : JavaScript |
| 30 | + { |
| 31 | + private RuntimeAdapter runtime; |
| 32 | + private PageAdapter page; |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes a new instance of the <see cref="V106JavaScript"/> class. |
| 36 | + /// </summary> |
| 37 | + /// <param name="runtime">The DevTools Protocol adapter for the Runtime domain.</param> |
| 38 | + /// <param name="page">The DevTools Protocol adapter for the Page domain.</param> |
| 39 | + public V106JavaScript(RuntimeAdapter runtime, PageAdapter page) |
| 40 | + { |
| 41 | + this.runtime = runtime; |
| 42 | + this.page = page; |
| 43 | + this.runtime.BindingCalled += OnRuntimeBindingCalled; |
| 44 | + this.runtime.ConsoleAPICalled += OnRuntimeConsoleApiCalled; |
| 45 | + this.runtime.ExceptionThrown += OnRuntimeExceptionThrown; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Asynchronously enables the Runtime domain in the DevTools Protocol. |
| 50 | + /// </summary> |
| 51 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 52 | + public override async Task EnableRuntime() |
| 53 | + { |
| 54 | + await runtime.Enable(); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Asynchronously disables the Runtime domain in the DevTools Protocol. |
| 59 | + /// </summary> |
| 60 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 61 | + public override async Task DisableRuntime() |
| 62 | + { |
| 63 | + await runtime.Disable(); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Asynchronously enables the Page domain in the DevTools Protocol. |
| 68 | + /// </summary> |
| 69 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 70 | + public override async Task EnablePage() |
| 71 | + { |
| 72 | + await page.Enable(); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Asynchronously disables the Page domain in the DevTools Protocol. |
| 77 | + /// </summary> |
| 78 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 79 | + public override async Task DisablePage() |
| 80 | + { |
| 81 | + await page.Disable(); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Adds a binding to a specific JavaScript name. |
| 86 | + /// </summary> |
| 87 | + /// <param name="name">The name to which to bind to.</param> |
| 88 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 89 | + public override async Task AddBinding(string name) |
| 90 | + { |
| 91 | + await runtime.AddBinding(new AddBindingCommandSettings() { Name = name }); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Removes a binding from a specific JavaScript name. |
| 96 | + /// </summary> |
| 97 | + /// <param name="name">The name to which to remove the bind from.</param> |
| 98 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 99 | + public override async Task RemoveBinding(string name) |
| 100 | + { |
| 101 | + await runtime.RemoveBinding(new RemoveBindingCommandSettings() { Name = name }); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Adds a JavaScript snippet to evaluate when a new document is opened. |
| 106 | + /// </summary> |
| 107 | + /// <param name="script">The script to add to be evaluated when a new document is opened.</param> |
| 108 | + /// <returns>A task that represents the asynchronous operation. The task result contains the internal ID of the script.</returns> |
| 109 | + public override async Task<string> AddScriptToEvaluateOnNewDocument(string script) |
| 110 | + { |
| 111 | + var result = await page.AddScriptToEvaluateOnNewDocument(new AddScriptToEvaluateOnNewDocumentCommandSettings() { Source = script }); |
| 112 | + return result.Identifier; |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Removes a JavaScript snippet from evaluate when a new document is opened. |
| 117 | + /// </summary> |
| 118 | + /// <param name="scriptId">The ID of the script to be removed.</param> |
| 119 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 120 | + public override async Task RemoveScriptToEvaluateOnNewDocument(string scriptId) |
| 121 | + { |
| 122 | + await page.RemoveScriptToEvaluateOnNewDocument(new RemoveScriptToEvaluateOnNewDocumentCommandSettings() { Identifier = scriptId }); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Evaluates a JavaScript snippet. It does not return a value. |
| 127 | + /// </summary> |
| 128 | + /// <param name="script">The script to evaluate</param> |
| 129 | + /// <returns>A task that represents the asynchronous operation.</returns> |
| 130 | + /// <remarks> |
| 131 | + /// This method is internal to the operation of pinned scripts in Selenium, and |
| 132 | + /// is therefore internal by design. |
| 133 | + /// </remarks> |
| 134 | + internal override async Task Evaluate(string script) |
| 135 | + { |
| 136 | + await runtime.Evaluate(new EvaluateCommandSettings { Expression = script }); |
| 137 | + } |
| 138 | + |
| 139 | + private void OnRuntimeBindingCalled(object sender, Runtime.BindingCalledEventArgs e) |
| 140 | + { |
| 141 | + BindingCalledEventArgs wrapped = new BindingCalledEventArgs() |
| 142 | + { |
| 143 | + ExecutionContextId = e.ExecutionContextId, |
| 144 | + Name = e.Name, |
| 145 | + Payload = e.Payload |
| 146 | + }; |
| 147 | + |
| 148 | + this.OnBindingCalled(wrapped); |
| 149 | + } |
| 150 | + |
| 151 | + private void OnRuntimeExceptionThrown(object sender, Runtime.ExceptionThrownEventArgs e) |
| 152 | + { |
| 153 | + // TODO: Collect stack trace elements |
| 154 | + var wrapped = new ExceptionThrownEventArgs() |
| 155 | + { |
| 156 | + Timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp), |
| 157 | + Message = e.ExceptionDetails.Text |
| 158 | + }; |
| 159 | + |
| 160 | + this.OnExceptionThrown(wrapped); |
| 161 | + } |
| 162 | + |
| 163 | + private void OnRuntimeConsoleApiCalled(object sender, ConsoleAPICalledEventArgs e) |
| 164 | + { |
| 165 | + List<ConsoleApiArgument> args = new List<ConsoleApiArgument>(); |
| 166 | + foreach (var arg in e.Args) |
| 167 | + { |
| 168 | + string argValue = null; |
| 169 | + if (arg.Value != null) |
| 170 | + { |
| 171 | + argValue = arg.Value.ToString(); |
| 172 | + } |
| 173 | + args.Add(new ConsoleApiArgument() { Type = arg.Type.ToString(), Value = argValue }); |
| 174 | + } |
| 175 | + |
| 176 | + var wrapped = new ConsoleApiCalledEventArgs() |
| 177 | + { |
| 178 | + Timestamp = new DateTime(1979, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp), |
| 179 | + Type = e.Type, |
| 180 | + Arguments = args.AsReadOnly() |
| 181 | + }; |
| 182 | + |
| 183 | + this.OnConsoleApiCalled(wrapped); |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments