Skip to content

Commit

Permalink
#1543 add stack trace to every error in SoftAsserts
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Sep 27, 2021
1 parent 1b89f34 commit 6f7d9d7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 5.25.0
* #969 Add support for OpenTest4j -- see PR #1545
* #1543 add stack trace to every error in SoftAsserts -- see PR #1545
* #1515 add method $.shadowRoot() -- see PR #1517
* #1556 add method `SelenideElement.ancestor()` -- thanks Oleg Berezhnoy for PR #1567
* #1571 fix method `$.screenshot()` on Retina display -- see PR #1576
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.codeborne.selenide.ex;

import org.opentest4j.MultipleFailuresError;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;

@ParametersAreNonnullByDefault
public class SoftAssertionError extends AssertionError {
public SoftAssertionError(String message) {
super(message);
public class SoftAssertionError extends MultipleFailuresError {
public SoftAssertionError(String message, List<? extends Throwable> failures) {
super(message, failures);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import static com.codeborne.selenide.AssertionMode.SOFT;
import static com.codeborne.selenide.logevents.LogEvent.EventStatus.FAIL;
import static java.lang.System.lineSeparator;
import static java.util.Collections.unmodifiableList;

@ParametersAreNonnullByDefault
Expand Down Expand Up @@ -48,20 +47,12 @@ public void failIfErrors(String testName) {
List<Throwable> errors = new ArrayList<>(this.errors);
this.errors.clear();

if (errors.size() == 1) {
throw new SoftAssertionError(errors.get(0).toString());
if (errors.size() == 1 && errors.get(0) instanceof AssertionError) {
throw (AssertionError) errors.get(0);
}
if (!errors.isEmpty()) {
StringBuilder sb = new StringBuilder();
sb.append("Test ").append(testName).append(" failed.").append(lineSeparator());
sb.append(errors.size()).append(" checks failed").append(lineSeparator());

int i = 0;
for (Throwable error : errors) {
sb.append(lineSeparator()).append("FAIL #").append(++i).append(": ");
sb.append(error).append(lineSeparator());
}
throw new SoftAssertionError(sb.toString());
String message = String.format("Test %s failed", testName);
throw new SoftAssertionError(message, errors);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.List;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;
Expand All @@ -18,14 +19,15 @@ final class ErrorsCollectorTest {
private final LogEvent mockedPassedEvent = mock(LogEvent.class);
private final LogEvent mockedFailedEvent = mock(LogEvent.class);
private final String defaultErrorMessage = "Couldn't find an element";
private final StaleElementReferenceException defaultError = new StaleElementReferenceException(defaultErrorMessage);
private final String defaultTestName = "ITestName";

@BeforeEach
void setup() {
when(mockedInProgressEvent.getStatus()).thenReturn(LogEvent.EventStatus.IN_PROGRESS);
when(mockedPassedEvent.getStatus()).thenReturn(LogEvent.EventStatus.PASS);
when(mockedFailedEvent.getStatus()).thenReturn(LogEvent.EventStatus.FAIL);
when(mockedFailedEvent.getError()).thenReturn(new StaleElementReferenceException(defaultErrorMessage));
when(mockedFailedEvent.getError()).thenReturn(defaultError);
}

@Test
Expand Down Expand Up @@ -83,9 +85,9 @@ void failIfErrorMethodWhenOnlyOneError() {
@Test
void failIfErrorMethodWhenMoreThenOneError() {
LogEvent mockedFailedEvent2 = mock(LogEvent.class);
String failedEvent2Message = "Second failure";
StaleElementReferenceException failedEvent2Error = new StaleElementReferenceException("Second failure");
when(mockedFailedEvent2.getStatus()).thenReturn(LogEvent.EventStatus.FAIL);
when(mockedFailedEvent2.getError()).thenReturn(new StaleElementReferenceException(failedEvent2Message));
when(mockedFailedEvent2.getError()).thenReturn(failedEvent2Error);

errorsCollector.afterEvent(mockedFailedEvent);
errorsCollector.afterEvent(mockedFailedEvent2);
Expand All @@ -95,17 +97,9 @@ void failIfErrorMethodWhenMoreThenOneError() {
}
catch (SoftAssertionError error) {
assertThat(error)
.as("Error title")
.hasMessageContaining(String.format("Test %s failed.", defaultTestName));
assertThat(error)
.as("Record about number of failed checks")
.hasMessageContaining("2 checks failed");
assertThat(error)
.as("First event message")
.hasMessageContaining(String.format("FAIL #1: org.openqa.selenium.StaleElementReferenceException: %s", defaultErrorMessage));
assertThat(error)
.as("Second event message is missing")
.hasMessageContaining(String.format("FAIL #2: org.openqa.selenium.StaleElementReferenceException: %s", failedEvent2Message));
.hasMessageStartingWith("Test " + defaultTestName + " failed (2 failures)");
assertThat(error.getFailures())
.isEqualTo(asList(defaultError, failedEvent2Error));
}
}
}
6 changes: 3 additions & 3 deletions statics/src/test/java/integration/SoftAssertJUnit5Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.junit5.SoftAssertsExtension;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -16,8 +16,8 @@

@ExtendWith({SoftAssertsExtension.class})
public class SoftAssertJUnit5Test extends IntegrationTest {
@BeforeAll
static void setUp() {
@BeforeEach
void setUp() {
Configuration.assertionMode = SOFT;
open("https://duckduckgo.com/");
$("#soft-assert-login").shouldNot(exist);
Expand Down

0 comments on commit 6f7d9d7

Please sign in to comment.