Skip to content

Commit

Permalink
[dotnet] add cdp v106 remove v103
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Sep 28, 2022
1 parent a2b161a commit 3170a49
Show file tree
Hide file tree
Showing 19 changed files with 948 additions and 153 deletions.
2 changes: 1 addition & 1 deletion dotnet/selenium-dotnet-version.bzl
Expand Up @@ -7,9 +7,9 @@ SUPPORTED_NET_STANDARD_VERSIONS = ["netstandard2.0", "netstandard2.1", "net5.0"]

SUPPORTED_DEVTOOLS_VERSIONS = [
"v85",
"v103",
"v104",
"v105",
"v106",
]

ASSEMBLY_COMPANY = "Selenium Committers"
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/DevTools/DevToolsDomains.cs
Expand Up @@ -37,9 +37,9 @@ public abstract class DevToolsDomains
// added to this dictionary.
private static readonly Dictionary<int, Type> SupportedDevToolsVersions = new Dictionary<int, Type>()
{
{ 106, typeof(V106.V106Domains) },
{ 105, typeof(V105.V105Domains) },
{ 104, typeof(V104.V104Domains) },
{ 103, typeof(V103.V103Domains) },
{ 85, typeof(V85.V85Domains) }
};

Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/webdriver/DevTools/DevToolsSession.cs
Expand Up @@ -31,12 +31,12 @@
namespace OpenQA.Selenium.DevTools
{
/// <summary>
/// Represents a WebSocket connection to a running DevTools instance that can be used to send
/// Represents a WebSocket connection to a running DevTools instance that can be used to send
/// commands and recieve events.
///</summary>
public class DevToolsSession : IDevToolsSession
{
public const int AutoDetectDevToolsProtocolVersion = 0;
public const int AutoDetectDevToolsProtocolVersion = 106;

private readonly string debuggerEndpoint;
private string websocketAddress;
Expand Down
66 changes: 66 additions & 0 deletions dotnet/src/webdriver/DevTools/v106/V106Domains.cs
@@ -0,0 +1,66 @@
// <copyright file="V106Domains.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.V106
{
/// <summary>
/// Class containing the domain implementation for version 106 of the DevTools Protocol.
/// </summary>
public class V106Domains : DevToolsDomains
{
private DevToolsSessionDomains domains;

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

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

/// <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 V106Network(domains.Network, domains.Fetch);

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

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

/// <summary>
/// Gets the object used for manipulating the browser's logs.
/// </summary>
public override DevTools.Log Log => new V106Log(domains.Log);
}
}
186 changes: 186 additions & 0 deletions dotnet/src/webdriver/DevTools/v106/V106JavaScript.cs
@@ -0,0 +1,186 @@
// <copyright file="V106JavaScript.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.V106.Page;
using OpenQA.Selenium.DevTools.V106.Runtime;

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

/// <summary>
/// Initializes a new instance of the <see cref="V106JavaScript"/> 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 V106JavaScript(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/v106/V106Log.cs
@@ -0,0 +1,80 @@
// <copyright file="V106Log.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.V106.Log;

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

/// <summary>
/// Initializes a new instance of the <see cref="V106Log"/> class.
/// </summary>
/// <param name="adapter">The adapter for the Log domain.</param>
public V106Log(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);
}
}
}

0 comments on commit 3170a49

Please sign in to comment.