1818
1919import com .microsoft .playwright .impl .Driver ;
2020import com .microsoft .playwright .impl .DriverJar ;
21- import org .junit .jupiter .api .*;
21+ import org .junit .jupiter .api .BeforeEach ;
22+ import org .junit .jupiter .api .Test ;
2223import org .junit .jupiter .api .io .TempDir ;
2324
25+ import java .io .IOException ;
26+ import java .lang .reflect .Field ;
27+ import java .net .MalformedURLException ;
28+ import java .net .ServerSocket ;
2429import java .nio .file .Files ;
2530import java .nio .file .Path ;
2631import java .util .Collections ;
2732import java .util .HashMap ;
2833import java .util .Map ;
2934import java .util .concurrent .TimeUnit ;
3035
31- import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
32- import static org .junit .jupiter .api .Assertions .assertEquals ;
33- import static org .junit .jupiter .api .Assertions .assertThrows ;
34- import static org .junit .jupiter .api .Assertions .assertTrue ;
36+ import static org .junit .jupiter .api .Assertions .*;
3537
3638public class TestInstall {
39+ private static boolean isPortAvailable (int port ) {
40+ try (ServerSocket ignored = new ServerSocket (port )) {
41+ return true ;
42+ } catch (IOException ignored ) {
43+ return false ;
44+ }
45+ }
46+
47+ private static int unusedPort () {
48+ for (int i = 10000 ; i < 11000 ; i ++) {
49+ if (isPortAvailable (i )) {
50+ return i ;
51+ }
52+ }
53+ throw new RuntimeException ("Cannot find unused local port" );
54+ }
55+
3756 @ BeforeEach
3857 void clearSystemProperties () {
3958 // Clear system property to ensure that the driver is loaded from jar.
@@ -44,16 +63,29 @@ void clearSystemProperties() {
4463 }
4564
4665 @ Test
47- @ Tags ({@ Tag ("isolated" ), @ Tag ("driverThrowTest" )})
48- void shouldThrowWhenBrowserPathIsInvalid (@ TempDir Path tmpDir ) {
66+ void shouldThrowWhenBrowserPathIsInvalid (@ TempDir Path tmpDir ) throws MalformedURLException , ClassNotFoundException , NoSuchMethodException , NoSuchFieldException , IllegalAccessException {
4967 Map <String ,String > env = new HashMap <>();
50- env .put ("PLAYWRIGHT_DOWNLOAD_HOST" , "https://127.0.0.127" );
68+
69+ // On macOS we can only use 127.0.0.1, so pick unused port instead.
70+ // https://superuser.com/questions/458875/how-do-you-get-loopback-addresses-other-than-127-0-0-1-to-work-on-os-x
71+ env .put ("PLAYWRIGHT_DOWNLOAD_HOST" , "https://127.0.0.1:" + unusedPort ());
5172 // Make sure the browsers are not installed yet by pointing at an empty dir.
5273 env .put ("PLAYWRIGHT_BROWSERS_PATH" , tmpDir .toString ());
5374 env .put ("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD" , "false" );
5475
55- assertThrows (RuntimeException .class , () -> Driver .ensureDriverInstalled (env , true ));
56- assertThrows (RuntimeException .class , () -> Driver .ensureDriverInstalled (env , true ));
76+ // Reset instance field value to null for the test.
77+ Field field = Driver .class .getDeclaredField ("instance" );
78+ field .setAccessible (true );
79+ Object value = field .get (Driver .class );
80+ field .set (Driver .class , null );
81+
82+ for (int i = 0 ; i < 2 ; i ++){
83+ RuntimeException exception = assertThrows (RuntimeException .class , () -> Driver .ensureDriverInstalled (env , true ));
84+ String message = exception .getMessage ();
85+ assertTrue (message .contains ("Failed to create driver" ), message );
86+ }
87+
88+ field .set (Driver .class , value );
5789 }
5890
5991 @ Test
0 commit comments