Skip to content

Commit

Permalink
[dotnet] Add CDP support for Chromium 95
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Oct 12, 2021
1 parent 7f29755 commit 9b1d5f2
Show file tree
Hide file tree
Showing 7 changed files with 788 additions and 0 deletions.
1 change: 1 addition & 0 deletions dotnet/selenium-dotnet-version.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ SUPPORTED_DEVTOOLS_VERSIONS = [
"v85",
"v93",
"v94",
"v95",
]

ASSEMBLY_COMPANY = "Selenium Committers"
Expand Down
1 change: 1 addition & 0 deletions dotnet/src/webdriver/DevTools/DevToolsDomains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class DevToolsDomains
// added to this dictionary.
private static readonly Dictionary<int, Type> SupportedDevToolsVersions = new Dictionary<int, Type>()
{
{ 95, typeof(V95.V95Domains) },
{ 94, typeof(V94.V94Domains) },
{ 93, typeof(V93.V93Domains) },
{ 85, typeof(V85.V85Domains) }
Expand Down
66 changes: 66 additions & 0 deletions dotnet/src/webdriver/DevTools/v95/V95Domains.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <copyright file="V95Domains.cs" company="WebDriver 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.

using System;
using System.Collections.Generic;
using System.Text;

namespace OpenQA.Selenium.DevTools.V95
{
/// <summary>
/// Class containing the domain implementation for version 94 of the DevTools Protocol.
/// </summary>
public class V95Domains : DevToolsDomains
{
private DevToolsSessionDomains domains;

public V95Domains(DevToolsSession session)
{
this.domains = new DevToolsSessionDomains(session);
}

/// <summary>
/// Gets the DevTools Protocol version for which this class is valid.
/// </summary>
public static int DevToolsVersion => 95;

/// <summary>
/// Gets the version-specific domains for the DevTools session. This value must be cast to a version specific type to be at all useful.
/// </summary>
public override DevTools.DevToolsSessionDomains VersionSpecificDomains => this.domains;

/// <summary>
/// Gets the object used for manipulating network information in the browser.
/// </summary>
public override DevTools.Network Network => new V95Network(domains.Network, domains.Fetch);

/// <summary>
/// Gets the object used for manipulating the browser's JavaScript execution.
/// </summary>
public override JavaScript JavaScript => new V95JavaScript(domains.Runtime, domains.Page);

/// <summary>
/// Gets the object used for manipulating DevTools Protocol targets.
/// </summary>
public override DevTools.Target Target => new V95Target(domains.Target);

/// <summary>
/// Gets the object used for manipulating the browser's logs.
/// </summary>
public override DevTools.Log Log => new V95Log(domains.Log);
}
}
186 changes: 186 additions & 0 deletions dotnet/src/webdriver/DevTools/v95/V95JavaScript.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// <copyright file="V95JavaScript.cs" company="WebDriver 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.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using OpenQA.Selenium.DevTools.V95.Page;
using OpenQA.Selenium.DevTools.V95.Runtime;

namespace OpenQA.Selenium.DevTools.V95
{
/// <summary>
/// Class containing the JavaScript implementation for version 89 of the DevTools Protocol.
/// </summary>
public class V95JavaScript : JavaScript
{
private RuntimeAdapter runtime;
private PageAdapter page;

/// <summary>
/// Initializes a new instance of the <see cref="V95JavaScript"/> class.
/// </summary>
/// <param name="runtime">The DevTools Protocol adapter for the Runtime domain.</param>
/// <param name="page">The DevTools Protocol adapter for the Page domain.</param>
public V95JavaScript(RuntimeAdapter runtime, PageAdapter page)
{
this.runtime = runtime;
this.page = page;
this.runtime.BindingCalled += OnRuntimeBindingCalled;
this.runtime.ConsoleAPICalled += OnRuntimeConsoleApiCalled;
this.runtime.ExceptionThrown += OnRuntimeExceptionThrown;
}

/// <summary>
/// Asynchronously enables the Runtime domain in the DevTools Protocol.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task EnableRuntime()
{
await runtime.Enable();
}

/// <summary>
/// Asynchronously disables the Runtime domain in the DevTools Protocol.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DisableRuntime()
{
await runtime.Disable();
}

/// <summary>
/// Asynchronously enables the Page domain in the DevTools Protocol.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task EnablePage()
{
await page.Enable();
}

/// <summary>
/// Asynchronously disables the Page domain in the DevTools Protocol.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task DisablePage()
{
await page.Disable();
}

/// <summary>
/// Adds a binding to a specific JavaScript name.
/// </summary>
/// <param name="name">The name to which to bind to.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task AddBinding(string name)
{
await runtime.AddBinding(new AddBindingCommandSettings() { Name = name });
}

/// <summary>
/// Removes a binding from a specific JavaScript name.
/// </summary>
/// <param name="name">The name to which to remove the bind from.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task RemoveBinding(string name)
{
await runtime.RemoveBinding(new RemoveBindingCommandSettings() { Name = name });
}

/// <summary>
/// Adds a JavaScript snippet to evaluate when a new document is opened.
/// </summary>
/// <param name="script">The script to add to be evaluated when a new document is opened.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the internal ID of the script.</returns>
public override async Task<string> AddScriptToEvaluateOnNewDocument(string script)
{
var result = await page.AddScriptToEvaluateOnNewDocument(new AddScriptToEvaluateOnNewDocumentCommandSettings() { Source = script });
return result.Identifier;
}

/// <summary>
/// Removes a JavaScript snippet from evaluate when a new document is opened.
/// </summary>
/// <param name="scriptId">The ID of the script to be removed.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task RemoveScriptToEvaluateOnNewDocument(string scriptId)
{
await page.RemoveScriptToEvaluateOnNewDocument(new RemoveScriptToEvaluateOnNewDocumentCommandSettings() { Identifier = scriptId });
}

/// <summary>
/// Evaluates a JavaScript snippet. It does not return a value.
/// </summary>
/// <param name="script">The script to evaluate</param>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <remarks>
/// This method is internal to the operation of pinned scripts in Selenium, and
/// is therefore internal by design.
/// </remarks>
internal override async Task Evaluate(string script)
{
await runtime.Evaluate(new EvaluateCommandSettings { Expression = script });
}

private void OnRuntimeBindingCalled(object sender, Runtime.BindingCalledEventArgs e)
{
BindingCalledEventArgs wrapped = new BindingCalledEventArgs()
{
ExecutionContextId = e.ExecutionContextId,
Name = e.Name,
Payload = e.Payload
};

this.OnBindingCalled(wrapped);
}

private void OnRuntimeExceptionThrown(object sender, Runtime.ExceptionThrownEventArgs e)
{
// TODO: Collect stack trace elements
var wrapped = new ExceptionThrownEventArgs()
{
Timestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
Message = e.ExceptionDetails.Text
};

this.OnExceptionThrown(wrapped);
}

private void OnRuntimeConsoleApiCalled(object sender, ConsoleAPICalledEventArgs e)
{
List<ConsoleApiArgument> args = new List<ConsoleApiArgument>();
foreach (var arg in e.Args)
{
string argValue = null;
if (arg.Value != null)
{
argValue = arg.Value.ToString();
}
args.Add(new ConsoleApiArgument() { Type = arg.Type.ToString(), Value = argValue });
}

var wrapped = new ConsoleApiCalledEventArgs()
{
Timestamp = new DateTime(1979, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(e.Timestamp),
Type = e.Type,
Arguments = args.AsReadOnly()
};

this.OnConsoleApiCalled(wrapped);
}
}
}
80 changes: 80 additions & 0 deletions dotnet/src/webdriver/DevTools/v95/V95Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// <copyright file="V95Log.cs" company="WebDriver 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.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium.DevTools.V95.Log;

namespace OpenQA.Selenium.DevTools.V95
{
/// <summary>
/// Class containing the browser's log as referenced by version 89 of the DevTools Protocol.
/// </summary>
public class V95Log : DevTools.Log
{
private LogAdapter adapter;

/// <summary>
/// Initializes a new instance of the <see cref="V95Log"/> class.
/// </summary>
/// <param name="adapter">The adapter for the Log domain.</param>
public V95Log(LogAdapter adapter)
{
this.adapter = adapter;
this.adapter.EntryAdded += OnAdapterEntryAdded;
}

/// <summary>
/// Asynchronously enables manipulation of the browser's log.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task Enable()
{
await adapter.Enable();
}

/// <summary>
/// Asynchronously disables manipulation of the browser's log.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task Disable()
{
await adapter.Disable();
}

/// <summary>
/// Asynchronously clears the browser's log.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public override async Task Clear()
{
await adapter.Clear();
}

private void OnAdapterEntryAdded(object sender, Log.EntryAddedEventArgs e)
{
EntryAddedEventArgs propagated = new EntryAddedEventArgs();
propagated.Entry = new LogEntry();
propagated.Entry.Kind = e.Entry.Source.ToString();
propagated.Entry.Message = e.Entry.Text;
this.OnEntryAdded(propagated);
}
}
}
Loading

0 comments on commit 9b1d5f2

Please sign in to comment.