Is it possible to subclass a Java object in the constructor?
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.findElem开发者_StackOverflow中文版ent(By.name("field_one"));
element.clear();
element.sendKeys("sample text");
//driver.findElement(By. id("submit")).click();
element.submit();
new WebDriverWait(driver, 10);
driver.quit();
}
}
The code you presented is not modifying the original class, but is creating an anonymous subclass of HtmlUnitDriver
.
For example:
class A {
void sayHello() { System.out.println("Hello!"); }
}
class Main {
public static void main(String[] args) {
A a = new A() {
@Override void sayHello() { System.out.println("Good bye"); }
}
a.sayHello();
}
}
This will print Good bye
. The type of the instance held by the local variable a
is an anonymous class, automatically generated by the compiler. The name of the class will be something like Main$0
.
It is hard to give a clear answer to this question without having the Javadoc of the class HtmlUnitDriver
. If HtmlUnitDriver
is an abstract class or an interface, then the sample code in your question is called an anonymous class. Otherwise, the code is simply overriding the method of the class.
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
精彩评论