2121import org .junit .jupiter .api .*;
2222import org .junit .jupiter .api .io .TempDir ;
2323
24+ import java .io .IOException ;
25+ import java .net .ServerSocket ;
2426import java .nio .file .Files ;
2527import java .nio .file .Path ;
2628import java .util .Collections ;
3436import static org .junit .jupiter .api .Assertions .assertTrue ;
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.
@@ -47,7 +66,10 @@ void clearSystemProperties() {
4766 @ Tags ({@ Tag ("isolated" ), @ Tag ("driverThrowTest" )})
4867 void shouldThrowWhenBrowserPathIsInvalid (@ TempDir Path tmpDir ) {
4968 Map <String ,String > env = new HashMap <>();
50- env .put ("PLAYWRIGHT_DOWNLOAD_HOST" , "https://127.0.0.127" );
69+
70+ // On macOS we can only use 127.0.0.1, so pick unused port instead.
71+ // https://superuser.com/questions/458875/how-do-you-get-loopback-addresses-other-than-127-0-0-1-to-work-on-os-x
72+ env .put ("PLAYWRIGHT_DOWNLOAD_HOST" , "https://127.0.0.1:" + unusedPort ());
5173 // Make sure the browsers are not installed yet by pointing at an empty dir.
5274 env .put ("PLAYWRIGHT_BROWSERS_PATH" , tmpDir .toString ());
5375 env .put ("PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD" , "false" );
0 commit comments