3

The answers to this question show how to enable Basic Auth with HtmlUnitDriver. However, how can I disable it again?

Note: the accepted answer to this question is correct, however, due to a bug it does not currently work.

Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93

1 Answers1

2

You can use a variant of .addCredentials():

WebDriver driver = new HtmlUnitDriver() {
    protected WebClient modifyWebClient(WebClient client) {
        DefaultCredentialsProvider creds = new DefaultCredentialsProvider();

        creds.addCredentials(username, password, hostname, port, realm);

        client.setCredentialsProvider(creds);
        return client;
    }
};
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
  • That's the correct way. However, by using Wireshark I can see that it still sends credentials to other domains also. Seems to be a bug. – Christian Neverdal Nov 16 '15 at 11:46
  • I also created this, by the way: https://github.com/SeleniumHQ/selenium/issues/1259 – Christian Neverdal Nov 16 '15 at 13:22
  • I update the answer, when your specify the hostname, it doesn't send the credentials. – Ahmed Ashour Nov 16 '15 at 17:31
  • I'm not by my computer now, but if you in your example do one getPage for Google.com with the abcd/efg credentials, then all subsequent getPage calls will also send credentials. I believe I tested with HtmlUnitDriver 2.18 as well as 2.19. – Christian Neverdal Nov 16 '15 at 17:47
  • I thought your question is to do selective sending of the credentials to some specific host (was previous to your last question edit). Now, I guess you need to 'disable' the credentials. Right? DefaultCredentialsProvider was just [changed](https://sourceforge.net/p/htmlunit/code/11550/#diff-2) to have .removeCredentials() – Ahmed Ashour Nov 16 '15 at 18:20
  • That is precicely the problem: even if I specify a host, the credentials will still be sent to all hosts if that host has been contacted once. I will update my question to include all details. See http://pastebin.com/viqcg8Vg – Christian Neverdal Nov 16 '15 at 18:30
  • I will make a complete, working example with Wireshark output. – Christian Neverdal Nov 16 '15 at 18:31
  • Actually, the way you do it by using WebClient directly instead of HtmlUnitDriver seems to be working! – Christian Neverdal Nov 16 '15 at 18:59