Skip to content

Commit

Permalink
refactor tests for $$.texts
Browse files Browse the repository at this point in the history
replaced useless unit-tests by analogous integration test
  • Loading branch information
asolntsev committed May 28, 2023
1 parent f91e4fc commit 62722b6
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 102 deletions.
36 changes: 0 additions & 36 deletions src/test/java/com/codeborne/selenide/collections/TextsTest.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,14 @@
package com.codeborne.selenide.collections;

import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebElement;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

final class TextsTest {
@Test
void applyWithEmptyList() {
assertThat(new Texts("One", "Two", "Three").test(emptyList()))
.isFalse();
}

@Test
void applyWithWrongSizeList() {
Texts texts = new Texts(asList("One", "Two", "Three"));
assertThat(texts.test(singletonList(element(""))))
.isFalse();
}

@Test
void applyWithMatchOnPartialText() {
Texts texts = new Texts(asList("One", "Two"));
assertThat(texts.test(asList(element("OneThing"), element("Two")))).isEqualTo(true);
}

@Test
void applyWithNoMatchOnPartialText() {
Texts texts = new Texts(asList("One", "Two"));
assertThat(texts.test(asList(element("Three"), element("Selenide")))).isEqualTo(false);
}

@Test
void testToString() {
assertThat(new Texts(asList("One", "Two")))
.hasToString("texts [One, Two]");
}

private WebElement element(String text) {
WebElement webElement = mock();
when(webElement.getText()).thenReturn(text);
return webElement;
}
}
66 changes: 0 additions & 66 deletions src/test/java/integration/collections/CollectionMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.ex.ElementNotFound;
import com.codeborne.selenide.ex.ListSizeMismatch;
import com.codeborne.selenide.ex.TextsMismatch;
import com.codeborne.selenide.ex.TextsSizeMismatch;
import integration.ITest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -130,50 +128,6 @@ void canCheckThatElementsHaveCorrectTexts() {
});
}

@Test
void ignoresWhitespacesInTexts() {
openFile("page_with_list_of_elements.html");
$$("ol.spaces li").shouldHave(
texts(" The \nfirst ", "The \t\t\tsecond\t\t\r\n", "The third")
);
}

@Test
void textsCheckThrowsElementNotFound() {
assertThatThrownBy(() -> $$(".non-existing-elements").shouldHave(texts("content1", "content2")))
.isInstanceOf(ElementNotFound.class);
}

@Test
void textsCheckThrowsTextsSizeMismatch() {
setTimeout(300);
assertThatThrownBy(() -> $$("#dynamic-content-container span")
.shouldHave(texts("static-content1", "static-content2", "dynamic-content1")))
.isInstanceOf(TextsSizeMismatch.class)
.hasMessageStartingWith("Texts size mismatch")
.hasMessageContaining("Actual: [dynamic content, dynamic content2], List size: 2")
.hasMessageContaining("Expected: [static-content1, static-content2, dynamic-content1], List size: 3")
.hasMessageContaining("Collection: #dynamic-content-container span");
}

@Test
void textsCheckThrowsTextsMismatch() {
setTimeout(300);
assertThatThrownBy(() -> $$("#dynamic-content-container span").shouldHave(texts("static-content1", "static-content2")))
.isInstanceOf(TextsMismatch.class)
.hasMessageStartingWith("Texts mismatch")
.hasMessageContaining("Actual: [dynamic content, dynamic content2]")
.hasMessageContaining("Expected: [static-content1, static-content2]")
.hasMessageContaining("Collection: #dynamic-content-container span")
.hasMessageContaining("Timeout: 300 ms.");
}

@Test
void failsFast_ifNoExpectedTextsAreGiven() {
assertThatThrownBy(() -> $$("#dynamic-content-container span").shouldHave(texts()))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
void canCheckCollectionAttributes() {
$$("#domain option").shouldHave(attributes("data-mailServerId",
Expand Down Expand Up @@ -362,26 +316,6 @@ void canChainFilterAndFirst() {
$$("div").filterBy(visible).get(2).click();
}

@Test
void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException_first() {
ElementsCollection elementsCollection = $$("not-existing-locator").first().$$("#multirowTable");
String description = "Check throwing ElementNotFound for %s";

assertThatThrownBy(() -> elementsCollection.shouldHave(texts("any text")))
.as(description, "texts").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(NoSuchElementException.class);
}

@Test
void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException() {
ElementsCollection elementsCollection = $$("not-existing-locator").get(1).$$("#multirowTable");
String description = "Check throwing ElementNotFound for %s";

assertThatThrownBy(() -> elementsCollection.shouldHave(texts("any text")))
.as(description, "texts").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(IndexOutOfBoundsException.class);
}

@Test
void errorWhenFindInLastElementOfEmptyCollection() {
assertThatThrownBy(() -> $$("#not_exist").last().$("#multirowTable").should(exist))
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/integration/collections/TextsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package integration.collections;

import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.ex.ElementNotFound;
import com.codeborne.selenide.ex.TextsMismatch;
import com.codeborne.selenide.ex.TextsSizeMismatch;
import integration.ITest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.NoSuchElementException;

import static com.codeborne.selenide.CollectionCondition.texts;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TextsTest extends ITest {
@BeforeEach
void openPage() {
openFile("page_with_list_of_elements.html");
}

@Test
void checksTexts() {
$$(".element").shouldHave(texts("One", "Two", "Three"));
$$(".element").shouldHave(texts(asList("One", "Two", "Three")));
}

@Test
void checksOrderOfTexts() {
assertThatThrownBy(() -> $$(".element").shouldHave(texts("Two", "One", "Three")))
.isInstanceOf(TextsMismatch.class)
.hasMessageContaining("Actual: [One, Two, Three]")
.hasMessageContaining("Expected: [Two, One, Three]");
}

@Test
void ignoresWhitespacesInTexts() {
$$("ol.spaces li").shouldHave(
texts("The \t\n first\n\r", " The second ", "The third")
);
}

@Test
void acceptsSubstrings() {
$$(".element").shouldHave(texts("On", "Two", "Three"));
$$(".element").shouldHave(texts("One", "wo", "Three"));
$$(".element").shouldHave(texts("One", "Two", "hre"));
}

@Test
void errorMessage() {
assertThatThrownBy(() -> $$(".element").shouldHave(texts("Three", "Two", "One")))
.isInstanceOf(TextsMismatch.class)
.hasMessageStartingWith("Texts mismatch")
.hasMessageContaining("Actual: [One, Two, Three]")
.hasMessageContaining("Expected: [Three, Two, One]")
.hasMessageContaining("Collection: .element");
}

@Test
void ignoresCase() {
$$(".element").shouldHave(texts("one", "two", "three"));
$$(".element").shouldHave(texts("ONE", "TWO", "THREE"));
$$(".element").shouldHave(texts("onE", "twO", "threE"));
}

@Test
void checksElementsCount() {
assertThatThrownBy(() -> $$(".element").shouldHave(texts("One", "Two", "Three", "Four")))
.isInstanceOf(TextsSizeMismatch.class)
.hasMessageStartingWith("Texts size mismatch")
.hasMessageContaining("Actual: [One, Two, Three], List size: 3")
.hasMessageContaining("Expected: [One, Two, Three, Four], List size: 4")
.hasMessageContaining("Collection: .element");
}

@Test
void throwsElementNotFound() {
assertThatThrownBy(() -> $$(".non-existing-elements").shouldHave(texts("content1", "content2")))
.isInstanceOf(ElementNotFound.class)
.hasMessageStartingWith("Element not found {.non-existing-elements}");
}

@Test
void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException_first() {
ElementsCollection elementsCollection = $$("not-existing-locator").first().$$("#multirowTable");
String description = "Check throwing ElementNotFound for %s";

assertThatThrownBy(() -> elementsCollection.shouldHave(texts("any text")))
.as(description, "texts").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(NoSuchElementException.class);
}

@Test
void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException() {
ElementsCollection elementsCollection = $$("not-existing-locator").get(1).$$("#multirowTable");
String description = "Check throwing ElementNotFound for %s";

assertThatThrownBy(() -> elementsCollection.shouldHave(texts("any text")))
.as(description, "texts").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(IndexOutOfBoundsException.class);
}

@Test
void emptyArrayIsNotAllowed() {
assertThatThrownBy(() -> $$(".element").shouldHave(texts()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("No expected texts given");
}

@Test
void emptyListIsNotAllowed() {
assertThatThrownBy(() -> $$(".element").shouldHave(texts(emptyList())))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("No expected texts given");
}
}

0 comments on commit 62722b6

Please sign in to comment.