diff --git a/src/main/java/com/codeborne/selenide/CollectionCondition.java b/src/main/java/com/codeborne/selenide/CollectionCondition.java
index e8a87516bc..26fe67c1fe 100644
--- a/src/main/java/com/codeborne/selenide/CollectionCondition.java
+++ b/src/main/java/com/codeborne/selenide/CollectionCondition.java
@@ -49,7 +49,7 @@ public static CollectionCondition sizeNotEqual(int expectedSize) {
/**
* Checks that given collection has given texts (each collection element CONTAINS corresponding text)
- *
+ *
*
NB! Ignores multiple whitespaces between words
*/
public static CollectionCondition texts(String... expectedTexts) {
@@ -58,7 +58,7 @@ public static CollectionCondition texts(String... expectedTexts) {
/**
* Checks that given collection has given texts (each collection element CONTAINS corresponding text)
- *
+ *
*
NB! Ignores multiple whitespaces between words
*/
public static CollectionCondition texts(List expectedTexts) {
@@ -67,7 +67,7 @@ public static CollectionCondition texts(List expectedTexts) {
/**
* Checks that given collection has given texts in any order (each collection element CONTAINS corresponding text)
- *
+ *
*
NB! Ignores multiple whitespaces between words
*/
public static CollectionCondition textsInAnyOrder(String... expectedTexts) {
@@ -76,7 +76,7 @@ public static CollectionCondition textsInAnyOrder(String... expectedTexts) {
/**
* Checks that given collection has given texts in any order (each collection element CONTAINS corresponding text)
- *
+ *
*
NB! Ignores multiple whitespaces between words
*/
public static CollectionCondition textsInAnyOrder(List expectedTexts) {
@@ -85,7 +85,7 @@ public static CollectionCondition textsInAnyOrder(List expectedTexts) {
/**
* Checks that given collection has given texts (each collection element EQUALS TO corresponding text)
- *
+ *
*
NB! Ignores multiple whitespaces between words
*/
public static CollectionCondition exactTexts(String... expectedTexts) {
@@ -94,10 +94,46 @@ public static CollectionCondition exactTexts(String... expectedTexts) {
/**
* Checks that given collection has given texts (each collection element EQUALS TO corresponding text)
- *
+ *
*
NB! Ignores multiple whitespaces between words
*/
public static CollectionCondition exactTexts(List expectedTexts) {
return new ExactTexts(expectedTexts);
}
+
+ /**
+ * Should be used for explaining the reason of condition
+ */
+ public CollectionCondition because(String message) {
+ return new ExplainedCollectionCondition(this, message);
+ }
+
+ private static class ExplainedCollectionCondition extends CollectionCondition {
+ private final CollectionCondition delegate;
+ private final String message;
+
+ private ExplainedCollectionCondition(CollectionCondition delegate, String message) {
+ this.delegate = delegate;
+ this.message = message;
+ }
+
+ @Override
+ public void fail(WebElementsCollection collection, List elements, Exception lastError, long timeoutMs) {
+ delegate.fail(collection, elements, lastError, timeoutMs);
+ }
+
+ @Override
+ public boolean apply(List element) {
+ return delegate.apply(element);
+ }
+
+ @Override
+ public String toString() {
+ return delegate.toString() + " (because " + message + ")";
+ }
+
+
+ }
+
+
}
diff --git a/src/test/java/com/codeborne/selenide/collections/CollectionConditionReasonTest.java b/src/test/java/com/codeborne/selenide/collections/CollectionConditionReasonTest.java
new file mode 100644
index 0000000000..ecfc2e8501
--- /dev/null
+++ b/src/test/java/com/codeborne/selenide/collections/CollectionConditionReasonTest.java
@@ -0,0 +1,21 @@
+package com.codeborne.selenide.collections;
+
+import com.codeborne.selenide.CollectionCondition;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CollectionConditionReasonTest {
+
+ @Test
+ public void testCollectionConditionWithBecause() {
+ CollectionCondition condition = CollectionCondition.sizeGreaterThanOrEqual(1).because("size is lower than expected");
+ Assert.assertEquals("size >= 1 (because size is lower than expected)", condition.toString());
+ }
+
+ @Test
+ public void testCollectionConditionWithoutBecause() {
+ CollectionCondition condition = CollectionCondition.sizeGreaterThanOrEqual(1);
+ Assert.assertEquals("size >= 1", condition.toString());
+ }
+
+}