Skip to content

Commit

Permalink
refactor tests for $$.sizeNotEqual
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 26, 2023
1 parent 5a60784 commit 343f472
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.codeborne.selenide.collections.SizeGreaterThanOrEqual;
import com.codeborne.selenide.collections.SizeLessThan;
import com.codeborne.selenide.collections.SizeLessThanOrEqual;
import com.codeborne.selenide.collections.SizeNotEqual;
import com.codeborne.selenide.collections.Texts;
import com.codeborne.selenide.collections.TextsInAnyOrder;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -46,12 +45,6 @@ void sizeLessThanOrEqual() {
.isInstanceOf(SizeLessThanOrEqual.class);
}

@Test
void sizeNotEqual() {
assertThat(CollectionCondition.sizeNotEqual(10))
.isInstanceOf(SizeNotEqual.class);
}

@Test
void textsWithObjectsList() {
CollectionCondition collectionCondition = CollectionCondition.texts("One", "Two", "Three");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,10 @@
package com.codeborne.selenide.collections;

import com.codeborne.selenide.ex.ListSizeMismatch;
import com.codeborne.selenide.impl.CollectionSource;
import org.junit.jupiter.api.Test;

import static com.codeborne.selenide.Mocks.mockCollection;
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;

final class SizeNotEqualTest {
@Test
void applyWithEmptyList() {
assertThat(new SizeNotEqual(10).test(emptyList()))
.isTrue();
}

@Test
void applyWithWrongSizeList() {
assertThat(new SizeNotEqual(10).test(singletonList(mock())))
.isTrue();
}

@Test
void applyWithCorrectSizeNotEqual() {
assertThat(new SizeNotEqual(1).test(singletonList(mock())))
.isFalse();
}

@Test
void failMethod() {
CollectionSource collection = mockCollection("Collection description");

assertThatThrownBy(() -> new SizeNotEqual(10).fail(collection,
emptyList(),
new Exception("Exception message"),
10000))
.isInstanceOf(ListSizeMismatch.class)
.hasMessageStartingWith(
String.format("List size mismatch: expected: <> 10, actual: 0, collection: Collection description%nElements: []"));
}

@Test
void testToString() {
assertThat(new SizeNotEqual(10))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package integration;
package integration.collections;

import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;
Expand All @@ -8,6 +8,7 @@
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;
import org.openqa.selenium.By;
Expand Down Expand Up @@ -410,10 +411,6 @@ void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException_first() {
.as(description, "sizeGreaterThanOrEqual").isInstanceOf(ListSizeMismatch.class)
.hasCauseExactlyInstanceOf(NoSuchElementException.class);

assertThatThrownBy(() -> elementsCollection.shouldHave(sizeNotEqual(0)))
.as(description, "sizeNotEqual").isInstanceOf(ListSizeMismatch.class)
.hasCauseExactlyInstanceOf(NoSuchElementException.class);

assertThatThrownBy(() -> elementsCollection.shouldHave(sizeLessThan(0)))
.as(description, "sizeLessThan").isInstanceOf(ListSizeMismatch.class)
.hasCauseExactlyInstanceOf(NoSuchElementException.class);
Expand Down Expand Up @@ -452,10 +449,6 @@ void shouldThrow_ElementNotFound_causedBy_IndexOutOfBoundsException() {
.as(description, "sizeGreaterThanOrEqual").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(IndexOutOfBoundsException.class);

assertThatThrownBy(() -> elementsCollection.shouldHave(sizeNotEqual(0)))
.as(description, "sizeNotEqual").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(IndexOutOfBoundsException.class);

assertThatThrownBy(() -> elementsCollection.shouldHave(sizeLessThan(0)))
.as(description, "sizeLessThan").isInstanceOf(ElementNotFound.class)
.hasCauseExactlyInstanceOf(IndexOutOfBoundsException.class);
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/integration/collections/SizeNotEqualTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package integration.collections;

import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.ex.ElementNotFound;
import com.codeborne.selenide.ex.ListSizeMismatch;
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.size;
import static com.codeborne.selenide.CollectionCondition.sizeNotEqual;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class SizeNotEqualTest extends ITest {

@BeforeEach
void openPage() {
openFile("page_with_selects_without_jquery.html");
}

@Test
void size_notEqual() {
$$("#radioButtons input").shouldHave(size(4));
$$("#radioButtons input").shouldHave(sizeNotEqual(3));
$$("#radioButtons input").shouldHave(sizeNotEqual(5));
}

@Test
void size_notEqual_failure() {
assertThatThrownBy(() -> $$("#radioButtons input").shouldHave(sizeNotEqual(4)))
.isInstanceOf(ListSizeMismatch.class)
.hasMessageStartingWith("List size mismatch")
.hasMessageContaining("expected: <> 4, actual: 4")
.hasMessageContaining("collection: #radioButtons input")
.hasMessageContaining("""
Elements: [
\t<input name="me" type="radio" value="master"></input>,
\t<input name="me" type="radio" value="margarita"></input>,
\t<input name="me" type="radio" value="cat"></input>,
\t<input name="me" type="radio" value="woland"></input>
]""");
}

@Test
void emptyCollection() {
$$("not-existing-locator").shouldHave(sizeNotEqual(3));
}

@Test
void emptyFilteredCollection() {
ElementsCollection collection = $$("not-existing-locator").first().$$("#multirowTable");

assertThatThrownBy(() -> collection.shouldHave(sizeNotEqual(0)))
.isInstanceOf(ListSizeMismatch.class)
.hasMessageStartingWith("List size mismatch")
.hasMessageContaining("expected: <> 0")
.hasMessageContaining("actual: 0")
.hasMessageContaining("collection: not-existing-locator[0]/#multirowTable")
.hasCauseExactlyInstanceOf(NoSuchElementException.class);
}

@Test
void emptyCollectionByIndex() {
ElementsCollection collection = $$("not-existing-locator").get(1).$$("#multirowTable");

assertThatThrownBy(() -> collection.shouldHave(sizeNotEqual(0)))
.isInstanceOf(ElementNotFound.class)
.hasMessageStartingWith("Element not found {not-existing-locator[1]/#multirowTable}")
.hasCauseExactlyInstanceOf(IndexOutOfBoundsException.class);
}
}

0 comments on commit 343f472

Please sign in to comment.