Files
cefsharp/CefSharp.Example/DevTools/DevToolsExtensions.cs
T
Alex Maitland 78e32a3b9f Enhancement - Add DevToolsClient (#3229)
* 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
2020-09-21 19:32:25 +10:00

58 lines
1.9 KiB
C#

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace CefSharp.Example.DevTools
{
public static class DevToolsExtensions
{
/// <summary>
/// Calls Page.captureScreenshot without any optional params
/// (Results in PNG image of default viewport)
/// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot
/// </summary>
/// <param name="browser">the ChromiumWebBrowser</param>
/// <returns>png encoded image as byte[]</returns>
public static async Task<byte[]> CaptureScreenShotAsPng(this IWebBrowser chromiumWebBrowser)
{
//if (!browser.HasDocument)
//{
// throw new System.Exception("Page hasn't loaded");
//}
var browser = chromiumWebBrowser.GetBrowser();
if (browser == null || browser.IsDisposed)
{
throw new Exception("browser is Null or Disposed");
}
//var param = new Dictionary<string, object>
//{
// { "format", "png" },
//}
//Make sure to dispose of our observer registration when done
using (var devToolsClient = browser.GetDevToolsClient())
{
const string methodName = "Page.captureScreenshot";
var result = await devToolsClient.ExecuteDevToolsMethodAsync(methodName);
dynamic response = JsonConvert.DeserializeObject<dynamic>(result.ResponseAsJsonString);
//Success
if (result.Success)
{
return Convert.FromBase64String((string)response.data);
}
var code = (string)response.code;
var message = (string)response.message;
throw new Exception(code + ":" + message);
}
}
}
}