Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6117347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:18:37+00:00 2026-05-23T15:18:37+00:00

Is it possible to subclass a Java object in the constructor? I am a

  • 0

Is it possible to subclass a Java object in the constructor?

I am a Java newbie trying out Selenium in an article here http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions there is a note about how to modify an HtmlUnitDriver driver object to support authentication with some demo code I have repeated here.

WebDriver driver = new HtmlUnitDriver() {
  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;
  }
};

Is the code an example that goes into the definition of subclass or is it a modification that is ‘inline’? I have assumed that it is possible but when I copy it into the IDE I get syntax errors showing that some of the properties are not defined.

After learning more about Java, anonymous classes and overrides this is my current code.
But I am getting a syntax error on the DefaultCredentialsProvider in Netbeans, and I am not sure whether it is due to the absence of required classes, or whether some more changes are required.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seleniumtest01;

/**
 *
 * @author richard
 */
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;
//import org.openqa.selenium.htmlunit.ChromeDriver;

public class Main {

  public static void main(String[] args) {
    // Create a new instance of the Firefox driver
    // Notice that the remainder of the code relies on the interface,
    // not the implementation.
    testBasicAuth();
    System.exit(0);
  }

  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;
      }
    };
    driver.get("http://user:selenium@192.168.1.2/");
    new WebDriverWait(driver, 10);

    WebElement element = driver.findElement(By.xpath("//a[text()='Connection']"));
    element.click();
    //element = driver.findElement(By.xpath("//a[text()='Admin Login']"));
    element = driver.findElement(By.xpath("//a[contains(@href, 'admin/connection')]"));//[contains(@href,'#id1')]
    element.click();
    element = driver.findElement(By.xpath("//a[text()='Connection 1']"));
    element.click();
    element = driver.findElement(By.name("field_one"));
    element.clear();
    element.sendKeys("sample text");
    //driver.findElement(By. id("submit")).click();
    element.submit();

    new WebDriverWait(driver, 10);
    driver.quit();
  }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:18:37+00:00Added an answer on May 23, 2026 at 3:18 pm

    After posting the question on sqa.stackexchange.com I got this is what the constructor for the WebDriver should be:

    WebDriver driver = new HtmlUnitDriver() {
    
      @Override
      protected WebClient modifyWebClient(WebClient client) {
        // This class ships with HtmlUnit itself
        DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
    
        // Set some example credentials
        creds.addCredentials("user", "selenium");
    
        // And now add the provider to the webClient instance
        client.setCredentialsProvider(creds);
    
        return client;
      }
    };
    

    After adding the override I was missing the new to the creds initialization, a Java newbie error.
    Thanks for the help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to find out the subclass, form an superclass object. if
Is it possible to convert a com.vividsolutions.jts.geom.Geometry (or a subclass of it) into a
In Java, is it possible to override member data in a subclass and have
I'm reading this: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2 They say: Consider the example program: class Point { int
In Java is it possible to dynamically create anonymous subclass instance given only instance
Within Java code it is possible to have numeric literals that use a suffix
Is it possible to subclass dynamically? I know there's ____bases____ but I don't want
Is it somehow possible to customize or subclass System.Windows.Forms.ColorDialog to add a few buttons?
Is it possible to make a public method private in a subclass? I don't
If a class defined an annotation, is it somehow possible to force its subclass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.