読者です 読者をやめる 読者になる 読者になる

yfj2’s Automatic Web Test Related Blog

yfj2のWEBテスト自動化に関わるブログ

【Geb】【Tips】WebDriverException SocketException Connection reset

【Geb】【Tips】WebDriverException SocketException Connection reset
著者:ふじさわゆうき

問題

  • 自動テスト中に"org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset"が発生してテストが続行できない。途中でエラー終了してしまう。
■エラーメッセージ
java.net.SocketException: Connection reset
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: '*****', ip: '*****', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Driver info: driver.version: HtmlUnitDriver

■スタックトレース
org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: '*****', ip: '*****', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_25'
Driver info: driver.version: HtmlUnitDriver
	at java.net.SocketInputStream.read(SocketInputStream.java:189)
	at java.net.SocketInputStream.read(SocketInputStream.java:121)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
	at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)
	at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)
	at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)
	at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.readLine(MultiThreadedHttpConnectionManager.java:1413)
	at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)
	at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)
	at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1098)
	at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
	at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
	at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:346)
	at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:97)
	at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1439)
	at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1370)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:326)
	at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:387)
	at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:220)
	at org.openqa.selenium.htmlunit.HtmlUnitDriver.get(HtmlUnitDriver.java:208)
	at geb.Browser.go(Browser.groovy:411)
	at geb.Browser.go(Browser.groovy:395)
	at geb.spock.GebSpec.methodMissing(GebSpec.groovy:54)

原因

  • 自動テスト対象のWEBサイトが攻撃とみなして接続を切断するため

解決

  • Gebのテスト実行を一定時間待ち状態にして1秒辺りのアクセス回数を制御する
    • Thread.sleep(<待ち時間(ms)>)
    • 以下例だと、1000ミリ秒(1秒)待ってから「go "http://example.com"」を実行する
    when:
        Thread.sleep(1000)
        go "http://example.com"