1

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

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

我知道隐式和显式等待。这些等待仅适用于 findElement 或 DOM 相关操作,但不适用于提交命令((参考: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 & 命令只是挂起。

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

4

1 回答 1

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 次)。

于 2013-05-15T20:15:18.983 回答