A runtime library and schema code generation tools for Chrome DevTools Protocol support in C#/.NET.
- Asynchronous and synchronous APIs for commands execution
- Disposable event subscriptions, one-time subscriptions
- Code-generation of domains, commands, events, types from protocol's JSON schema
- Multiple CDP schema files support in generation pipeline with domains definitions merging
- .NET Standard 2.0 compatible
ChromeProtocol.Runtime
: contains all necessary run-time code to work through CDP.ChromeProtocol.Domains
: contains pre-generated classes representing official CDP domains set.
// A port should be the one used in --remote-debugging-port argument when launching Chrome
var debuggingEndpoint = new Uri("ws://127.0.0.1:1234");
var browserClient = new DefaultProtocolClient(debuggingEndpoint, new ConsoleLogger(...));
var pageClient = browserClient.CreateScoped(sessionId);
// Send and wait for response
var targets = await browserClient.SendCommandAsync(Domains.Target.GetTargets());
await pageClient.SendCommandAsync(Domains.Debugger.Enable());
// Just send and resume execution immediately
await pageClient.FireCommandAsync(Domains.Runtime.Evaluate("alert('Hello there')"));
pageClient.SubscribeAsync<Domains.Debugger.Paused>(paused => {
Console.WriteLine("paused called");
})
// Subscriptions are disposable
var subscription = pageClient.SubscribeAsync<Domains.Target.TargetInfoChanged>(changed => ...);
...
subscription.Dispose();
// Single use subscription
var subscription = pageClient.SubscribeOnce<Domains.Page.FrameNavigated>(navigated => {
InitializeSomeStuff();
});
In case pre-generated domains from ChromeProtocol.Domains
package are not enough for your use case, you can generate code by your own schema files.
To do so:
- Install
dotnet cdp
command line into your solution/project tools viadotnet tool install dotnet-cdp
(ordotnet tool install -g dotnet-cdp
to install tools globally) - Prepare schema files. Chrome browser & JS schema could be obtained here.
- Execute generate command:
> dotnet cdp generate js_protocol.json browser_protocol.json --namespace YourApp.Domains --output YourApp.Domains/SomeFolder/Generated
dotnet cdp --help
will guide you with all available options:> dotnet cdp generate --help DESCRIPTION: Generates strongly-typed C# classes for domain types, events and commands from protocol definition files to be used with ChromeProtocol. USAGE: dotnet cdp generate <path> [OPTIONS] EXAMPLES: dotnet cdp generate js_protocol.json mono_protocol.json -n DevTools.Api.Generated -o ./out ARGUMENTS: <path> Path to the .json file with the CDP schema to generate protocol definitions from OPTIONS: DEFAULT -h, --help Prints help information -n, --namespace Generated Namespace for the generated files -o, --output Generated Folder where generated files should be placed --clean True Should output folder be cleaned before performing generation or not
.NET SDK 8.0 or newer
dotnet build
from solution folder
Just run dotnet test
:
dotnet test ChromeProtocol.Runtime.Tests
dotnet test ChromeProtocol.Tools.Tests