1717
1818package org .openqa .selenium ;
1919
20- import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
20+ import static org .assertj .core .api .Assertions .* ;
2121import static org .openqa .selenium .testing .drivers .Browser .FIREFOX ;
2222import static org .openqa .selenium .testing .drivers .Browser .SAFARI ;
2323
24+ import org .junit .jupiter .api .BeforeEach ;
2425import org .junit .jupiter .api .Test ;
26+ import org .openqa .selenium .remote .RemoteWebDriver ;
27+ import org .openqa .selenium .remote .SessionId ;
2528import org .openqa .selenium .testing .JupiterTestBase ;
2629import org .openqa .selenium .testing .NoDriverAfterTest ;
2730import org .openqa .selenium .testing .NotYetImplemented ;
2831
2932class 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