在第二个.get(url)上使用HTMLUnitDriver ScriptException(找不到tmp文件)的Selenium

时间:2015-07-16 11:57:55

标签: java selenium htmlunit-driver

我使用testng和htmlUnitDriver运行selenium测试。我为所有测试使用一个webDriver实例。我只是在每个测试类之后注销并清除cookie driver.manage().deleteAllCookies()(删除cookie本身不负责我为此测试)

所以基本上发生的是:

driver.get(url1);
... login to Single Page App ...
driver ... pseudo ... find logout-element and click logout
driver.manage().deleteAllCokies();
driver.get(url1);
... input credentials ...
loginForm.submit();

这会导致以下异常:

    com.gargoylesoftware.htmlunit.ScriptException: java.io.FileNotFoundException: C:\tmp\selenium\htmlunit5619416597224576810.tmp (Das System kann die angegebene Datei nicht finden)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:705)
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:637)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:612)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:1001)
        at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventListeners(EventListenersContainer.java:179)
        at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:239)
        at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:814)
        at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:748)
        at com.gargoylesoftware.htmlunit.html.HtmlElement$1.run(HtmlElement.java:920)
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
        at com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:925)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1298)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:256)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:475)
        at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2074)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:733)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:820)
        at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1325)
        at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1268)
        at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1216)
        at org.openqa.selenium.htmlunit.HtmlUnitWebElement.submit(HtmlUnitWebElement.java:177)
        ... <snip> ...
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
    Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: C:\tmp\selenium\htmlunit5619416597224576810.tmp (Das System kann die angegebene Datei nicht finden)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:739)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.access$500(JavaScriptEngine.java:93)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:700)
        ... 56 more
    Caused by: java.io.FileNotFoundException: C:\tmp\selenium\htmlunit5619416597224576810.tmp (Das System kann die angegebene Datei nicht finden)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at com.gargoylesoftware.htmlunit.DownloadedContent$OnFile.getInputStream(DownloadedContent.java:81)
        at com.gargoylesoftware.htmlunit.WebResponseData.getStream(WebResponseData.java:91)
        at com.gargoylesoftware.htmlunit.WebResponseData.getInputStream(WebResponseData.java:156)
        at com.gargoylesoftware.htmlunit.WebResponse.getContentAsStream(WebResponse.java:241)
        at com.gargoylesoftware.htmlunit.util.WebResponseWrapper.getContentAsStream(WebResponseWrapper.java:58)
        at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:150)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:468)
        at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2074)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:733)
        ... 58 more

删除该文件发生在第二次接听电话上。我很乐意就可能导致这种情况的原因提出建议。

这是我使用的硒maven依赖:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.45.0</version>
    </dependency>

1 个答案:

答案 0 :(得分:1)

此错误是因为缓存了WebResponse,并且在获取新页面时清除旧页面并删除其本地文件(默认情况下内容大于500 KB)。

您可以使用以下内容直到修复此问题。

    WebDriver driver = new HtmlUnitDriver() {
        @Override
        protected WebClient getWebClient() {
            WebClient c = super.getWebClient();
            c.getCache().setMaxSize(0);
            return c;
        }
    };