java.io.IOException: 主机中的软件使用 selenium 中的 Sendkeys() 中止了连接 java

java.io.IOException: Connection was aborted by the software in your host machine with Sendkey() in selenium java

OS: Win7 64位 Browser:Chrome

我正在完成我的任务,用 selenium 测试一个程序。 我正在使用教授提供的代码,它在我的 localhost:8080 上创建了一个网络应用程序(因此应该没有任何连接问题)

public void canConenctTwoPlayers() {
    // connect
    //2: missing code
    this.indexPage.connect.click();
    assertThat(this.indexPage.hasText("Successfully connected to the game with unique "), is(true));
    this.indexPage.numberPlayers.click();
    this.indexPage.numberPlayers.sendKeys("2");
    this.indexPage.open.click();
    //second connect
    ChromeDriver second_player = quickConnectAnotherUser();
    //3: missing code
    assertThat(this.indexPage.hasText("The game is now ready to begin"), is(true));
    // quit
    //4: missing code
    disconnectSecondUser(second_player);
    this.indexPage.disconnect.click();      
}

这里的numberPlayers是window中的元素,顾名思义,输入玩家的数量。但是当我使用 sendKeys 时,它给了我这个例外

java.io.IOException: java.util.concurrent.ExecutionException: 
java.io.IOException: Connection was aborted by the software in your host machine

当我注释掉 sendkeys 时,代码运行良好。

2018-11-30 13:24:52.749  INFO 18632 --- [nio-8080-exec-1] c.c.poker.game.PokerSocketHandler        : Opened new session for _fc6yznc.
2018-11-30 13:24:52.749  INFO 18632 --- [nio-8080-exec-1] ca.carleton.poker.game.PokerGame         : Player is trying to connect _fc6yznc, WebSocketServerSockJsSession[id=_fc6yznc]
2018-11-30 13:24:52.749  INFO 18632 --- [nio-8080-exec-1] ca.carleton.poker.game.PokerGame         : Setting first player as admin:_fc6yznc.
2018-11-30 13:24:52.749  INFO 18632 --- [nio-8080-exec-1] ca.carleton.poker.game.PokerGame         : Player is being added _fc6yznc, WebSocketServerSockJsSession[id=_fc6yznc]
2018-11-30 13:24:52.750  INFO 18632 --- [nio-8080-exec-1] ca.carleton.poker.game.PokerGame         : Sessions WebSocketServerSockJsSession[id=_fc6yznc]
2018-11-30 13:24:52.750  INFO 18632 --- [nio-8080-exec-1] c.c.poker.game.PokerSocketHandler        : Sending admin message to player.
2018-11-30 13:24:52.750  INFO 18632 --- [nio-8080-exec-1] ca.carleton.poker.game.PokerGame         : Current number of players is 1. Required number is 4.
2018-11-30 13:24:52.872 ERROR 18632 --- [nio-8080-exec-2] c.c.poker.game.PokerSocketHandler        : TRANSPORT ERROR - Error with the network.

java.io.IOException: java.util.concurrent.ExecutionException: java.io.IOException: Connection was aborted by the software in your host machine
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:282)
at org.apache.tomcat.websocket.WsSession.sendCloseMessage(WsSession.java:570)
at org.apache.tomcat.websocket.WsSession.onClose(WsSession.java:510)
at org.apache.tomcat.websocket.WsFrameBase.processDataControl(WsFrameBase.java:342)
at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:284)
at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:130)
at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:60)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:203)
at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:198)
at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:96)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:663)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: java.util.concurrent.ExecutionException: java.io.IOException: Connection was aborted by the software in your host machine
at org.apache.tomcat.websocket.FutureToSendHandler.get(FutureToSendHandler.java:102)
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:275)
... 16 common frames omitted
Caused by: java.io.IOException: Connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(Unknown Source)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.write(Unknown Source)
at sun.nio.ch.SocketChannelImpl.write(Unknown Source)
at org.apache.tomcat.util.net.NioChannel.write(NioChannel.java:127)
at org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:184)
at org.apache.coyote.http11.upgrade.NioServletOutputStream.doWriteInternal(NioServletOutputStream.java:94)
at org.apache.coyote.http11.upgrade.NioServletOutputStream.doWrite(NioServletOutputStream.java:61)
at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.writeInternal(AbstractServletOutputStream.java:165)
at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.write(AbstractServletOutputStream.java:132)
at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.onWritePossible(WsRemoteEndpointImplServer.java:98)
at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.doWrite(WsRemoteEndpointImplServer.java:80)
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.writeMessagePart(WsRemoteEndpointImplBase.java:450)
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessage(WsRemoteEndpointImplBase.java:338)
at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:270)
... 16 common frames omitted

2018-11-30 13:24:52.872  INFO 18632 --- [nio-8080-exec-2] c.c.poker.game.PokerSocketHandler        : Closing session for _fc6yznc with status CloseStatus[code=1000, reason=null].
2018-11-30 13:24:52.872  INFO 18632 --- [nio-8080-exec-2] c.c.poker.game.PokerSocketHandler        : Disabling all accounts because the admin left.
2018-11-30 13:24:52.873  INFO 18632 --- [nio-8080-exec-2] ca.carleton.poker.game.PokerGame         : Sessions WebSocketServerSockJsSession[id=_fc6yznc]
2018-11-30 13:24:52.873  INFO 18632 --- [nio-8080-exec-2] c.carleton.poker.session.SessionHandler  : Registered _fc6yznc for disconnect.

这个错误信息...

java.io.IOException: java.util.concurrent.ExecutionException: 
java.io.IOException: Connection was aborted by the software in your host machine

...表示 异常 是由于 并发执行 .

而引发的

根据讨论 this issue is observed when you attempt to invoke sendKeys() when a JavaScript / jQuery 仍然活跃。

解决方案

解决方案是为 JavaScript / jQuery to complete using either of the ExpectedConditions:

引入 WebDriverWait