java - 异常和可抛出异常未被捕获

标签 java selenium phantomjs

我正在运行 Selenium 测试,我不希望线程结束,所以我正在捕获我能捕获的一切。这是我的程序的结构

我的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();
}

测试.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 中捕获异常和 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

最佳答案

检查堆栈跟踪,可以看到异常是由finally中的driver.quit()引发的。如果您想捕获这些,您必须用另一个 try{}

包围对 quit() 的调用
at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:464)
at myprogram.Test.run(Test.java:155)

关于java - 异常和可抛出异常未被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32647228/

相关文章:

java - 如何在 java 中从 List<String> 中删除和设置?

java - 使用 Window 的 FLAG_FULLSCREEN 时 ICS 和 Jelly Bean 之间的不同行为

java - 使用不同的参数在TestNG中重复整个测试类

java - 即使在添加 set property webdriver 之后也获取 java.lang.IllegalStateException

phantomjs - 如何设置环境变量 PHANTOMJS_BIN

javascript - CasperJS click() 不加载新的 html

java - 检查事件是否正确并根据结果启用按钮

java - 当不使用 else 语句时会发生什么?

java - 使用正则表达式从字符串中提取数字

javascript - CasperJS填写没有表单id的表单