Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement - Add DevToolsClient (Direct Devtools Protocol) #3165

Closed
3 tasks done
amaitland opened this issue Jul 2, 2020 · 7 comments · Fixed by #3229
Closed
3 tasks done

Enhancement - Add DevToolsClient (Direct Devtools Protocol) #3165

amaitland opened this issue Jul 2, 2020 · 7 comments · Fixed by #3229
Assignees
Milestone

Comments

@amaitland
Copy link
Member

amaitland commented Jul 2, 2020

Follow up to #3158

A nicer API build on top of the CEF API to make it easier to send/receive messages from DevTools.

The API is generated on top of the DevTools protocol to create a set of model classes to simplify executing DevTools commands.

  • Add Strongly typed DevTools Domain classes
  • Deprecated methods are not generated
  • Only methods (no strongly typed events yet, a new issue should be created for that.)

The CefSharp.DevTools.DevToolsClient class is the primary method of accessing the different domains. Each DevTools Domain has it's own namespace that contains enums and strongly typed return classes.

Network Domain is part of the CefSharp.DevTools.Network namespace. The methods are accessibly though the DevToolsClient class.

Some basic examples look like (extract from the unit tests)

using (var browser = new ChromiumWebBrowser("www.google.com"))
{
	await browser.WaitForInitialLoadAsync();

	using (var devToolsClient = browser.GetDevToolsClient())
	{
		var response = await devToolsClient.Network.SetCookieAsync(name, value, domain: domain, sameSite: sameSite);
	}
}

using (var browser = new ChromiumWebBrowser("www.google.com"))
{
	await browser.WaitForInitialLoadAsync();

	using (var devToolsClient = browser.GetDevToolsClient())
	{
		var response = await devToolsClient.Browser.GetVersionAsync();
		var jsVersion = response.JsVersion;
		var revision = response.Revision;
	}
}
@amaitland amaitland added this to the 85.4.x milestone Jul 2, 2020
@amaitland amaitland changed the title Enhancement - Add DevToolsManager (Direct Devtools Protocol) Enhancement - Add DevToolsClient (Direct Devtools Protocol) Aug 19, 2020
amaitland added a commit to amaitland/CefSharp that referenced this issue Aug 26, 2020
@amaitland amaitland self-assigned this Sep 16, 2020
amaitland added a commit to amaitland/CefSharp that referenced this issue Sep 18, 2020
amaitland added a commit that referenced this issue Sep 21, 2020
* Core - Add DevToolsClient

Issue #3165

* DevTools Client - Add Network domain class

- Add hand written class to get a better sense of how the code should be structured.
- Improve exception handling

* Devtools - First draft of devtools protocol methods (generated)

Not complete yet

* Devtools - Simple method execution working

* DevTools - Add .Net Style cased property names to response objects

- Use internal field and datacontract attributes

* DevTools - Improve mapping

* DevTools - Strongly typed enums

* DevTools Client - Generated from active browser instance (M84)

* Devtools Client - Extract IDevToolsClient interface for testability

Improve xml doc comments

* DevTools Client - Improve error handling

* DevTools Client - Improve enum conversion

* Revert to older c# syntax

* DevTools Client - Fix failing tests

* Devtools Client - Add SetCookie Tests

* DevTools Client - Add Capture SyncContext by default

- Add option to capture the SyncContext
- Add option to provide custom SyncContext (can just pass in UI SyncContext and all continuations will happen there).
- Fix bug in ExecuteDevToolsMethodAsync sending the command twice when exeucted on the CEF UI thread

* Devtools Client - Add missing file reference

The problem with developing with the .Net Core project where files are automatically included is you
forget to include them in the regular csproj file.

* Devtools Client - Add CanCanEmulate Test case

* DevTools Client - Add AddScriptToEvaluateOnNewDocumentAsync test

* DevTools Client - OnDevToolsEvent only parse data if event handler != null

* DevTools Client - GetDevToolsClient remove ThrowExceptionIfDisposed/ThrowExceptionIfBrowserNotInitialized calls

Those methods were removed as the GetBrowser call handles the checks for us.

* DevTools Client - Improve error handling

* DevTools Client - Update xml doc comments

* DevTools Client - Add validation method (partial)

Add ability to manually add validation logic

* DevTools Client - Comment out failing test

Was working when targeting M84, now with M85 it fails.

The mapping appears to be correct, will need to investigate further

* OffScreen Example - Remove test code
@amaitland amaitland linked a pull request Oct 6, 2020 that will close this issue
9 tasks
@amaitland
Copy link
Member Author

CEF doesn't support every aspect of DevTools, the generated API is based of the protocol definition which can be downloaded from the browser at runtime and is used for code generation. If you find a method that CEF doesn't support please post here and I will at some point update the generator to remove those methods.

TO BE REMOVED:

  • Page.printToPdf is only accessibly when using Headless Chromium and not supported.

@amaitland
Copy link
Member Author

Developing features like this takes considerably time and effort, if you use CefSharp in a commercial application then please consider sponsoring me https://github.com/cefsharp/CefSharp#financial-support

@amaitland
Copy link
Member Author

Example of changing User Agent at runtime at https://stackoverflow.com/a/64543667/4583726

@totti240282
Copy link

With GetDevToolsClient is possible capture traffic in network tab ?

@amaitland
Copy link
Member Author

Yes. You can subscribe to http://cefsharp.github.io/api/85.3.x/html/F_CefSharp_DevTools_DevToolsClient_DevToolsEvent.htm

As per #3158 (comment) you must call enable/disable for the specific domain.

@amaitland
Copy link
Member Author

For .Net core it might be worth generating some nicer code in relation to the JSON parsing involved here. Using https://docs.microsoft.com/en-us/dotnet/api/system.text.json?view=net-5.0 should be feasible.

@amaitland
Copy link
Member Author

See https://gist.github.com/amaitland/eabb42a191c4943610f2407196b0e03a for an example of changing tge user agent at runtime using the DevTools protocol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants