public void CanRemoveEventListenerBeforeAddingOne() { using (var devToolsClient = new DevToolsClient(null)) { devToolsClient.Network.RequestWillBeSent -= (sender, args) => { }; } }
/// <summary> /// Gets a new Instance of the DevTools client /// </summary> /// <param name="browser">the IBrowser instance</param> /// <returns>DevToolsClient</returns> public static DevToolsClient GetDevToolsClient(this IBrowser browser) { var browserHost = browser.GetHost(); WebBrowserExtensions.ThrowExceptionIfBrowserHostNull(browserHost); var devToolsClient = new DevToolsClient(browser); var observerRegistration = browserHost.AddDevToolsMessageObserver(devToolsClient); devToolsClient.SetDevToolsObserverRegistration(observerRegistration); return(devToolsClient); }
/// <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 // IDevToolsClient tmp = browser.GetDevToolsClient(); DevToolsClient devToolsClient = browser.GetDevToolsClient(); using (devToolsClient) { 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); } }
public void IsIEventProxyRemovedFromConcurrentDictionary() { const string eventName = "Browser.downloadProgress"; using (var devToolsClient = new DevToolsClient(null)) { EventHandler <DownloadProgressEventArgs> eventHandler1 = (object sender, DownloadProgressEventArgs args) => { }; EventHandler <DownloadProgressEventArgs> eventHandler2 = (object sender, DownloadProgressEventArgs args) => { }; devToolsClient.AddEventHandler(eventName, eventHandler1); devToolsClient.AddEventHandler(eventName, eventHandler2); var hasHandlers = devToolsClient.RemoveEventHandler(eventName, eventHandler1); Assert.True(hasHandlers); hasHandlers = devToolsClient.RemoveEventHandler(eventName, eventHandler2); Assert.False(hasHandlers); } }