im在学习如何使用硒来自动化的过程中,我们浏览并通过在线课程进行工作。
我目前遇到一些问题,试图使用htmlunitdriver进行无头浏览器测试
我有一个简单的测试,该测试应该加载网页并断言标题,代码如下。
package com.seleniumsimplified.webdriver;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import static org.junit.Assert.assertTrue;
public class MyFirstTest {
@Test
public void driverIsTheKing(){
WebDriver driver = new HtmlUnitDriver();
driver.get("http://compendiumdev.co.uk/selenium");
assertTrue(driver.getTitle().startsWith("Selenium Simplified"));
}
}
尝试运行测试时,测试似乎无法正常运行,大约需要4个小型,此后我收到以下错误,而我不确定如何解决:
"C:Program FilesJavajdk1.8.0_131binjava" -ea -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:Program Files (x86)JetBrainsIntelliJ IDEA Community` Edition 2016.2bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "C:Program Files (x86)JetBrainsIntelliJ IDEA Community Edition 2016.2libidea_rt.jar;C:Program Files (x86)JetBrainsIntelliJ IDEA Community Edition 2016.2pluginsjunitlibjunit-rt.jar;C:Program FilesJavajdk1.8.0_131jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_131jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_131jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_131jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_131jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_131jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_131jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_131jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_131jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_131jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_131jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_131jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_131jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_131jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_131jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_131jrelibjce.jar;C:Program FilesJavajdk1.8.0_131jrelibjfr.jar;C:Program FilesJavajdk1.8.0_131jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_131jrelibjsse.jar;C:Program FilesJavajdk1.8.0_131jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_131jrelibplugin.jar;C:Program FilesJavajdk1.8.0_131jrelibresources.jar;C:Program FilesJavajdk1.8.0_131jrelibrt.jar;C:Usersra.r.hopeIdeaProjectswebdriverbasicstargettest-classes;C:Usersrobert.hope.m2repositoryjunitjunit4.12junit-4.12.jar;C:Usersrobert.hope.m2repositoryorghamcresthamcrest-core1.3hamcrest-core-1.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-java3.5.3selenium-java-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-api3.5.3selenium-api-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-chrome-driver3.5.3selenium-chrome-driver-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-edge-driver3.5.3selenium-edge-driver-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-firefox-driver3.5.3selenium-firefox-driver-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-ie-driver3.5.3selenium-ie-driver-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-opera-driver3.5.3selenium-opera-driver-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-remote-driver3.5.3selenium-remote-driver-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-safari-driver3.5.3selenium-safari-driver-3.5.3.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumselenium-support3.5.3selenium-support-3.5.3.jar;C:Usersrobert.hope.m2repositorycglibcglib-nodep3.2.4cglib-nodep-3.2.4.jar;C:Usersrobert.hope.m2repositoryorgapachecommonscommons-exec1.3commons-exec-1.3.jar;C:Usersrobert.hope.m2repositoryorgapachecommonscommons-lang33.5commons-lang3-3.5.jar;C:Usersrobert.hope.m2repositorycommons-codeccommons-codec1.10commons-codec-1.10.jar;C:Usersrobert.hope.m2repositorycommons-iocommons-io2.5commons-io-2.5.jar;C:Usersrobert.hope.m2repositorycommons-loggingcommons-logging1.2commons-logging-1.2.jar;C:Usersrobert.hope.m2repositoryorgw3ccsssac1.3sac-1.3.jar;C:Usersrobert.hope.m2repositorynetsourceforgecssparsercssparser .9.23cssparser-0.9.23.jar;C:Usersrobert.hope.m2repositorycomgooglecodegsongson2.8.0gson-2.8.0.jar;C:Usersrobert.hope.m2repositorycomgoogleguavaguava23.0guava-23.0.jar;C:Usersrobert.hope.m2repositorycomgooglecodefindbugsjsr3051.3.9jsr305-1.3.9.jar;C:Usersrobert.hope.m2repositorycomgoogleerrorproneerror_prone_annotations2.0.18error_prone_annotations-2.0.18.jar;C:Usersrobert.hope.m2repositorycomgooglej2objcj2objc-annotations1.1j2objc-annotations-1.1.jar;C:Usersrobert.hope.m2repositoryorgcodehausmojoanimal-sniffer-annotations1.14animal-sniffer-annotations-1.14.jar;C:Usersrobert.hope.m2repositorynetsourceforgehtmlunithtmlunit2.27htmlunit-2.27.jar;C:Usersrobert.hope.m2repositorynetsourceforgehtmlunithtmlunit-core-js2.27htmlunit-core-js-2.27.jar;C:Usersrobert.hope.m2repositorynetsourceforgehtmlunitneko-htmlunit2.27neko-htmlunit-2.27.jar;C:Usersrobert.hope.m2repositoryorgapachehttpcomponentshttpclient4.5.3httpclient-4.5.3.jar;C:Usersrobert.hope.m2repositoryorgapachehttpcomponentshttpcore4.4.6httpcore-4.4.6.jar;C:Usersrobert.hope.m2repositoryorgapachehttpcomponentshttpmime4.5.3httpmime-4.5.3.jar;C:Usersrobert.hope.m2repositoryorgeclipsejettyjetty-client9.4.5.v20170502jetty-client-9.4.5.v20170502.jar;C:Usersrobert.hope.m2repositoryorgeclipsejettyjetty-http9.4.5.v20170502jetty-http-9.4.5.v20170502.jar;C:Usersrobert.hope.m2repositoryorgeclipsejettyjetty-io9.4.5.v20170502jetty-io-9.4.5.v20170502.jar;C:Usersrobert.hope.m2repositoryorgeclipsejettyjetty-util9.4.5.v20170502jetty-util-9.4.5.v20170502.jar;C:Usersrobert.hope.m2repositorynetjavadevjnajna4.1.0jna-4.1.0.jar;C:Usersrobert.hope.m2repositorynetjavadevjnajna-platform4.1.0jna-platform-4.1.0.jar;C:Usersrobert.hope.m2repositorycomcodebornephantomjsdriver1.4.0phantomjsdriver-1.4.0.jar;C:Usersrobert.hope.m2repositoryjavaxservletjavax.servlet-api3.1.0javax.servlet-api-3.1.0.jar;C:Usersrobert.hope.m2repositoryorgeclipsejettywebsocketwebsocket-api9.4.5.v20170502websocket-api-9.4.5.v20170502.jar;C:Usersrobert.hope.m2repositoryorgeclipsejettywebsocketwebsocket-client9.4.5.v20170502websocket-client-9.4.5.v20170502.jar;C:Usersrobert.hope.m2repositoryorgeclipsejettywebsocketwebsocket-common9.4.5.v20170502websocket-common-9.4.5.v20170502.jar;C:Usersrobert.hope.m2repositoryxalanserializer2.7.2serializer-2.7.2.jar;C:Usersrobert.hope.m2repositoryxalanxalan2.7.2xalan-2.7.2.jar;C:Usersrobert.hope.m2repositoryxercesxercesImpl2.11.0xercesImpl-2.11.0.jar;C:Usersrobert.hope.m2repositoryxml-apisxml-apis1.4.01xml-apis-1.4.01.jar;C:Usersrobert.hope.m2repositoryorgseleniumhqseleniumhtmlunit-driver2.27htmlunit-driver-2.27.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.seleniumsimplified.webdriver.MyFirstTest,driverIsTheKing
Sep 13, 2017 8:24:34 AM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://compendiumdev.co.uk:443: Connection reset
Sep 13, 2017 8:24:34 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://compendiumdev.co.uk:443
Sep 13, 2017 8:25:34 AM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://compendiumdev.co.uk:443: Connection reset
Sep 13, 2017 8:25:34 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://compendiumdev.co.uk:443
Sep 13, 2017 8:26:43 AM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {s}->https://compendiumdev.co.uk:443: Connection reset
Sep 13, 2017 8:26:43 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://compendiumdev.co.uk:443
org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset
Build info: version: '3.5.3', revision: 'a88d25fe6b', time: '2017-08-29T12:42:44.417Z'
System info: host: 'LT-LGC07999', ip: '10.44.22.14', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:686)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.lambda$8(HtmlUnitDriver.java:655)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.lambda$0(HtmlUnitDriver.java:412)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.waitForClose(SSLSocketImpl.java:1769)
at sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:124)
at sun.security.ssl.Handshaker.kickstart(Handshaker.java:1067)
at sun.security.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1487)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1351)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355)
at com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.connectSocket(HtmlUnitSSLConnectionSocketFactory.java:186)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:194)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1379)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1429)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1298)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:392)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:313)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:668)
... 3 more
我在网上进行了一些研究,发现使用htmlunitdriver时默认情况下JavaScript被禁用,但是我不确定这对我的问题是否重要
我通过在创建新的htmlunitdriver
时将值设置为true来实验启用Java脚本WebDriver driver = new HtmlUnitDriver(true);
我尝试这样做并重新运行代码,但是不幸的是,我在运行测试时会重新获得相同的结果......
以前有人遇到过此类问题,并且能够提供有关如何进行测试进行的任何指导。
用于使用硒的参考IM 3.53Junit 4.12和Htmluintdriver 2.27
additonaly i可以使用Chromedriver或Firefoxdriver进行相同的测试,并且测试可以正确运行和通过
谢谢
htmlunitdriver是硒版2.53的一部分。如果您使用的是2.52或更早的硒,则无需下载和安装htmlunitdriver,它已经存在。
,但您仍然想使用它从这里下载
https://github.com/seleniumhq/htmlunit-driver/releases
在2.53版本中,您需要单独添加