How to embed a Selenium WebDriver as a WPF control?

selenium-webdriver, wpf

Solution

This was a slightly trick one ;)

As Selenium uses external applications (the browsers) most of the time there is no "native" solution like just integrating the Browser-UI in an app.

There are, however, Windows-specific APIs to achieve exactly that with WinForms.

UnsafeNativeMethods.cs

private static class UnsafeNativeMethods {
        [DllImport("user32")]
        public static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}

Basically the idea is to "wrap" the external browser within a usercontrol in your app.

SeleniumHost.cs

    public void AttachDriverService(DriverService service) {
        //get the process started by selenium
        var driverProcess = Process.GetProcessById(service.ProcessId);

        //find the first child-process (should be the browser)
        var browserProcess = driverProcess.GetChildren()
                                .Where(p => p.ProcessName != "conhost")
                                .First();

        _BrowserHandle = browserProcess.MainWindowHandle;

        //set the parent window utilizing Win32-API
        UnsafeNativeMethods.SetParent(_BrowserHandle.Value, this.Handle);
        //handle moving/resizing of your appo to be reflected on browser
        UnsafeNativeMethods.MoveWindow(_BrowserHandle.Value, 0, 0, Width, Height, true);

        this.Resize += (sender, e) => {
            UnsafeNativeMethods.MoveWindow(_BrowserHandle.Value, 0, 0, Width, Height, true);
        };
    }

At last the extension used to enumerate child-processes via WMI:

ProcessExtensions.cs

public static IEnumerable<Process> GetChildren(this Process parent) {
    var query = new ManagementObjectSearcher($@"
        SELECT *
        FROM Win32_Process
        WHERE ParentProcessId={parent.Id}");

    return from item in query.Get().OfType<ManagementBaseObject>()
           let childProcessId = (int)(UInt32)item["ProcessId"]
           select Process.GetProcessById(childProcessId);
}

and call the method like:

var host = new SeleniumHost();
var service = InternetExplorerDriverService.CreateDefaultService();
var driver = new InternetExplorerDriver(service);
host.AttachDriverService(service);

When done, this solves the WinForms-Part. To integrate this in WPF you need to leverage WindowsFormsHost to display the WinForms-Control.

Check out my fresh published repo on GitHub for further reference or directly leverage the NuGet-Package.

Please bear with me, as those are very hot bits - so there sure will be bugs and further improvements to make in the future (like removing the chrome/border from the browser). Hopefully you can get the idea and/or maybe contribute on GitHub.

Problem

Is there a way to embed a `WebDriver` driver to a WPF window, similarly to the WPF's `WebBrowser` control? Optionally, is there a way to use Selenium on the `WebBrowser` control itself? So far, it's only possible to create a new `WebDriver` window, separate from any other WPF window in the application.

Original source