Search code examples
javaseleniumbasic-authenticationhtmlunit-driver

HtmlUnitDriver: enable/disable Basic Authentication?


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.


Solution

  • 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;
        }
    };