From 1255e95dac75972f69462c5226e7424643b2560f Mon Sep 17 00:00:00 2001 From: Mikhail Sidelnikov Date: Mon, 28 May 2018 16:07:43 +0300 Subject: [PATCH 1/2] Added because(message) method to elementsCollections --- .../selenide/CollectionCondition.java | 45 ++++++++++++++++--- .../CollectionConditionReasonTest.java | 21 +++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/codeborne/selenide/collections/CollectionConditionReasonTest.java diff --git a/src/main/java/com/codeborne/selenide/CollectionCondition.java b/src/main/java/com/codeborne/selenide/CollectionCondition.java index e8a87516bc..fb7489fd7f 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,43 @@ 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); } + + 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()); + } + +} From 59f49897e04aa9720342446507b3028900baa670 Mon Sep 17 00:00:00 2001 From: Mikhail Sidelnikov Date: Mon, 28 May 2018 16:17:17 +0300 Subject: [PATCH 2/2] Added javadoc for because method in CollectionCondition --- src/main/java/com/codeborne/selenide/CollectionCondition.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/codeborne/selenide/CollectionCondition.java b/src/main/java/com/codeborne/selenide/CollectionCondition.java index fb7489fd7f..26fe67c1fe 100644 --- a/src/main/java/com/codeborne/selenide/CollectionCondition.java +++ b/src/main/java/com/codeborne/selenide/CollectionCondition.java @@ -101,6 +101,9 @@ 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); }