How to override HtmlUnitDriver to support http authentication with Faq example

619 views
Skip to first unread message

Frank Church

unread,
Jul 7, 2011, 8:26:36 AM7/7/11
to seleniu...@googlegroups.com
I need to implement http authentication with HtmlUnitDriver and the faq gives this example.

After a query on stackoverflow  this is the code I have come up with so far.

package seleniumtest01;

/**
 *
 * @author frank
 */
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.WebClient;

public class Main {

  public static void testBasicAuth() {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface,
    // not the implementation.
    //WebDriver driver = new FirefoxDriver();
    WebDriver driver = new HtmlUnitDriver() {

      @Override
      protected WebClient modifyWebClient(WebClient client) {
        // This class ships with HtmlUnit itself
        DefaultCredentialsProvider creds = DefaultCredentialsProvider();

        // Set some example credentials
        creds.addCredentials("username", "password");

        // And now add the provider to the webClient instance
        client.setCredentialsProvider(creds);

        return client;
      }
    };
};


I am getting a syntax error that DefaultCredentialsProvider is not defined.

Are there any imports I am missing, or does the code require more changes?

I am new to Java so hints and explanations are welcome.


Luke Inman-Semerau

unread,
Jul 7, 2011, 2:21:57 PM7/7/11
to seleniu...@googlegroups.com
You're missing the "new" operator, should be:

DefaultCredentialsProvider creds = new DefaultCredentialsProvider();




--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/M8MIUK7HnAYJ.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

Frank Church

unread,
Jul 14, 2011, 4:26:54 AM7/14/11
to Selenium Users


On Jul 7, 7:21 pm, Luke Inman-Semerau <luke.seme...@gmail.com> wrote:
> You're missing the "new" operator, should be:
>
>    DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
>

Thanks, I had someone give me the same answer on stackexchange.

Isn't top posting frowned upon in Selenium Users?


> On Thu, Jul 7, 2011 at 5:26 AM, Frank Church <vfcli...@gmail.com> wrote:
> > I need to implement http authentication with HtmlUnitDriver and the faq<http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Ho...>gives this example.
>
> > After a query on stackoverflow<http://stackoverflow.com/questions/6605308/is-it-possible-to-subclass...>

Luke Inman-Semerau

unread,
Jul 14, 2011, 10:13:11 AM7/14/11
to seleniu...@googlegroups.com
I don't think so, since most people use gmail or view it through
googlegroups... Google nicely separates it for you, so you just see
the last reply. Plus iPhone defaults to replying on top ;)

-Luke

>>> To post to this group, send email to selenium-
>>> us...@googlegroups.com.


>>> To unsubscribe from this group, send email to
>>> selenium-user...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/selenium-users?hl=en.
>

> --
> You received this message because you are subscribed to the Google
> Groups "Selenium Users" group.

Reply all
Reply to author
Forward
0 new messages