Возможно ли создать подкласс Java-объекта в конструкторе?
Я новичок в Java, опробующий Selenium, в статье здесь http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions есть примечание о том, как изменить объект драйвера HtmlUnitDriver для поддержки аутентификации с помощью некоторого демонстрационного кода, который я повторил здесь.
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;
}
};
Является ли код примером, который входит в определение подкласса, или это модификация, которая является «встроенной»? Я предположил, что это возможно, но когда я копирую его в IDE, я получаю синтаксические ошибки, показывающие, что некоторые свойства не определены.
Узнав больше о Java, анонимных классах и переопределениях, это мой текущий код.
Но я получаю синтаксическую ошибку на DefaultCredentialsProvider в Netbeans, и я не уверен, связано ли это с отсутствием обязательных классов или требуются дополнительные изменения.
/*
* 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();
}
}