我正在运行Selenium测试,我不希望线程结束,所以我正在抓住我能做的一切。这是我的程序的结构
My Main.java
for (int i = 0; i < numberOfThreads; i++)
{
System.out.println("Inside loop to create threads!");
System.out.println("Number of threads to create: " + numberOfThreads);
totalNumberOfThreads++;
String threadName = "Thread" + totalNumberOfThreads;
if (type.contains("auto"))
{
newThread = new Thread(new Test());
}
UncaughtExceptionHandler handler = new UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread t, Throwable ex)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(new File("throwable.txt"), true);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintStream ps = new PrintStream(fos);
ex.printStackTrace(ps);
}
};
newThread.setName(threadName);
newThread.setUncaughtExceptionHandler(handler);
newThread.start();
}
Test.java
public class Test extends Common implements Runnable
{
@Override
public void run()
{
WebDriver driver = null;
while (true)
{
try
{
driver = new PhantomJSDriver(caps);
// bunch of test code
}
catch (Exception e)
{
e.printStackTrace();
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
driver.quit();
}
}
}
}
即使我在Test.java中捕获Exceptions和Throwables,我仍然会在throwable.txt中不时看到以下异常,这意味着错误没有在Test.java中被捕获
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46'
System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.1.5-x86_64-linode61', java.version: '1.8.0_60'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:589)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:618)
at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:464)
at myprogram.Test.run(Test.java:155)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.openqa.selenium.WebDriverException: The PhantomJS/GhostDriver server has unexpectedly died!
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46'
System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.1.5-x86_64-linode61', java.version: '1.8.0_60'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.phantomjs.PhantomJSCommandExecutor.execute(PhantomJSCommandExecutor.java:88)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:568)
... 4 more
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:30041 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:143)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:89)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:134)
at org.openqa.selenium.phantomjs.PhantomJSCommandExecutor.execute(PhantomJSCommandExecutor.java:82)
... 5 more
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
... 18 more
答案 0 :(得分:2)
检查堆栈跟踪,您可以看到driver.quit()在finally中抛出了异常。如果你想抓住这些东西,你必须通过另一个试试{}
包围quit()的调用at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:464)
at myprogram.Test.run(Test.java:155)
答案 1 :(得分:0)
你的终于仍未被try / catch包围。
您需要以下内容:
finally{
try{
driver.quit();
}catch(Exception ex){
//do something
}
}