Skip to content

Commit

Permalink
speed up the slowest collections tests
Browse files Browse the repository at this point in the history
In most tests, we don't really need to wait 4 seconds.
There is only a few tests where we do want to test the "waiting".
  • Loading branch information
asolntsev committed May 28, 2023
1 parent 51b9e07 commit ec4bbcd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 34 deletions.
38 changes: 22 additions & 16 deletions src/test/java/integration/collections/CollectionMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.ex.AttributesMismatch;
import com.codeborne.selenide.ex.ElementNotFound;
import com.codeborne.selenide.ex.ListSizeMismatch;
import com.codeborne.selenide.ex.TextsMismatch;
Expand Down Expand Up @@ -134,11 +133,10 @@ void canCheckThatElementsHaveCorrectTexts() {

@Test
void ignoresWhitespacesInTexts() {
withLongTimeout(() -> {
$$("#dynamic-content-container span").shouldHave(
texts(" dynamic \ncontent ", "dynamic \t\t\tcontent2\t\t\r\n"),
exactTexts("dynamic \t\n content\n\r", " dynamic content2 "));
});
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
Expand All @@ -149,19 +147,26 @@ void textsCheckThrowsElementNotFound() {

@Test
void textsCheckThrowsTextsSizeMismatch() {
withLongTimeout(() -> {
assertThatThrownBy(() -> $$("#dynamic-content-container span")
.shouldHave(texts("static-content1", "static-content2", "dynamic-content1")))
.isInstanceOf(TextsSizeMismatch.class);
});
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 textCheckThrowsTextsMismatch() {
withLongTimeout(() -> {
assertThatThrownBy(() -> $$("#dynamic-content-container span").shouldHave(texts("static-content1", "static-content2")))
.isInstanceOf(TextsMismatch.class);
});
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
Expand Down Expand Up @@ -220,6 +225,7 @@ void errorMessageShouldShow_whichElementInChainWasNotFound() {
.shouldHave(texts("foo bar")))
.isInstanceOf(ElementNotFound.class)
.hasMessageContaining("Element not found {#multirowTable.findBy(text \"INVALID-TEXT\")/valid-selector}")
.hasMessageContaining("Timeout: 1 ms.")
.hasCauseInstanceOf(NoSuchElementException.class)
.getCause()
.hasMessageStartingWith("Cannot locate an element #multirowTable.findBy(text \"INVALID-TEXT\")");
Expand Down
40 changes: 26 additions & 14 deletions src/test/java/integration/collections/CollectionWaitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

final class CollectionWaitTest extends ITest {
private final long startedAt = System.currentTimeMillis();
private long startedAt;

@BeforeEach
void openTestPage() {
openFile("collection_with_delays.html");
setTimeout(1000);
startedAt = System.currentTimeMillis();
}

@Test
Expand All @@ -41,14 +42,17 @@ void waitsUntilNthElementAppears() {

@Test
void failsIfWrongSize() {
setTimeout(3);
assertThatThrownBy(() -> $$("#collection li").shouldHave(size(-1)))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("expected: = -1, actual: 20, collection: #collection li");
assertTestTookMoreThan(1, SECONDS);
.hasMessageContaining("expected: = -1, actual: ")
.hasMessageContaining("collection: #collection li")
.hasMessageContaining("Timeout: 3 ms.");
assertTestTookLessThan(500, MILLISECONDS);
}

@Test
void canDetermineSize() {
void waitsUntilCollectionsGetsLoaded() {
$$("#collection li").shouldHave(size(20));
}

Expand All @@ -64,11 +68,12 @@ void waitsUntilLastNElementsGetLoaded() {

@Test
void firstNElements_TextsMismatchErrorMessage() {
assertThatThrownBy(() -> $$("#collection li").first(2).shouldHave(texts("Element", "#wrong")))
assertThatThrownBy(() -> $$("#collection li").first(2).shouldHave(texts("Element", "#wrong"), Duration.ofSeconds(1)))
.isInstanceOf(TextsMismatch.class)
.hasMessageContaining(String.format("Actual: [Element #0, Element #1]%n" +
.hasMessageStartingWith(String.format("Texts mismatch%nActual: [Element #0, Element #1]%n" +
"Expected: [Element, #wrong]%n" +
"Collection: #collection li:first(2)"));
"Collection: #collection li:first(2)"))
.hasMessageContaining("Timeout: 1 s.");
assertTestTookMoreThan(1, SECONDS);
}

Expand All @@ -84,11 +89,12 @@ void firstNElements_TextsSizeMismatchErrorMessage() {

@Test
void lastNElements_errorMessage() {
assertThatThrownBy(() -> $$("#collection li").last(2).shouldHave(texts("Element", "#wrong")))
assertThatThrownBy(() -> $$("#collection li").last(2).shouldHave(texts("Element", "#wrong"), Duration.ofSeconds(1)))
.isInstanceOf(TextsMismatch.class)
.hasMessageContaining(String.format("Actual: [Element #18, Element #19]%n" +
"Expected: [Element, #wrong]%n" +
"Collection: #collection li:last(2)"));
"Collection: #collection li:last(2)"))
.hasMessageContaining("Timeout: 1 s.");
assertTestTookMoreThan(1, SECONDS);
}

Expand All @@ -103,27 +109,33 @@ void customTimeoutForCollections() {
void waitsForCustomTimeoutForCollections() {
setTimeout(1);
assertThatThrownBy(() ->
$$("#collection li").last(2).shouldHave(texts("Element #88888", "Element #99999"), Duration.ofMillis(2000))
$$("#collection li").last(2).shouldHave(texts("Element #88888", "Element #99999"), Duration.ofMillis(999))
)
.isInstanceOf(TextsMismatch.class)
.hasMessageContaining("Actual: [Element #18, Element #19]")
.hasMessageContaining("Expected: [Element #88888, Element #99999]");
assertTestTookMoreThan(2000, MILLISECONDS);
.hasMessageContaining("Expected: [Element #88888, Element #99999]")
.hasMessageContaining("Timeout: 999 ms.");
assertTestTookMoreThan(999, MILLISECONDS);
}

@Test
void waitsForElementInsideCollection() {
setTimeout(2000);
setTimeout(100);
assertThatThrownBy(() ->
$$("h1").findBy(cssClass("active")).findAll("h2").shouldHave(texts("nothing else matters"))
)
.isInstanceOf(ElementNotFound.class)
.hasMessageContaining("Element not found {h1.findBy(css class \"active\")");
assertTestTookMoreThan(2000, MILLISECONDS);
assertTestTookMoreThan(100, MILLISECONDS);
}

private void assertTestTookMoreThan(int value, TimeUnit unit) {
long endedAt = System.currentTimeMillis();
assertThat(endedAt - startedAt).isGreaterThanOrEqualTo(unit.toMillis(value));
}

private void assertTestTookLessThan(int value, TimeUnit unit) {
long endedAt = System.currentTimeMillis();
assertThat(endedAt - startedAt).isLessThanOrEqualTo(unit.toMillis(value));
}
}
7 changes: 7 additions & 0 deletions src/test/java/integration/collections/ExactTextsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ void checksTexts() {
$$(".element").shouldHave(exactTexts(asList("One", "Two", "Three")));
}

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

@Test
void doesNotAcceptSubstrings() {
assertThatThrownBy(() -> $$(".element").shouldHave(exactTexts("On", "Tw", "Thre")))
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/page_with_selects_without_jquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ <h2>Radio buttons</h2>
<a href="long_ajax_request.html" id="ajax-button">Want to see ajax in action?</a>

<script type="text/javascript">
var commandDuration = 100;
const delay = 50;

setTimeout(function() {document.getElementById('container').innerHTML = '<div id="dynamic-content-container"></div>';}, commandDuration*3);
setTimeout(function() {document.getElementById('dynamic-content-container').innerHTML = '<span id="dynamic-content">dynamic content</span>';}, commandDuration*4);
setTimeout(function() {document.getElementById('dynamic-content-container').innerHTML += '<span id="dynamic-content2">dynamic content2</span>';}, commandDuration*5);
setTimeout(function() {document.getElementById('container').innerHTML = '<div id="dynamic-content-container"></div>';}, delay);
setTimeout(function() {document.getElementById('dynamic-content-container').innerHTML = '<span id="dynamic-content">dynamic content</span>';}, delay*2);
setTimeout(function() {document.getElementById('dynamic-content-container').innerHTML += '<span id="dynamic-content2">dynamic content2</span>';}, delay*3);

</script>

Expand Down

0 comments on commit ec4bbcd

Please sign in to comment.