Webdriver - 无限页面加载提交

时间:2013-05-15 13:17:53

标签: webdriver selenium-webdriver

我正在测试登录页面。在某些时候(读作间歇性),主页的加载需要无限的时间。在这种情况下,以下命令永远不会完成 -

driver.findElement(By.id("Login")).submit();

我知道隐含的&明确的等待。 这些等待仅适用于findElement或DOM相关操作,但不适用于提交命令((Ref:https://sqa.stackexchange.com/questions/2606/what-is-seleniums-default-timeout-for-page-loading

调试日志跟踪 -

15 May 2013 16:42:08 DEBUG wire:77 - << "{"name":"submitElement","sessionId":"949f6c8f-a8fc-4e13-b4da-bf6c19c893fe","status":0,"value":""}"
15 May 2013 16:42:08 DEBUG DefaultClientConnection:152 - Connection shut down
15 May 2013 16:42:08 DEBUG ThreadSafeClientConnManager:272 - Released connection is not reusable.
15 May 2013 16:42:08 DEBUG ConnPoolByRoute:434 - Releasing connection [HttpRoute[{}->http://127.0.0.1:7055]][null]
15 May 2013 16:42:08 DEBUG ConnPoolByRoute:679 - Notifying no-one, there are no waiting threads
15 May 2013 16:42:08 DEBUG SalesForce:153 - Verify next page
15 May 2013 16:42:08 DEBUG ThreadSafeClientConnManager:221 - Get connection: HttpRoute[{}->http://127.0.0.1:7055], timeout = 120000
15 May 2013 16:42:08 DEBUG ConnPoolByRoute:350 - [HttpRoute[{}->http://127.0.0.1:7055]] total kept alive: 0, total issued: 0, total allocated: 0 out of 2000
15 May 2013 16:42:08 DEBUG ConnPoolByRoute:523 - No free connections [HttpRoute[{}->http://127.0.0.1:7055]][null]
15 May 2013 16:42:08 DEBUG ConnPoolByRoute:369 - Available capacity: 2000 out of 2000 [HttpRoute[{}->http://127.0.0.1:7055]][null]
15 May 2013 16:42:08 DEBUG ConnPoolByRoute:549 - Creating new connection [HttpRoute[{}->http://127.0.0.1:7055]]
15 May 2013 16:42:08 DEBUG DefaultClientConnectionOperator:145 - Connecting to 127.0.0.1:7055
15 May 2013 16:42:08 DEBUG RequestAddCookies:132 - CookieSpec selected: best-match
15 May 2013 16:42:08 DEBUG RequestAuthCache:75 - Auth cache not set in the context
15 May 2013 16:42:08 DEBUG DefaultHttpClient:643 - Attempt 1 to execute request
15 May 2013 16:42:08 DEBUG DefaultClientConnection:264 - Sending request: GET /hub/session/949f6c8f-a8fc-4e13-b4da-bf6c19c893fe/title HTTP/1.1
15 May 2013 16:42:08 DEBUG wire:63 - >> "GET /hub/session/949f6c8f-a8fc-4e13-b4da-bf6c19c893fe/title HTTP/1.1[\r][\n]"
15 May 2013 16:42:08 DEBUG wire:63 - >> "Accept: application/json, image/png[\r][\n]"
15 May 2013 16:42:08 DEBUG wire:63 - >> "Cache-Control: no-cache[\r][\n]"
15 May 2013 16:42:08 DEBUG wire:63 - >> "Host: 127.0.0.1:7055[\r][\n]"
15 May 2013 16:42:08 DEBUG wire:63 - >> "Connection: Keep-Alive[\r][\n]"
15 May 2013 16:42:08 DEBUG wire:63 - >> "[\r][\n]"
15 May 2013 16:42:08 DEBUG headers:268 - >> GET /hub/session/949f6c8f-a8fc-4e13-b4da-bf6c19c893fe/title HTTP/1.1
15 May 2013 16:42:08 DEBUG headers:271 - >> Accept: application/json, image/png
15 May 2013 16:42:08 DEBUG headers:271 - >> Cache-Control: no-cache
15 May 2013 16:42:08 DEBUG headers:271 - >> Host: 127.0.0.1:7055
15 May 2013 16:42:08 DEBUG headers:271 - >> Connection: Keep-Alive

基本上,DefaultClientConnection没有收到200 OK&amp;命令只是挂起。

在等待特定时间后,如果浏览器没有响应提交命令,请寻找关闭浏览器的解决方案。

1 个答案:

答案 0 :(得分:0)

我猜id“登录”是一个表单。

您是否可以选择表单的提交按钮(假设存在一个)并在按钮上调用.click(),而不是在表单上调用.submit()?

然后你应该能够使用隐式或显式等待。

编辑:

提交表单后,您可以创建一个等待响应的自定义方法,并在超时后关闭浏览器(如有必要)。

        int tryCount = 0;
        boolean desiredResponseReceived = false;
        while (desiredResponseReceived == false && tryCount < 20) {
            String statusText = (String) js.executeScript("return xhr.statusText;");
            if (statusText.indexOf("200") != -1) {
                desiredResponseReceived = true;
            }
            else {
                Thread.sleep(250);
                tryCount++;
            }
        }

        if (desiredResponseReceived == false) {
            driver.quit();
        }

如上所述,如果在5秒内xhr响应中没有出现“200”,此方法将关闭浏览器。 (因为它在最终到达if (desiredResponseReceived == false)并关闭浏览器之前每隔250毫秒检查20次,然后关闭浏览器。)