Debugging JavaScript in Chromium Embedded Framework - wpf

I have a WPF application which uses CEF to display web content. My question is, is there a way to debug the Javascript/Web parts inside a WPF application?

You may also use ShowDevTools() extension method (source)
ChromiumWebBrowser browser = new ChromiumWebBrowser();
browser.ShowDevTools(); // Opens Chrome Developer tools window

Enable remote debugging in your application:
C# (CefSharp)
CefSettings.RemoteDebuggingPort = 8088;
C++
CefSettings settings;
settings.remote_debugging_port = 8088;
then run your app and point your browser to http://localhost:8088/ to access the Chromium developer console (the same you have in Chrome with Ctrl+Shift+j)

While the accepted answer is correct, it doesn't really have enough detail.
I got this working in CefSharp using the WinForms control in a WPF application. (the WinForms control seems to have better performance). The code for remote debugging will probably be very similar for the WPF control though.
var settings = new CefSettings { RemoteDebuggingPort = 8088 };
Cef.Initialize(settings);
WindowsFormsHost.Child = new ChromiumWebBrowser(url);
Then go to http://localhost:8088/ in your browser.

To use 'ShowDevTools()' you will need first verify if the browser is initialized.
An example solution:
//Add an event to check
ChromeBrowser.IsBrowserInitializedChanged += ChromeBrowser_IsBrowserInitializedChanged;
//Declare the event method to be called
private void ChromeBrowser_IsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs e)
{
if (e.IsBrowserInitialized)
{
ChromeBrowser.ShowDevTools();
}
}

To open the Chromium Dev-Tools window you can do the following:
CefBrowser.GetBrowser().GetHost().ShowDevTools();
This is similar to Eido95's answer, but it doesn't require the extension methods, which essentially just wrap these method calls.
NOTE: The control needs to be initialized before calling this method can be called. If you're wiring-up and F12-like functionality this shouldn't be a problem. If you're trying to do this when the app is starting you will need to listen for the ChromiumWebBrowser.IsBrowserInitializedChanged event

An alternative can be to launch cef with --enable-chrome-runtime.
You'll have the fully featured debugger (link files on disk and edit them from the debugger)

Related

Cefsharp3-WinForms x64 shows blank page but x86 works

I have issues with initializing CefSharp3. The control give a blank page after initializing.
I follow the instructions from CefSharp Wiki page (https://github.com/cefsharp/CefSharp/wiki/Quick-Start and http://ourcodeworld.com/articles/read/173/how-to-use-cefsharp-chromium-embedded-framework-csharp-in-a-winforms-application)
The problem is that when I build the application for x64 i get a blank page, but it works fine in x86.
The only code in my WinForms project is this:
public ChromiumWebBrowser chromeBrowser;
public void InitializeChromium()
{
CefSettings settings = new CefSettings();
// Initialize cef with the provided settings
Cef.Initialize(settings);
// Create a browser component
chromeBrowser = new ChromiumWebBrowser("http://ourcodeworld.com");
// Add it to the form and fill it to the form window.
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill;
}
public Form1()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
I also tried adding a panel, and initializing ChromiumBrowser in the panel with panel1.Controls.Add(chromeBrowser) instead of this.Controls.add(chromeBrowser), but the issue is still there.
I found this in the troubleshooting section at CefSharp wiki:
https://github.com/cefsharp/CefSharp/wiki/Trouble-Shooting
b) The developer tools. Add a button to your form and make a call to "browser.ShowDevTools()". If you can see a document has loaded and you have a DOM in there, then your problem is a display output one and your most likely problem is one of not setting 'Dock' correctly, or some other setting is causing the browser to render offscreen/headless. If you get a blank tool window, or no tool window at all, then CefSharp has failed to initialize correctly, so you have a set-up issue to troubleshoot.
This is exactly the symptoms that I experience.
I tried creating the same project on my laptop (MacBook Pro with Win7, VS2013 and .NET 4.5.2) and it worked like a charm. This means something is up with my workstation (win10, VS2015 .NET 4.5.3).
Any ideas?
This appears to be a bug in the latest build. The solution for now is to use the v51.0.0 build.
You can do this quite easily in the NuGet package manager in VS2015, or if your using VS2013, use the "--version 51.0.0" option when installing cefsharp from the package manager command line.
The issue for the problem is here:
https://github.com/cefsharp/CefSharp/issues/1870
Keep an eye on the issue for future solutions.
Update 28/11/2016
This is a bug in the current V53 release of CefSharp. It's been confirmed by the CefSharp team and is addressed in issue #1819 (https://github.com/cefsharp/CefSharp/issues/1819)
The bug has been apparently fixed, but will not be released until V55.
The solution for now, is to either go back to V51 or to build your own version from source for V55.

Cefsharp Winforms White Screen

I am embeding Cefsharp for Winforms version 51 inside a Windows Forms Host in WPF.
I have tried running the simple example and using winforms CefSharp inside the Windows Forms Host works fine.
However when I make a new solution Cefsharp renders a white screen instead of the website.
Maybe you could add your code to help us to understand your issue.
I am using this code to create a cefsharp webbrowser control and it is working correctly:
CefSettings settings = new CefSettings();
//validate if the CEF instance was already initialized
if (Cef.IsInitialized == false)
Cef.Initialize(settings, true, true);
chromeBrowser = new ChromiumWebBrowser("www.stackoverflow.com");
chromeBrowser.Dock = DockStyle.Fill;
this.Controls.Add(chromeBrowser);
It may have two reasons.
First is a communication problem. If you are using proxy, then communication could be failed. Then your Cefsharp renders a white screen.
And the second reason may be your code.
ChromiumWebBrowser customBrowser;
customBrowser.Load(any_url);
This code load "any_url".

How can I automate Silverlight application with selenium webdriver?

I'm new with Silverlight and Selenium. I searched to automate my Silverlight application with Selenium webdriver but I didn't find any useful source. Can someone guide me?
Cheers,
It's possible to declare methods and attributes as [Scriptable] or a whole class as [ScriptableType]. This way you can invoke/access them via JavaScript, which can be done via WebDriver's executeScript and executeAsyncScript methods. In the class constructor, you can make the instance visible in the DOM by calling:
HtmlPage.RegisterScriptableObject("AnyNameYouWant", this);
Note that no default WebDriver interaction (click, typeKeys) will work within your Silverlight object, so a click on a button, for instance, will have to be done programmatically like:
var peer = new ButtonAutomationPeer(button);
var ip = (IInvokeProvider)peer;
ip.Invoke();
The silverlight-selenium project (https://code.google.com/p/silverlight-selenium/) provides some fixtures for common UI components, relying solely on this JavaScript to Silverlight bridge. Unfortunately, this project is not currently active, but the examples should give you some insights.

Xilium.CefGlue in Winforms application not working properly with ogg

I'm trying to integrate the Xilium.CefGlue browser into an existing project, but i don't seem to get it working with ogg. I'm using the latest versions of the Xilium.CefGlue(xilium-xilium.cefglue-3caa551bd830) and the Cef binary (cef_binary_3.1384.1045). Everything works great when running the projects in the Xilium.CefGlue solution.
To make sure the problem is not caused by my other project, i started a new Winforms project, added references to Xilium.CefGlue.dll, Xilium.CefGlue.Demo.dll and Xilium.CefGlue.WindowsForms.dll. I use the same code in my Program.cs file as used in CefGlue.Client project in the examples, and in my form i create a new CefBrowser and add it directly to my controls. I also placed the needed ceflib files in the correct location.
Now, when running the application and loading a webpage, everything seems to hang unless i change the SingleProcess to true in the CefSettings in my Program.cs. However, when loading another page which contains html5 video (ogg), the page is not getting rendered. I do notice that the interaction is there, because i can start the video by clicking in the center of the page where a button should be located. I can hear the video playing, but the page simply stays blank.
Loading the same page in the CefGlue.Client does work like it should. Now, when i change the setting SingleProcess to true in the example project CefGlue.client, the same thing occurs, so i guess it must have something to do with this.
Anyone got any ideas on what is going wrong?
Thanks,
Andy
Found the answer somewhere else by Sébastien Frippiat:
it seems that using SingleProcess=false doesn't work when debugging with Visual Studio (should be related to Visual Studio using a executable called project_exe.vshost.exe instead of project_exe.exe).
So i changed my CefSettings to this:
var settings = new CefSettings
{
BrowserSubprocessPath = #"C:\CefGlueBrowser\CefGlueBrowser\bin\x86\Debug\CefGlueBrowser.exe",
SingleProcess = false,
MultiThreadedMessageLoop = true,
LogSeverity = CefLogSeverity.Default,
LogFile = "CefGlue.log",
};
That seems to fix it for debugging.
Just disable Visual Studio Hosting Process and you wont have problem for debugging
http://msdn.microsoft.com/en-us/library/ms185330(v=vs.80).aspx

Access Silverlight window within Chrome via System.Windows.Automation

This is probably futile, but I'm wondering if anyone out there has experience doing this.
I'm trying to access a Silverlight application hosted within Google Chrome by using System.Windows.Automation (e.g., AutomationElement).
The problem I'm having is that Chrome hosts the Silverlight app within a child process. If I attempt to find the "Silverlight Control" AutomationElement (by using the main process' hWnd), it fails.
If I locate the Silverlight host child process, it does not have a window handle, and if I attempt to find the control using the child process' Handle it fails.
I know it's there... I can see it using Inspect
but I can only find this by clicking in the Silverlight app and navigating up in Inspect. I cannot navigate down from the tab window using AutomationElement.FindFirst or Inspect.
Its like there is a disconnect between the window and the Silverlight plugin that isn't seen in IE or Firefox, and I don't know how to get around it.
Has anybody else been able to do this?
I'm not sure if this helps or not in your instance but I've run into a couple instances where I needed to enable communication into Silverlight from outside (Office Application AddIns that need to communicate with the Silverlight Application). I've used javascript in the html page hosting the Silverlight Application as a bridge for that communication:
function sendToNav(message) {
var nav = document.getElementById("Nav");
nav.content.NavigationPage.HandleScriptedMessage(message);
}
function passMessageToHost(message) {
if (window.external == null) return;
window.external.HandleScriptedMessage(message);
}
If there any additional information I can provide please let me know. Hope this is of some use to you.

Resources