Example usage for java.lang Throwable setStackTrace

List of usage examples for java.lang Throwable setStackTrace

Introduction

In this page you can find the example usage for java.lang Throwable setStackTrace.

Prototype

public void setStackTrace(StackTraceElement[] stackTrace) 

Source Link

Document

Sets the stack trace elements that will be returned by #getStackTrace() and printed by #printStackTrace() and related methods.

Usage

From source file:org.auraframework.test.util.WebDriverTestCase.java

/**
 * Gather up useful info to add to a test failure. try to get
 * <ul>/*from ww  w  . ja  v a2  s. c o  m*/
 * <li>any client js errors</li>
 * <li>last known js test function</li>
 * <li>running/waiting</li>
 * <li>a screenshot</li>
 * </ul>
 * 
 * @param originalErr the test failure
 * @throws Throwable a new AssertionFailedError or UnexpectedError with the original and additional info
 */
private Throwable addAuraInfoToTestFailure(Throwable originalErr) {
    StringBuffer description = new StringBuffer();
    if (originalErr != null) {
        String msg = originalErr.getMessage();
        if (msg != null) {
            description.append(msg);
        }
    }

    description.append(String.format("\nBrowser: %s", currentBrowserType));
    if (auraUITestingUtil != null) {
        description.append("\nUser-Agent: " + auraUITestingUtil.getUserAgent());
    }
    if (currentDriver == null) {
        description.append("\nTest failed before WebDriver was initialized");
    } else {

        if (this instanceof PerfExecutorTest) {
            JSONArray json = this.getLastCollectedMetrics();
            description.append("\nPerfMetrics: " + json + ';');
        }

        description.append("\nWebDriver: " + currentDriver);
        description.append("\nJS state: ");
        try {
            String dump = (String) auraUITestingUtil
                    .getRawEval("return (window.$A && $A.test && $A.test.getDump())||'';");
            if (dump.isEmpty()) {
                description.append("no errors detected");
            } else {
                description.append(dump);
            }
        } catch (Throwable t) {
            description.append(t.getMessage());
        }

        String screenshotsDirectory = System.getProperty("screenshots.directory");
        if (screenshotsDirectory != null) {
            String img = getBase64EncodedScreenshot(originalErr, true);
            if (img == null) {
                description.append("\nScreenshot: {not available}");
            } else {
                String fileName = getClass().getName() + "." + getName() + "_" + currentBrowserType + ".png";
                File path = new File(screenshotsDirectory + "/" + fileName);
                try {
                    path.getParentFile().mkdirs();
                    byte[] bytes = Base64.decodeBase64(img.getBytes());
                    FileOutputStream fos = new FileOutputStream(path);
                    fos.write(bytes);
                    fos.close();
                    String baseUrl = System.getProperty("screenshots.baseurl");
                    description.append(String.format("%nScreenshot: %s/%s", baseUrl, fileName));
                } catch (Throwable t) {
                    description.append(String.format("%nScreenshot: {save error: %s}", t.getMessage()));
                }
            }
        }

        try {
            description.append("\nApplication cache status: ");
            description.append(auraUITestingUtil.getRawEval(
                    "var cache=window.applicationCache;return (cache===undefined || cache===null)?'undefined':cache.status;")
                    .toString());
        } catch (Exception ex) {
            description.append("error calculating status: " + ex);
        }
        description.append("\n");
        if (SauceUtil.areTestsRunningOnSauce()) {
            String linkToJob = SauceUtil.getLinkToPublicJobInSauce(currentDriver);
            description.append("\nSauceLabs-recording: ");
            description.append((linkToJob != null) ? linkToJob : "{not available}");
        }
    }

    // replace original exception with new exception with additional info
    Throwable newFailure;
    if (originalErr instanceof AssertionFailedError) {
        newFailure = new AssertionFailedError(description.toString());
    } else {
        description.insert(0, originalErr.getClass() + ": ");
        newFailure = new UnexpectedError(description.toString(), originalErr.getCause());
    }
    newFailure.setStackTrace(originalErr.getStackTrace());
    return newFailure;
}