7

I am trying to use the Selenium HtmlUnit driver in C# tests. As far as I know, the only way to use the HtmlUnit driver in C# is through Selenium server and the RemoteWebDriver:

var driver = new OpenQA.Selenium.Remote.RemoteWebDriver(
    OpenQA.Selenium.Remote.DesiredCapabilities.HtmlUnitWithJavaScript());

However, I also need to use NTLM authentication. Using the non-remote driver in Java, it can apparently be configured like this:

WebDriver driver = new HtmlUnitDriver() {    
  protected WebClient modifyWebClient(WebClient client) { 
      // Does nothing here to be overridden. 
      DefaultCredentialsProvider creds = new DefaultCredentialsProvider(); 
      creds.addNTLMCredentials("userName", "password", null, -1,  "myComputerName", "myDomain"); 
      client.setCredentialsProvider(creds); 

      return client; 
    }  
}

(Source: https://groups.google.com/forum/#!topic/webdriver/ktIWIs5m0mQ)

But this obviously does not solve my problem since I am using C#. How can I do that ? (I can use Chrome successfully, but I would like to use HtmlUnit for speed).

Thanks !

personne3000
  • 1,780
  • 3
  • 16
  • 27
  • Which version are you using? Did you install it with Nuget or simply downloading the zip and referencing the dlls? – Joshua Drake Sep 09 '15 at 14:41
  • Does the page prompt for credentials? – Joshua Drake Sep 09 '15 at 17:02
  • Have you considered simply converting the HtmlUnitDriver? http://blog.stevensanderson.com/2010/03/30/using-htmlunit-on-net-for-headless-browser-automation/ – Joshua Drake Sep 09 '15 at 17:08
  • I am using Selenium webdriver from nuget, so I guess it is v2.47.0. The page does not prompt for credentials of course, since htmlunit has no UI (and this is NTLM). I have considered converting the java code to .NET, but I don't think this is a good solution because it would be harder to follow Selenium updates, it would add many dependencies, and this is not the normal way to go when doing Selenium testing – personne3000 Sep 10 '15 at 01:40
  • For the page pop, I was wondering about in the browser, because the existing code provides a method for providing credentials to a pop-up. – Joshua Drake Sep 14 '15 at 15:02

1 Answers1

1

In order to pass credentials you need to overload the modifyWebClient of the HtmlUnitDriver, as you saw in the discussion link1.

For the .NET developer the only way to use the HtmlUnitDriver is via the RemoteWebDriver, and based on the discussion HtmlUnit wrapper for .NET2 the developers chose not to expose all of the HtmlUnit driver classes:

I'm loathe to take any more dependencies in the .NET bindings... If you're dead-set on using HtmlUnit as your headless browser of choice, you can always use it via the RemoteWebDriver

Therefore you cannot use NTLM, or any other credential method, with the RemoteWebDriver.

If you were willing to do and maintain the work you could convert all the HtmlUnit code as detailed in the second link of @JasonPlutext's answer3.

  1. The original sample appears to be from the selenium FAQ.
  2. Linked to from Is there an HtmlUnitDriver for .NET? here on SO.
Community
  • 1
  • 1
Joshua Drake
  • 2,704
  • 3
  • 35
  • 54