继承 HtmlUnitDriver 以支持 http 身份验证的 Java 代码是什么?

软件测试 硒2 爪哇
2022-02-02 18:26:33

是否可以在构造函数中对 Java 对象进行子类化?

我是一个 Java 新手,在常见问题解答中尝试 Selenium 示例,http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_How_do_I_handle_authentication_with_the_其中有一个关于如何修改 HtmlUnitDriver 驱动程序对象以支持使用我在此处重复的一些演示代码进行身份验证的注释。

在 stackoverflow 线程https://stackoverflow.com/questions/6605308/is-it-possible-to-subclass-a-java-object-in-the-constructor中询问并了解有关 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();
  }


}

请耐心等待,我通过 Selenium 学习 Java。

1个回答

尝试改变这个:

DefaultCredentialsProvider creds = DefaultCredentialsProvider();

对此:

DefaultCredentialsProvider creds = new DefaultCredentialsProvider();