Skip to content

Commit cfc922c

Browse files
committed
remove useless sleeps from SessionHandlingTest
NB! This change decrease execution time of `SessionHandlingTest` from ~28s to 7s.
1 parent addf0a0 commit cfc922c

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

java/test/org/openqa/selenium/SessionHandlingTest.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@
1717

1818
package org.openqa.selenium;
1919

20-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20+
import static org.assertj.core.api.Assertions.*;
2121
import static org.openqa.selenium.testing.drivers.Browser.FIREFOX;
2222
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
2323

24+
import org.junit.jupiter.api.BeforeEach;
2425
import org.junit.jupiter.api.Test;
26+
import org.openqa.selenium.remote.RemoteWebDriver;
27+
import org.openqa.selenium.remote.SessionId;
2528
import org.openqa.selenium.testing.JupiterTestBase;
2629
import org.openqa.selenium.testing.NoDriverAfterTest;
2730
import org.openqa.selenium.testing.NotYetImplemented;
2831

2932
class SessionHandlingTest extends JupiterTestBase {
33+
@BeforeEach
34+
void setUp() {
35+
assertThat(getSessionId()).isNotNull();
36+
}
3037

3138
@NoDriverAfterTest
3239
@Test
3340
void callingQuitMoreThanOnceOnASessionIsANoOp() {
3441
driver.quit();
35-
sleepTight(3000);
42+
waitUntilBrowserFullyClosed();
3643
driver.quit();
3744
}
3845

@@ -42,37 +49,36 @@ void callingQuitMoreThanOnceOnASessionIsANoOp() {
4249
@NotYetImplemented(SAFARI)
4350
public void callingQuitAfterClosingTheLastWindowIsANoOp() {
4451
driver.close();
45-
sleepTight(3000);
4652
driver.quit();
4753
}
4854

4955
@NoDriverAfterTest
5056
@Test
5157
void callingAnyOperationAfterClosingTheLastWindowShouldThrowAnException() {
5258
driver.close();
53-
sleepTight(3000);
5459
assertThatExceptionOfType(NoSuchSessionException.class).isThrownBy(driver::getCurrentUrl);
5560
}
5661

5762
@NoDriverAfterTest
5863
@Test
5964
void callingAnyOperationAfterQuitShouldThrowAnException() {
6065
driver.quit();
61-
sleepTight(3000);
66+
waitUntilBrowserFullyClosed();
6267
assertThatExceptionOfType(NoSuchSessionException.class).isThrownBy(driver::getCurrentUrl);
6368
}
6469

6570
@Test
66-
void shouldContinueAfterSleep() {
67-
sleepTight(10000);
68-
driver.getWindowHandle(); // should not throw
71+
void shouldContinueAfterSleep() throws InterruptedException {
72+
assertThatCode(() -> driver.getWindowHandle()).doesNotThrowAnyException();
73+
Thread.sleep(50);
74+
assertThatCode(() -> driver.getWindowHandle()).doesNotThrowAnyException();
75+
}
76+
77+
private void waitUntilBrowserFullyClosed() {
78+
wait.until($ -> getSessionId() == null);
6979
}
7080

71-
private void sleepTight(long duration) {
72-
try {
73-
Thread.sleep(duration);
74-
} catch (InterruptedException e) {
75-
e.printStackTrace();
76-
}
81+
private SessionId getSessionId() {
82+
return ((RemoteWebDriver) driver).getSessionId();
7783
}
7884
}

0 commit comments

Comments
 (0)