Skip to content

Commit

Permalink
#969 Add support for OpenTest4j in other assertion errors too
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Sep 27, 2021
1 parent 6f7d9d7 commit ec61267
Show file tree
Hide file tree
Showing 33 changed files with 193 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/codeborne/selenide/Modal.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private String getLogSubject(@Nullable String expectedDialogText) {

private static void checkDialogText(Driver driver, @Nullable String expectedDialogText, String actualDialogText) {
if (expectedDialogText != null && !expectedDialogText.equals(actualDialogText)) {
DialogTextMismatch assertionError = new DialogTextMismatch(driver, actualDialogText, expectedDialogText);
DialogTextMismatch assertionError = new DialogTextMismatch(driver, expectedDialogText, actualDialogText);
throw UIAssertionError.wrap(driver, assertionError, driver.config().timeout());
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/codeborne/selenide/ObjectCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public interface ObjectCondition<T> {
@CheckReturnValue
String actualValue(T object);

@Nullable
@CheckReturnValue
String expectedValue();

@Nonnull
@CheckReturnValue
String describe(T object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private Error windowNotFoundError(String message, Throwable cause) {
}

private Error alertNotFoundError(Throwable cause) {
AlertNotFoundException error = new AlertNotFoundException(driver, "Alert not found", cause);
AlertNotFoundException error = new AlertNotFoundException(driver, cause);
return UIAssertionError.wrap(driver, error, config.timeout());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void fail(CollectionSource collection,
List<String> difference = new ArrayList<>(expectedTexts);
difference.removeAll(actualTexts);
throw new DoesNotContainTextsError(collection,
actualTexts, expectedTexts, difference, explanation,
expectedTexts, actualTexts, difference, explanation,
timeoutMs, lastError);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.codeborne.selenide.ex.ElementNotFound;
import com.codeborne.selenide.ex.TextsMismatch;
import com.codeborne.selenide.ex.TextsSizeMismatch;
import com.codeborne.selenide.impl.Html;
import com.codeborne.selenide.impl.CollectionSource;
import com.codeborne.selenide.impl.Html;
import org.openqa.selenium.WebElement;

import javax.annotation.CheckReturnValue;
Expand Down Expand Up @@ -63,7 +63,7 @@ else if (elements.size() != expectedTexts.size()) {
throw new TextsSizeMismatch(collection, ElementsCollection.texts(elements), expectedTexts, explanation, timeoutMs);
}
else {
throw new TextsMismatch(collection, ElementsCollection.texts(elements), expectedTexts, explanation, timeoutMs);
throw new TextsMismatch(collection, expectedTexts, ElementsCollection.texts(elements), explanation, timeoutMs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void fail(CollectionSource collection,
@Nullable Exception lastError,
long timeoutMs) {
throw new ElementWithTextNotFound(
collection, ElementsCollection.texts(elements), singletonList(expectedText), explanation, timeoutMs, lastError);
collection, singletonList(expectedText), ElementsCollection.texts(elements), explanation, timeoutMs, lastError);
}

@CheckReturnValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public String actualValue(Clipboard clipboard) {
return clipboard.getText();
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return expectedContent;
}

@Nonnull
@CheckReturnValue
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public String actualValue(LocalStorage localStorage) {
return localStorage.getItems().toString();
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return item;
}

@CheckReturnValue
@Override
public boolean test(LocalStorage localStorage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public String actualValue(LocalStorage localStorage) {
return localStorage.getItems().toString();
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return String.format("%s=%s", item, value);
}

@CheckReturnValue
@Override
public boolean test(LocalStorage localStorage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public String actualValue(SessionStorage sessionStorage) {
return sessionStorage.getItems().toString();
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return item;
}

@CheckReturnValue
@Override
public boolean test(SessionStorage sessionStorage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public String actualValue(SessionStorage sessionStorage) {
return sessionStorage.getItems().toString();
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return String.format("%s=%s", item, value);
}

@CheckReturnValue
@Override
public boolean test(SessionStorage sessionStorage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@ParametersAreNonnullByDefault
public abstract class CurrentFrameCondition implements ObjectCondition<WebDriver> {
protected final String name;
private final String name;
protected final String expectedUrl;

protected CurrentFrameCondition(String name, String expectedUrl) {
Expand Down Expand Up @@ -40,6 +40,13 @@ public String actualValue(WebDriver webDriver) {
return getCurrentFrameUrl(webDriver);
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return expectedUrl;
}

@CheckReturnValue
@Nonnull
protected String getCurrentFrameUrl(WebDriver webDriver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public String actualValue(WebDriver webDriver) {
return String.valueOf(webDriver.getWindowHandles().size());
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return String.valueOf(expectedNumberOfWindows);
}

@Nonnull
@CheckReturnValue
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public String actualValue(WebDriver webDriver) {
return webDriver.getTitle();
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return title;
}

@Nonnull
@Override
public String description() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@ParametersAreNonnullByDefault
public abstract class UrlCondition implements ObjectCondition<WebDriver> {
protected final String name;
private final String name;
protected final String expectedUrl;

protected UrlCondition(String name, String expectedUrl) {
Expand All @@ -25,6 +25,13 @@ public String actualValue(WebDriver webDriver) {
return webDriver.getCurrentUrl();
}

@Override
@Nullable
@CheckReturnValue
public String expectedValue() {
return expectedUrl;
}

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

@ParametersAreNonnullByDefault
public class AlertNotFoundException extends UIAssertionError {
public AlertNotFoundException(Driver driver, String message, Throwable cause) {
super(driver, message, cause);
public AlertNotFoundException(Driver driver, Throwable cause) {
super(driver, "Alert not found", cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

import javax.annotation.ParametersAreNonnullByDefault;

import static com.codeborne.selenide.ex.ErrorMessages.actualValue;
import static com.codeborne.selenide.ex.ErrorMessages.extractActualValue;

@ParametersAreNonnullByDefault
public class ConditionMetException extends UIAssertionError {
public class ConditionMetException extends ObjectConditionError {
public <T> ConditionMetException(Driver driver, ObjectCondition<T> condition, T subject) {
super(driver, condition.describe(subject) + " " + condition.negativeDescription() + actualValue(condition, subject));
super(driver,
condition.describe(subject) + " " + condition.negativeDescription(),
condition.expectedValue(),
extractActualValue(condition, subject)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

import javax.annotation.ParametersAreNonnullByDefault;

import static com.codeborne.selenide.ex.ErrorMessages.actualValue;
import static com.codeborne.selenide.ex.ErrorMessages.extractActualValue;

@ParametersAreNonnullByDefault
public class ConditionNotMetException extends UIAssertionError {
public class ConditionNotMetException extends ObjectConditionError {
public <T> ConditionNotMetException(Driver driver, ObjectCondition<T> condition, T subject) {
super(driver, condition.describe(subject) + " " + condition.description() + actualValue(condition, subject));
super(driver,
condition.describe(subject) + " " + condition.description(),
condition.expectedValue(),
extractActualValue(condition, subject)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

@ParametersAreNonnullByDefault
public class DialogTextMismatch extends UIAssertionError {
public DialogTextMismatch(Driver driver, String actualText, String expectedText) {
public DialogTextMismatch(Driver driver, String expectedText, String actualText) {
super(driver,
String.format("Dialog text mismatch" +
"%nActual: %s" +
"%nExpected: %s", actualText, expectedText));
"%nExpected: %s", actualText, expectedText),
expectedText, actualText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
public class DoesNotContainTextsError extends UIAssertionError {

public DoesNotContainTextsError(CollectionSource collection,
List<String> actualTexts, List<String> expectedTexts, List<String> difference,
List<String> expectedTexts, List<String> actualTexts, List<String> difference,
@Nullable String explanation, long timeoutMs, @Nullable Throwable lastError) {
super(collection.driver(),
"The collection with text elements: " + actualTexts +
lineSeparator() + "should contain all of the following text elements: " + expectedTexts +
(explanation == null ? "" : lineSeparator() + "Because: " + explanation) +
lineSeparator() + "but could not find these elements: " + difference +
lineSeparator() + "Collection: " + collection.description(),
expectedTexts,
actualTexts,
lastError);
super.timeoutMs = timeoutMs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
@ParametersAreNonnullByDefault
public class ElementWithTextNotFound extends UIAssertionError {

public ElementWithTextNotFound(CollectionSource collection, List<String> actualTexts,
List<String> expectedTexts, @Nullable String explanation,
public ElementWithTextNotFound(CollectionSource collection,
List<String> expectedTexts, List<String> actualTexts,
@Nullable String explanation,
long timeoutMs, @Nullable Throwable lastError) {
super(collection.driver(),
"Element with text not found" +
lineSeparator() + "Actual: " + actualTexts +
lineSeparator() + "Expected: " + expectedTexts +
(explanation == null ? "" : lineSeparator() + "Because: " + explanation) +
lineSeparator() + "Collection: " + collection.description(),
expectedTexts,
actualTexts,
lastError);
super.timeoutMs = timeoutMs;
}
Expand Down
35 changes: 23 additions & 12 deletions src/main/java/com/codeborne/selenide/ex/ErrorMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ErrorMessages {
private static final DurationFormat df = new DurationFormat();

@CheckReturnValue
@Nonnull
protected static String timeout(long timeoutMs) {
return String.format("%nTimeout: %s", df.format(timeoutMs));
}
Expand All @@ -45,22 +46,32 @@ static String actualValue(Condition condition, Driver driver, @Nullable WebEleme
@CheckReturnValue
@Nonnull
static <T> String actualValue(ObjectCondition<T> condition, @Nullable T object) {
if (object != null) {
try {
Object actualValue = condition.actualValue(object);
if (actualValue != null) {
return String.format("%nActual value: %s", actualValue);
}
}
catch (RuntimeException failedToGetValue) {
String failedActualValue = failedToGetValue.getClass().getSimpleName() + ": " + failedToGetValue.getMessage();
return String.format("%nActual value: %s", substring(failedActualValue, 0, 50));
}
if (object == null) {
return "";
}
return formatActualValue(extractActualValue(condition, object));
}

@CheckReturnValue
@Nullable
static <T> String extractActualValue(ObjectCondition<T> condition, @Nonnull T object) {
try {
return condition.actualValue(object);
}
catch (RuntimeException failedToGetValue) {
String failedActualValue = failedToGetValue.getClass().getSimpleName() + ": " + failedToGetValue.getMessage();
return substring(failedActualValue, 0, 50);
}
return "";
}

@CheckReturnValue
@Nonnull
static <T> String formatActualValue(@Nullable String actualValue) {
return actualValue == null ? "" : String.format("%nActual value: %s", actualValue);
}

@CheckReturnValue
@Nonnull
static String causedBy(@Nullable Throwable cause) {
if (cause == null) {
return "";
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/codeborne/selenide/ex/ListSizeMismatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.codeborne.selenide.impl.CollectionSource;
import org.openqa.selenium.WebElement;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Collection;
import java.util.List;

import static com.codeborne.selenide.ElementsCollection.elementsToString;
Expand All @@ -22,10 +24,18 @@ public ListSizeMismatch(Driver driver, String operator, int expectedSize,
super(driver,
"List size mismatch: expected: " + operator + ' ' + expectedSize +
(explanation == null ? "" : " (because " + explanation + ")") +
", actual: " + (actualElements == null ? 0 : actualElements.size()) +
", actual: " + sizeOf(actualElements) +
", collection: " + collection.description() +
lineSeparator() + "Elements: " + elementsToString(collection.driver(), actualElements), lastError
lineSeparator() + "Elements: " + elementsToString(collection.driver(), actualElements),
expectedSize,
sizeOf(actualElements),
lastError
);
super.timeoutMs = timeoutMs;
}

@CheckReturnValue
private static int sizeOf(@Nullable Collection<?> collection) {
return collection == null ? 0 : collection.size();
}
}
Loading

0 comments on commit ec61267

Please sign in to comment.