Skip to content

Commit

Permalink
refactor tests for $$.exactTexts
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 27, 2023
1 parent a5e931f commit 0d0c57a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 143 deletions.
120 changes: 0 additions & 120 deletions src/test/java/com/codeborne/selenide/collections/ExactTextsTest.java
Original file line number Diff line number Diff line change
@@ -1,135 +1,15 @@
package com.codeborne.selenide.collections;

import com.codeborne.selenide.ex.ElementNotFound;
import com.codeborne.selenide.ex.TextsMismatch;
import com.codeborne.selenide.ex.TextsSizeMismatch;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebElement;

import java.util.List;

import static com.codeborne.selenide.Mocks.mockCollection;
import static com.codeborne.selenide.Mocks.mockElement;
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.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

final class ExactTextsTest {
@Test
void varArgsConstructor() {
ExactTexts exactTexts = new ExactTexts("One", "Two", "Three");
assertThat(exactTexts.expectedTexts)
.as("Expected texts list")
.isEqualTo(asList("One", "Two", "Three"));
}

@Test
void applyOnWrongSizeList() {
ExactTexts exactTexts = new ExactTexts("One", "Two", "Three");

assertThat(exactTexts.test(singletonList(mock())))
.isFalse();
}

@Test
void applyOnCorrectSizeAndCorrectElementsText() {
ExactTexts exactTexts = new ExactTexts("One", "Two");
WebElement webElement1 = mockElement("One");
WebElement webElement2 = mockElement("Two");

assertThat(exactTexts.test(asList(webElement1, webElement2)))
.isTrue();
}

@Test
void applyOnCorrectListSizeButWrongElementsText() {
ExactTexts exactTexts = new ExactTexts("One", "Two");
WebElement webElement1 = mockElement("One");
WebElement webElement2 = mockElement("One");

assertThat(exactTexts.test(asList(webElement1, webElement2)))
.isFalse();
}

@Test
void failWithNullElementsList() {
failOnEmptyOrNullElementsList(null);
}

private void failOnEmptyOrNullElementsList(List<WebElement> elements) {
ExactTexts exactTexts = new ExactTexts("One");
RuntimeException cause = new IllegalArgumentException("bad thing happened");

assertThatThrownBy(() -> exactTexts.fail(mockCollection("Collection description"), elements, cause, 10000))
.isInstanceOf(ElementNotFound.class)
.hasMessage(String.format("Element not found {Collection description}%nExpected: Exact texts [One]%n" +
"Timeout: 10 s.%n" +
"Caused by: java.lang.IllegalArgumentException: bad thing happened"));
}

@Test
void failWithEmptyElementsList() {
failOnEmptyOrNullElementsList(emptyList());
}

@Test
void failOnTextMismatch() {
ExactTexts exactTexts = new ExactTexts("One");
Exception cause = new Exception("Exception method");

WebElement mockedWebElement = mock();
when(mockedWebElement.getText()).thenReturn("Hello");

assertThatThrownBy(() ->
exactTexts.fail(mockCollection("Collection description"), singletonList(mockedWebElement), cause, 10000))
.isInstanceOf(TextsMismatch.class)
.hasMessage(String.format("Texts mismatch%n" +
"Actual: [Hello]%n" +
"Expected: [One]%n" +
"Collection: Collection description%n" +
"Timeout: 10 s.%n" +
"Caused by: java.lang.Exception: Exception method"));
}

@Test
void failOnTextSizeMismatch() {
ExactTexts exactTexts = new ExactTexts("One", "Two");
Exception cause = new Exception("Exception method");
WebElement webElement = mockElement("One");

assertThatThrownBy(() -> exactTexts.fail(mockCollection("Collection description"),
singletonList(webElement), cause, 10000)
)
.isInstanceOf(TextsSizeMismatch.class)
.hasMessageContaining("Actual: [One], List size: 1")
.hasMessageContaining("Expected: [One, Two], List size: 2")
.hasMessageContaining("Collection: Collection description")
.hasMessageContaining("Timeout: 10 s.")
.hasMessageEndingWith("Caused by: java.lang.Exception: Exception method");
}

@Test
void testToString() {
ExactTexts exactTexts = new ExactTexts("One", "Two", "Three");
assertThat(exactTexts)
.hasToString("Exact texts [One, Two, Three]");
}

@Test
void emptyArrayIsNotAllowed() {
assertThatThrownBy(ExactTexts::new)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("No expected texts given");
}

@Test
void emptyListIsNotAllowed() {
assertThatThrownBy(() -> new ExactTexts(emptyList()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("No expected texts given");
}
}
23 changes: 0 additions & 23 deletions src/test/java/integration/collections/CollectionMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,12 @@ void ignoresWhitespacesInTexts() {
});
}

@Test
void canCheckThatElementsHaveExactlyCorrectTexts() {
withLongTimeout(() -> {
assertThatThrownBy(() -> $$("#dynamic-content-container span").shouldHave(exactTexts("content", "content2")))
.isInstanceOf(TextsMismatch.class);
});
}

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

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

@Test
void textsCheckThrowsTextsSizeMismatch() {
withLongTimeout(() -> {
Expand Down Expand Up @@ -404,10 +389,6 @@ void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException_first() {
ElementsCollection elementsCollection = $$("not-existing-locator").first().$$("#multirowTable");
String description = "Check throwing ElementNotFound for %s";

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

assertThatThrownBy(() -> elementsCollection.shouldHave(texts("any text")))
.as(description, "texts").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(NoSuchElementException.class);
Expand All @@ -418,10 +399,6 @@ void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException() {
ElementsCollection elementsCollection = $$("not-existing-locator").get(1).$$("#multirowTable");
String description = "Check throwing ElementNotFound for %s";

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

assertThatThrownBy(() -> elementsCollection.shouldHave(texts("any text")))
.as(description, "texts").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(IndexOutOfBoundsException.class);
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/integration/collections/ExactTextsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
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.exactTexts;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

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

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

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

@Test
void checksElementsCount() {
assertThatThrownBy(() -> $$(".element").shouldHave(exactTexts("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(exactTexts("content1", "content2")))
.isInstanceOf(ElementNotFound.class)
.hasMessageStartingWith("Element not found {.non-existing-elements}");
}

@Test
void throwsElementNotFound_causedBy_NoSuchElementException() {
ElementsCollection collection = $$("not-existing-locator").first().$$("h1");

assertThatThrownBy(() -> collection.shouldHave(exactTexts("One", "Two")))
.isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(NoSuchElementException.class);
}

@Test
void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException() {
ElementsCollection collection = $$("not-existing-locator").get(1).$$("h1");

assertThatThrownBy(() -> collection.shouldHave(exactTexts("One", "Two")))
.isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(IndexOutOfBoundsException.class);
}

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

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

0 comments on commit 0d0c57a

Please sign in to comment.