【发布时间】:2011-07-07 02:50:46
【问题描述】:
是否可以在构造函数中对 Java 对象进行子类化?
我是一名 Java 新手,在 http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions 的一篇文章中尝试使用 Selenium,这里有一条关于如何修改 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、匿名类和覆盖的更多信息后,这是我当前的代码。 但是我在 Netbeans 中的 DefaultCredentialsProvider 上遇到语法错误,我不确定这是由于缺少必需的类,还是需要进行更多更改。
/*
* 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();
}
}
【问题讨论】:
-
如果您将某些内容声明为受保护,您可能会忘记@Override?span>
-
我是 Java 新手,正在学习关于子类、匿名类等的 Java 视图,@override 已经出现在 Netbeans 中,所以我现在正在查找它。
标签: java constructor selenium-webdriver subclassing