How do I handle authentication with the HtmlUnitDriver?
Asked
Active
Viewed 5,428 times
3

Ripon Al Wasim
- 36,924
- 42
- 155
- 176

Jasmine.Olivra
- 1,751
- 9
- 24
- 37
-
1Does [this](http://code.google.com/p/selenium/issues/detail?id=34) help? – Petr Janeček Jun 28 '12 at 12:06
2 Answers
2
If that is the basic authentication that you need you can do this when creating a HtmlUnitDriver instance: (the code is in scala, but you can easily change it to java)
new HtmlUnitDriver() {
override def modifyWebClient(client: WebClient) = {
val creds = new DefaultCredentialsProvider()
creds.addCredentials("user-name", "user-password");
client.setCredentialsProvider(creds)
client
}
}

Andro Selva
- 53,910
- 52
- 193
- 240

Sylwester Gryzio
- 997
- 6
- 12
2
Try this in java seemed to work for me
WebDriver driver = new HtmlUnitDriver() {
protected WebClient modifyWebClient(WebClient client) {
// This class ships with HtmlUnit itself
DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
// Set some example credentials
creds.addCredentials("username", "password");
// And now add the provider to the webClient instance
client.setCredentialsProvider(creds);
return client;
}
};

eandersson
- 25,781
- 8
- 89
- 110

Theo
- 138
- 4