我在 intellij 中使用 HtmlUnitDriver 编写了一个测试。代码是:
import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class HtmlUnitDemo {
WebDriver driver;
public static void main(String args[]){
HtmlUnitDemo obj = new HtmlUnitDemo();
obj.setup();
obj.openURL();
}
public void openURL() {
driver.get("https://mail.google.com");
driver.findElement(By.id("username")).sendKeys("xyz");
driver.findElement(By.id("password")).sendKeys("pqr");
driver.findElement(By.className("btn-submit")).click();
Assert.assertEquals(driver.getTitle(), "CAS – Central Authentication Service");
}
public void setup(){
driver = new HtmlUnitDriver();
}
}
此代码在我使用 gradle 时构建。但是,当我在 IDE 中运行它时,它给了我一个
*java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init>(Ljava/lang/String;ILorg/apache/http/conn/scheme/SchemeSocketFactory;)
整个堆栈跟踪是:
at com.gargoylesoftware.htmlunit.HttpWebConnection.createHttpClient(HttpWebConnection.java:504)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getHttpClient(HttpWebConnection.java:470)
at com.gargoylesoftware.htmlunit.HttpWebConnection.setUseInsecureSSL(HttpWebConnection.java:659)
at com.gargoylesoftware.htmlunit.WebClient.setUseInsecureSSL(WebClient.java:1085)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.createWebClient(HtmlUnitDriver.java:263)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:129)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:172)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.<init>(HtmlUnitDriver.java:168)
at smoke.com.thoughtworks.twu.HtmlUnitDemo.setup(HtmlUnitDemo.java:30)
at smoke.com.thoughtworks.twu.HtmlUnitDemo.main(HtmlUnitDemo.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
我用谷歌搜索了这个错误,它说的是与类路径有关的内容。但我不确定到底要做什么。我对这个领域很陌生。因此,任何帮助将不胜感激。先感谢您。