Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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

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

After some asking in a stackoverflow thread http://stackoverflow.com/questions/6605308/is-it-possible-to-subclass-a-java-object-in-the-constructor and 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();
  }


}

Bear with me, I learning Java via Selenium.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Try changing this:

DefaultCredentialsProvider creds = DefaultCredentialsProvider();

to this:

DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
share|improve this answer
    
nice hack, I will try it next time when creating new instance ;) –  yoosiba Jul 8 '11 at 9:02
    
@yoossiba, it's not a hack, it's proper syntax to construct objects in Java. –  dzieciou Jun 7 at 8:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.