Geb/Selenium测试挂起加载新页面 [英] Geb/Selenium tests hang loading new page

查看:125
本文介绍了Geb/Selenium测试挂起加载新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Selenium测试经常无限期地挂在CI上,总是试图在driver.get中加载新页面.我正在使用PhantomJS 1.9.8.

My Selenium tests frequently hang indefinitely on CI, always at an attempt to load a new page in driver.get. I am using PhantomJS 1.9.8.

经过几轮调试,我认为我已将问题追究到PhantomJS中未完成的网页加载.

After several rounds of debugging, I think I've traced the problem to the webpage load not completing in PhantomJS.

Selenium RemoteWebDriver已将请求发送到PhantomJS/GhostDriver,并且正在等待响应.

Selenium RemoteWebDriver has sent a request to PhantomJS/GhostDriver and it is waiting on a response.

GhostDriver仍在接受请求.我可以通过按curl http://localhost:port/session/:sessionId/url来查看它停留在哪个页面上,此外,如果我从命令行(curl -d '{"url": ...}' http://localhost:port/session/:sessionId/url)重新请求同一页面,那么被阻止的Selenium请求将神奇地从该位置恢复离开.

GhostDriver is still accepting requests. I can see what page it's stuck on by hitting curl http://localhost:port/session/:sessionId/url and further, if I re-request the same page from the command line (curl -d '{"url": ...}' http://localhost:port/session/:sessionId/url) then the blocked Selenium request magically resumes from where it left off.

更新:Ghostdriver本身并未挂起-尽管其默认超时时间实际上是无限的.如果我指定页面加载超时(例如manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS)),则当PhantomJS中发生问题时,GhostDriver将返回带有超时的错误响应,测试将失败,并且构建将继续进行.

UPDATE: Ghostdriver itself is not hanging -- its default timeout is essentially infinite, though. If I specify a page load timeout, e.g., manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS) then when the problem happens in PhantomJS, the GhostDriver will return an error response with a timeout, the test will fail, and the build will proceed.

如果我调试PhantomJS本身(--debug = TRUE),那么我将其视为故障点日志中的最后一件事

If I debug PhantomJS itself (--debug=TRUE) then I see this as the last thing in the logs at the point of failure

2015-03-21T21:26:39 [DEBUG] WebPage - updateLoadingProgress: 86 (然后什么都没有,直到超时)

2015-03-21T21:26:39 [DEBUG] WebPage - updateLoadingProgress: 86 (then nothing until timeout reached)

在挂起时从Java端进行的堆栈跟踪如下:

The stack trace from the Java side at the point of hanging is like this:

"Forwarding get on session fd1ac2c0-ccd4-11e4-a596-a1f7b09caa5d to remote" prio=10 tid=0x0000000001f74800 nid=0x5cc3 runnable [0x00002b87c3039000]
 java.lang.Thread.State: RUNNABLE
     at java.net.SocketInputStream.socketRead0(Native Method)
     at java.net.SocketInputStream.read(SocketInputStream.java:150)
     at java.net.SocketInputStream.read(SocketInputStream.java:121)
     at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)
     at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)
     at horg.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:281)
     at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:92)
     at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
     at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
     at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
     at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
     at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:219)
     at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
     at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
     at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:712)
     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)
     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
     at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:316)
     at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:295)
     at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:66)
     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
     at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:276)
     at org.openqa.selenium.WebDriver$get.call(Unknown Source)
     at geb.Browser.go(Browser.groovy:371)
     at geb.Browser$go.call(Unknown Source)
     at geb.Page.to(Page.groovy:169)

感觉与此类似,但是使用PhantomJS而不是Firefox:

It feels similar to this but with PhantomJS instead of Firefox:

Firefox WebDriver挂起,等待页面加载

推荐答案

我希望您能找到一种将其实现到代码中的方法,但是当我在PhantomJS挂起时遇到类似情况时,这对我有用.

I hope you can find a way to implement this into your code, but this is what worked for me when I was having a similar situation with PhantomJS hanging.

我追踪到它挂在了driver.get()呼叫上,这对我来说是在说什么事情没有通过,或者网络驱动程序不是-由于某种原因-使加载成功命令返回驱动程序,从而允许脚本继续.

I traced it to be hanging on a driver.get() call, which for me was saying something wasn't going through or the webdriver just wasn't - for some reason - giving the load successful command back to the driver, allowing the script to continue.

因此,我添加了以下内容:

So, I added the following:

driver = webdriver.PhantomJS()

# set timeout information
driver.set_page_load_timeout(15)

我已经在5秒钟的时间进行了测试,但是它没有等待足够长的时间,所以什么也不会发生. 15秒对我来说很棒,但这也许是您应该测试的东西.

I've tested this at a time of 5 (seconds) and it just didn't wait long enough and nothing would happen. 15 seconds worked great for me, but that's maybe something you should test.

最重要的是,只要有一个可供webdriver超时的选项,我都会创建一个循环,以便driver.get()可以尝试重新发送.get()命令.实施try / except堆叠方案,我能够做到这一点:

On top of this, I also created a loop whenever there was an option for the webdriver to timeout, so that the driver.get() could attempt to re-send the .get() command. Implementing a try / except stacked scenario, I was able to approach this:

while finished == 0:
    try:
        driver.get(url3)
        finished = 1
    except:
        sleep(5)

我看到一个例外句柄为:

I have seen an except handle as:

except TimeoutException as e:
    #Handle your exception here
    print(e)

但是我没有用.不过,很高兴知道如何捕获特定的异常.

but I had no use for this. It might be nice to know how to catch specific exceptions, though.

有关更多超时选项,请参见以下解决方案:在Selenium Webdriver上设置超时.PhantomJS

See this solution for more options for a timeout: Setting timeout on selenium webdriver.PhantomJS

这篇关于Geb/Selenium测试挂起加载新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆