Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binding cefSharp client with Selenium Chrome Driver c#

I found this https://bitbucket.org/chromiumembedded/cef/wiki/UsingChromeDriver link which has java binding for cef client and Selenium Driver.

So I prepared one for me to use it with c# windows application. what i have done is created a new winapp project x86 that only contains the following code that runs with no error or issue:

using CefSharp;
using CefSharp.WinForms;
namespace ClientBrowser
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    public ChromiumWebBrowser browser;
    private void Form1_Load(object sender, EventArgs e)
    {
        var settings = new CefSettings();
        settings.CefCommandLineArgs.Add("enable-npapi", "1");
        settings.IgnoreCertificateErrors = true;
        //settings.CefCommandLineArgs.Add("enable-system-flash", "1");
        Cef.Initialize(settings);
        browser = new ChromiumWebBrowser("");
        this.Controls.Add(browser);
        browser.Dock = DockStyle.Fill;
    }
  }
}

above project is to act as cef client for the below new winapp project x86 below:

    private void Form1_Load(object sender, EventArgs e)
    {
        try {
            var options = new ChromeOptions();
            options.BinaryLocation = @"path/ClientBrowser.exe";
            //options.AddArgument("--log-level=3");
            var service = ChromeDriverService.CreateDefaultService();
            //service.HideCommandPromptWindow = true;
            driver = new ChromeDriver(service,options); //chromedriver.exe

            driver.Navigate().GoToUrl("http://stackoverflow.com/");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

now when I run the new above Project,

chromedriver.exe says:

Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 57883 Only local connections are allowed.

and then

ClientBrowser.exe opens up with nothing displayed in it and no error encountered but after 60 sec I am getting:

The HTTP request to the remote WebDriver server for URL http://localhost:57883/session timed out after 60 seconds.

but it should open http://stackoverflow.com/ for me, I don't know what I am doing wrong, I have not included any selenium-server-standalone-x.y.z.jar? do I need this, if yes please any one let me know how do I include it.

also tried running it with Administrator Privilege

is there any way to directly bind ChromeDriver with ChromiumWebBrowser , so there will be no need for cefclient.exe

also tried with RemoteWebDriver:

       try {
            var options = new ChromeOptions();
            options.BinaryLocation = @"C:\pathto\ClientBrowser.exe";
            options.AddArgument("--remote-debugging-port=1131");
            options.AddArgument("url=data:,");
            //options.AddArgument("--log-level=3");

            var service = ChromeDriverService.CreateDefaultService();
            //service.HideCommandPromptWindow = true;
            service.Port = 1131;
            service.Start();

            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.SetCapability(ChromeOptions.Capability, options);

            driver = new RemoteWebDriver(service.ServiceUrl, capabilities); //DesiredCapabilities.Chrome()

            //driver = new ChromeDriver(service,options); //chromedriver.exe

            driver.Navigate().GoToUrl("http://stackoverflow.com/");

        }

but RemoteWebDriver also didn't work for me, really stuck now, please

any help or suggestion would be a great help for me and for others too who wanted to do the same like me, Thanks in advance.

like image 545
Mohammed Sufian Avatar asked Mar 09 '16 18:03

Mohammed Sufian


2 Answers

I've just stumbled across this after spending many hours trying to achieve the same thing as the OP. I too was able to get Selenium working fine when testing against the cefclient.exe. However, our mock up CEF based application I was trying to test wouldn't play ball and Selenium would time out no matter what port I tried.

The solution turned out to be rather simple. It appears that when you launch the CEF embedded browser you can either pass in a URL string as an address or leave it blank. If you leave it blank, the page will be empty. This seems to cause Selenium to time out. If you add a URL though, Selenium connects fine and no port settings etc are required. Just the binary location switch. So my solution was as follows:

Within the CEF initialization code:

    _browser = new ChromiumWebBrowser("data:,")
    {
        Dock = DockStyle.Fill,
    };

Within the Selenium initialization code:

var options = new ChromeOptions { BinaryLocation = "PathToYourCef.exe" };

cefDriver = new ChromeDriver(options);
like image 150
Phonesis Avatar answered Oct 20 '22 19:10

Phonesis


This is a usage example with the Chromium Embedded Framework:

var service = ChromeDriverService.CreateDefaultService();
var options = new ChromeOptions();
options.BinaryLocation = @"C:\Users\florent\Downloads\Cef\cefclient.exe";
options.AddArgument("url=data:,");

var driver = new ChromeDriver(service, options);
driver.Navigate().GoToUrl("https://www.google.co.uk");

You can download a recent build here (Test App): https://cefbuilds.com/

like image 30
Florent B. Avatar answered Oct 20 '22 18:10

Florent B.