/*
 * (C) Copyright 2017 Boni Garcia (http://bonigarcia.github.io/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package io.github.bonigarcia.test.advance;

// tag::snippet-in-doc[]
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.openqa.selenium.remote.DesiredCapabilities.firefox;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.grid.selenium.GridLauncherV3;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

import io.github.bonigarcia.DriverCapabilities;
import io.github.bonigarcia.DriverUrl;
import io.github.bonigarcia.SeleniumExtension;
import io.github.bonigarcia.wdm.ChromeDriverManager;
import io.github.bonigarcia.wdm.FirefoxDriverManager;

@ExtendWith(SeleniumExtension.class)
public class RemoteWebDriverJupiterTest {

    @DriverUrl
    String url = "http://localhost:4445/wd/hub";

    @DriverCapabilities
    Capabilities capabilities = firefox();

    @BeforeAll
    static void setup() throws Exception {
        // Start hub
        GridLauncherV3.main(new String[] { "-role", "hub", "-port", "4445" });

        // Register Chrome in hub
        ChromeDriverManager.getInstance().setup();
        GridLauncherV3.main(new String[] { "-role", "node", "-hub",
                "http://localhost:4445/grid/register", "-browser",
                "browserName=chrome", "-port", "5555" });

        // Register Firefox in hub
        FirefoxDriverManager.getInstance().setup();
        GridLauncherV3.main(new String[] { "-role", "node", "-hub",
                "http://localhost:4445/grid/register", "-browser",
                "browserName=firefox", "-port", "5556" });
    }

    @Test
    void testWithRemoteChrome(
            @DriverUrl("http://localhost:4445/wd/hub") @DriverCapabilities("browserName=chrome") RemoteWebDriver driver) {
        exercise(driver);
    }

    @Test
    void testWithRemoteFirefox(RemoteWebDriver driver) {
        exercise(driver);
    }

    void exercise(WebDriver driver) {
        driver.get("https://bonigarcia.github.io/selenium-jupiter/");
        assertThat(driver.getTitle(),
                containsString("JUnit 5 extension for Selenium"));
    }

}
// end::snippet-in-doc[]

@@ -668,16 +668,109 @@
 vance;%0A%0A
+import static java.lang.String.valueOf;%0Aimport static java.lang.invoke.MethodHandles.lookup;%0A
 // tag::
@@ -964,16 +964,124 @@
 firefox;
+%0Aimport static org.slf4j.LoggerFactory.getLogger;%0A%0Aimport java.io.IOException;%0Aimport java.net.ServerSocket;
 %0A%0Aimport
@@ -1377,16 +1377,41 @@
 bDriver;
+%0Aimport org.slf4j.Logger;
 %0A%0Aimport
@@ -1726,16 +1726,755 @@
 Test %7B%0A%0A
+    static final Logger log = getLogger(lookup().lookupClass());%0A%0A    static int hubPort = 4446;%0A    static int chromeNodePort = 5555;%0A    static int firefoxNodePort = 5556;%0A%0A    static %7B%0A        try %7B%0A            ServerSocket serverSocket = new ServerSocket(0);%0A            hubPort = serverSocket.getLocalPort();%0A            serverSocket.close();%0A%0A            serverSocket = new ServerSocket(0);%0A            chromeNodePort = serverSocket.getLocalPort();%0A            serverSocket.close();%0A%0A            serverSocket = new ServerSocket(0);%0A            firefoxNodePort = serverSocket.getLocalPort();%0A            serverSocket.close();%0A        %7D catch (IOException e) %7B%0A            log.error(%22Exception finding free port%22, e);%0A        %7D%0A    %7D%0A%0A
     @Dri
@@ -2507,36 +2507,47 @@
 ttp://localhost:
-4445
+%22 + hubPort + %22
 /wd/hub%22;%0A%0A    @
@@ -2708,32 +2708,49 @@
 LauncherV3.main(
+%0A                
 new String%5B%5D %7B %22
@@ -2777,14 +2777,24 @@
 t%22, 
-%224445%22
+valueOf(hubPort)
  %7D);
@@ -2974,36 +2974,47 @@
 ttp://localhost:
-4445
+%22 + hubPort + %22
 /grid/register%22,
@@ -3077,14 +3077,31 @@
 t%22, 
-%225555%22
+valueOf(chromeNodePort)
  %7D);
@@ -3291,20 +3291,31 @@
 calhost:
-4445
+%22 + hubPort + %22
 /grid/re
@@ -3387,14 +3387,32 @@
 t%22, 
-%225556%22
+valueOf(firefoxNodePort)
  %7D);
@@ -3475,51 +3475,8 @@
     
- @DriverUrl(%22http://localhost:4445/wd/hub%22)
  @Dr

Use available ports in RemoteWebDriver test