Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NTLM authentication in Selenium RemoteWebDriver

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 !

like image 209
personne3000 Avatar asked Sep 03 '15 02:09

personne3000


People also ask

Can we use RemoteWebDriver instead of WebDriver?

Selenium RemoteWebDriver : Difference between WebDriver and RemoteWebDriver. Selenium Webdriver is a tool used to execute automated test cases on various browsers. The object of the WebDriver is a browser. Selenium RemoteWebDriver implements the WebDriver interface to execute test cases.

What is the use of RemoteWebDriver in selenium?

Selenium RemoteWebDriver is used to execute the browser automation suite on a remote machine. In other words, RemoteWebDriver is a class that implements the WebDriver interface on the remote server. The browser driver classes like FirefoxDriver, ChromeDriver, InternetExplorerDriver, etc.

How do I handle Windows based authentication popup in selenium?

To handle the basic authentication popup, we can pass the username and password along with the web page's URL. When the login pop-up is prompted, we enter the username as “admin” and the password as “admin” and then login. Thus, the user would be successfully logged into the website.


1 Answers

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.
like image 95
Joshua Drake Avatar answered Sep 30 '22 06:09

Joshua Drake