Skip to content

Commit

Permalink
Improved exception reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
wakaleo committed Jan 10, 2016
1 parent b5a6c97 commit 32aba8a
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import org.openqa.selenium.WebDriverException;

public class SerenityWebDriverException extends WebDriverException {
public class SerenityManagedException extends RuntimeException {

private final String detailedMessage;
private final StackTraceElement[] stackTrace;
Expand All @@ -14,27 +14,27 @@ public class SerenityWebDriverException extends WebDriverException {
public static Throwable detachedCopyOf(Throwable testErrorException) {
if (!(testErrorException instanceof WebDriverException)) {
return testErrorException;
} else if (testErrorException instanceof SerenityWebDriverException) {
} else if (testErrorException instanceof SerenityManagedException) {
return testErrorException;
} else {
return new SerenityWebDriverException((WebDriverException) testErrorException);
return new SerenityManagedException(testErrorException);
}
}


public SerenityWebDriverException(WebDriverException testErrorException) {
super(testErrorException.getCause());
public SerenityManagedException(Throwable testErrorException) {
super(testErrorException);
this.detailedMessage = testErrorException.getMessage();
this.stackTrace = testErrorException.getStackTrace();
this.cause = testErrorException.getCause();
this.exceptionClass = testErrorException.getClass();
}

public SerenityWebDriverException(String message, WebDriverException testErrorException) {
super(testErrorException.getCause());
public SerenityManagedException(String message, Throwable testErrorException) {
super(testErrorException);
this.detailedMessage = message;
this.stackTrace = testErrorException.getStackTrace();
this.cause = testErrorException.getCause();
this.cause = testErrorException.getCause();
this.exceptionClass = testErrorException.getClass();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.serenitybdd.core.exceptions.SerenityWebDriverException;
import net.serenitybdd.core.exceptions.SerenityManagedException;
import net.serenitybdd.core.model.FailureDetails;
import net.serenitybdd.core.time.SystemClock;
import net.thucydides.core.ThucydidesSystemProperty;
Expand Down Expand Up @@ -1112,7 +1112,7 @@ public void setUserStory(Story story) {

public void determineTestFailureCause(Throwable cause) {
if (cause != null) {
RootCauseAnalyzer rootCauseAnalyser = new RootCauseAnalyzer(SerenityWebDriverException.detachedCopyOf(cause));
RootCauseAnalyzer rootCauseAnalyser = new RootCauseAnalyzer(SerenityManagedException.detachedCopyOf(cause));
FailureCause rootCause = rootCauseAnalyser.getRootCause();
this.testFailureClassname = rootCauseAnalyser.getRootCause().getErrorType();
this.testFailureMessage = rootCauseAnalyser.getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import net.serenitybdd.core.exceptions.SerenityWebDriverException;
import net.serenitybdd.core.exceptions.SerenityManagedException;
import net.serenitybdd.core.exceptions.UnrecognisedException;
import net.thucydides.core.model.TestFailureException;
import net.thucydides.core.util.NameConverter;
Expand Down Expand Up @@ -36,8 +36,8 @@ public FailureCause(Throwable cause, StackTraceElement[] stackTrace) {
}

private static String exceptionClassName(Throwable cause) {
if (cause instanceof SerenityWebDriverException) {
return ((SerenityWebDriverException) cause).getExceptionClass().getName();
if (cause instanceof SerenityManagedException) {
return ((SerenityManagedException) cause).getExceptionClass().getName();
} else {
return cause.getClass().getName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.thucydides.core.model.stacktrace;

import net.serenitybdd.core.exceptions.SerenityWebDriverException;
import net.serenitybdd.core.exceptions.SerenityManagedException;
import net.thucydides.core.guice.Injectors;
import net.thucydides.core.model.failures.FailureAnalysis;
import net.thucydides.core.util.EnvironmentVariables;
Expand Down Expand Up @@ -30,7 +30,7 @@ public FailureCause getRootCause() {

private Throwable originalExceptionFrom(Throwable thrownException) {

if (!(thrownException instanceof WebdriverAssertionError) && ((thrownException instanceof SerenityWebDriverException) || (thrownException instanceof AssertionError))){
if (!(thrownException instanceof WebdriverAssertionError) && ((thrownException instanceof SerenityManagedException) || (thrownException instanceof AssertionError))){
return thrownException;
}
if (failureAnalysis.reportAsCompromised(thrownException.getClass())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.thucydides.core.steps;

import net.thucydides.core.webdriver.WebdriverAssertionError;
import net.serenitybdd.core.exceptions.SerenityManagedException;

import java.io.Serializable;

Expand All @@ -19,7 +19,8 @@ public Throwable convertToAssertion() {
if (RuntimeException.class.isAssignableFrom(throwable.getClass())) {
return throwable;
} else {
return new WebdriverAssertionError(throwable);
// return new WebdriverAssertionError(throwable);
return new SerenityManagedException(throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.thucydides.core.steps;


import net.serenitybdd.core.exceptions.SerenityManagedException;

/**
* Description and underlying cause behind a step failure.
* A <code>StepFailure</code> holds a description of the failed test step and the
Expand All @@ -21,9 +23,15 @@ public StepFailure(final ExecutedStepDescription description, final Throwable ca
this.description = description;
this.cause = cause;
if (cause != null) {
this.exceptionClass = cause.getClass();
this.message = cause.getMessage();
this.stackTraceElements = cause.getStackTrace();
if (cause instanceof SerenityManagedException) {
this.exceptionClass = ((SerenityManagedException)cause).getExceptionClass();
this.message = cause.getMessage();
this.stackTraceElements = ((SerenityManagedException)cause).getStackTrace();
} else {
this.exceptionClass = cause.getClass();
this.message = cause.getMessage();
this.stackTraceElements = cause.getStackTrace();
}
} else {
this.exceptionClass = null;
this.message = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.serenitybdd.core.PendingStepException;
import net.serenitybdd.core.Serenity;
import net.serenitybdd.core.SkipNested;
import net.serenitybdd.core.exceptions.SerenityWebDriverException;
import net.serenitybdd.core.exceptions.SerenityManagedException;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import net.thucydides.core.ThucydidesSystemProperty;
Expand Down Expand Up @@ -257,7 +257,7 @@ private boolean shouldRunInDryRunMode(final Method methodOrStep, final Class cal
}

public void reportMethodError(Throwable generalException, Object obj, Method method, Object[] args) throws Throwable {
error = SerenityWebDriverException.detachedCopyOf(generalException);
error = SerenityManagedException.detachedCopyOf(generalException);
Throwable assertionError = forError(error).convertToAssertion();
notifyStepStarted(obj, method, args);
notifyOfStepFailure(obj, method, args, assertionError);
Expand All @@ -268,7 +268,7 @@ private Object invokeMethodAndNotifyFailures(Object obj, Method method, Object[]
try {
result = invokeMethod(obj, args, proxy);
} catch (Throwable generalException) {
error = SerenityWebDriverException.detachedCopyOf(generalException);
error = SerenityManagedException.detachedCopyOf(generalException);
Throwable assertionError = forError(error).convertToAssertion();
notifyStepStarted(obj, method, args);
notifyOfStepFailure(obj, method, args, assertionError);
Expand Down Expand Up @@ -314,7 +314,7 @@ private Object runTestStep(final Object obj, final Method method,
} catch (AssumptionViolatedException assumptionFailed) {
result = appropriateReturnObject(obj, method);
} catch (Throwable testErrorException) {
error = SerenityWebDriverException.detachedCopyOf(testErrorException);
error = SerenityManagedException.detachedCopyOf(testErrorException);
logStepFailure(obj, method, args, forError(error).convertToAssertion());
result = appropriateReturnObject(obj, method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.google.common.base.Preconditions;
import io.appium.java_client.AppiumDriver;
import net.serenitybdd.core.Serenity;
import net.serenitybdd.core.exceptions.SerenityWebDriverException;
import net.serenitybdd.core.exceptions.SerenityManagedException;
import net.serenitybdd.core.pages.DefaultTimeouts;
import net.serenitybdd.core.pages.PageObject;
import net.thucydides.core.ThucydidesSystemProperty;
Expand Down Expand Up @@ -195,7 +195,7 @@ protected synchronized WebDriver newWebdriverInstance(final Class<? extends WebD

activateJavascriptSupportFor(driver);
return driver;
} catch (SerenityWebDriverException toPassThrough) {
} catch (SerenityManagedException toPassThrough) {
throw toPassThrough;
} catch (Exception cause) {
throw new UnsupportedDriverException("Could not instantiate " + driverClass, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import net.serenitybdd.core.buildinfo.DriverCapabilityRecord;
import net.serenitybdd.core.exceptions.SerenityWebDriverException;
import net.serenitybdd.core.exceptions.SerenityManagedException;
import net.thucydides.core.guice.Injectors;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
Expand Down Expand Up @@ -42,7 +42,7 @@ public WebDriver newRemoteDriver(URL remoteUrl, Capabilities capabilities) {
return driver;
} catch (UnreachableBrowserException unreachableBrowser) {
String errorMessage = unreachableBrowserErrorMessage(unreachableBrowser);
throw new SerenityWebDriverException(errorMessage, unreachableBrowser);
throw new SerenityManagedException(errorMessage, unreachableBrowser);

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.thucydides.core.webdriver

import com.opera.core.systems.OperaDriver
import net.serenitybdd.core.exceptions.SerenityWebDriverException
import net.serenitybdd.core.exceptions.SerenityManagedException
import net.thucydides.core.util.MockEnvironmentVariables
import org.openqa.selenium.Capabilities
import org.openqa.selenium.WebDriver
Expand Down Expand Up @@ -141,7 +141,7 @@ class WhenConfiguringTheWebdriverInstance extends Specification {
def webDriverFactory = new WebDriverFactory(new WebdriverInstanceFactory(), environmentVariables)
webDriverFactory.newInstanceOf(SupportedWebDriver.REMOTE)
then:
def e = thrown(SerenityWebDriverException)
def e = thrown(SerenityManagedException)
e.message.contains("Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.")
e.message.contains("UnknownHostException - host-does-not-exist")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.thucydides.core.steps;

import net.serenitybdd.core.exceptions.SerenityWebDriverException;
import net.serenitybdd.core.exceptions.SerenityManagedException;
import net.thucydides.core.annotations.*;
import net.thucydides.core.model.TestOutcome;
import net.thucydides.core.pages.Pages;
Expand Down Expand Up @@ -642,7 +642,7 @@ public void the_proxy_should_notify_listeners_with_a_description_and_a_cause_whe
verify(listener).stepFailed(argument.capture());
assertThat(argument.getValue().getDescription().getStepClass().getName(), is(SimpleTestScenarioSteps.class.getName()));
assertThat(argument.getValue().getDescription().getName(), is("failing_web_step"));
assertThat(argument.getValue().getException().getClass().getName(), is(SerenityWebDriverException.class.getName()));
assertThat(argument.getValue().getException().getClass().getName(), is(SerenityManagedException.class.getName()));

verify(listener, times(1)).stepFailed(any(StepFailure.class));

Expand All @@ -665,7 +665,7 @@ public void the_proxy_should_notify_listeners_with_a_description_and_a_cause_whe

ArgumentCaptor<StepFailure> argument = ArgumentCaptor.forClass(StepFailure.class);
verify(listener).stepFailed(argument.capture());
assertThat(argument.getValue().getMessage(), is("java.lang.AssertionError: Oops!"));
assertThat(argument.getValue().getMessage(), is("Oops!"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.jayway.restassured.specification.RequestSpecification;
import com.jayway.restassured.specification.ResponseSpecification;
import net.serenitybdd.core.Serenity;
import net.serenitybdd.core.exceptions.SerenityWebDriverException;
import net.serenitybdd.core.exceptions.SerenityManagedException;
import net.serenitybdd.core.rest.RestMethod;
import net.serenitybdd.core.rest.RestQuery;
import net.serenitybdd.rest.decorators.RestDecorator;
Expand Down Expand Up @@ -174,7 +174,7 @@ private static Object wrappedResult(Method method, Object target, Object[] args)
return result;
}
} catch (Exception generalException) {
Throwable error = SerenityWebDriverException.detachedCopyOf(generalException.getCause());
Throwable error = SerenityManagedException.detachedCopyOf(generalException.getCause());
Throwable assertionError = forError(error).convertToAssertion();
notifyOfStepFailure(method, args, assertionError);
return stubbed(method);
Expand Down

0 comments on commit 32aba8a

Please sign in to comment.