// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you 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 org.openqa.grid.e2e.misc;

import junit.framework.Assert;

import org.junit.Test;
import org.openqa.grid.selenium.GridLauncherV3;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.net.UrlChecker;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * Ensure that launching the hub / node in most common ways simulating command line args works
 */
public class GridViaCommandLineTest {

  @Test
  public void testRegisterNodeToHub() throws Exception {
    String[] hubArgs = {"-role", "hub"};
    GridLauncherV3.main(hubArgs);
    UrlChecker urlChecker = new UrlChecker();
    urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL("http://localhost:4444/grid/console"));

    String[] nodeArgs = {"-role", "node", "-hub", "http://localhost:4444", "-browser", "browserName=chrome,maxInstances=1"};
    GridLauncherV3.main(nodeArgs);
    urlChecker.waitUntilAvailable(100, TimeUnit.SECONDS, new URL("http://localhost:5555/wd/hub/status"));

    WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),
                                                   DesiredCapabilities.chrome());

    try {
      driver.get("http://localhost:4444/grid/console");
      Assert.assertEquals("Should only have one chrome registered to the hub", 1, driver.findElements(By.cssSelector("img[src$='chrome.png']")).size());
    } finally {
      try {
        driver.quit();
      } catch (Exception e) {}
    }


  }
}

@@ -843,32 +843,26 @@
 ort 
+org.
 junit.
-framework.
 Assert;%0A
 %0Aimp
@@ -857,17 +857,16 @@
 Assert;%0A
-%0A
 import o
@@ -993,24 +993,67 @@
 .WebDriver;%0A
+import org.openqa.selenium.net.PortProber;%0A
 import org.o
@@ -1457,16 +1457,65 @@
 ption %7B%0A
+    Integer hubPort = PortProber.findFreePort();%0A
     Stri
@@ -1544,16 +1544,45 @@
 %22, %22hub%22
+, %22-port%22, hubPort.toString()
 %7D;%0A    G
@@ -1712,32 +1712,46 @@
 ECONDS, new URL(
+String.format(
 %22http://localhos
@@ -1744,36 +1744,34 @@
 ttp://localhost:
-4444
+%25d
 /grid/console%22))
@@ -1768,17 +1768,79 @@
 console%22
-)
+, hubPort)));%0A%0A%0A    Integer nodePort = PortProber.findFreePort(
 );%0A%0A    
@@ -1907,13 +1907,19 @@
 ost:
-4444%22
+%22 + hubPort
 , %22-
@@ -1963,16 +1963,46 @@
 ances=1%22
+, %22-port%22, nodePort.toString()
 %7D;%0A    G
@@ -2088,32 +2088,46 @@
 ECONDS, new URL(
+String.format(
 %22http://localhos
@@ -2132,12 +2132,10 @@
 ost:
-5555
+%25d
 /wd/
@@ -2145,16 +2145,27 @@
 /status%22
+, nodePort)
 ));%0A%0A   
@@ -2212,16 +2212,30 @@
 new URL(
+String.format(
 %22http://
@@ -2248,20 +2248,18 @@
 ost:
-4444
+%25d
 /wd/hub%22
 ),%0A 
@@ -2254,16 +2254,26 @@
 /wd/hub%22
+, hubPort)
 ),%0A     
@@ -2377,16 +2377,30 @@
 ver.get(
+String.format(
 %22http://
@@ -2413,12 +2413,10 @@
 ost:
-4444
+%25d
 /gri
@@ -2425,16 +2425,26 @@
 console%22
+, hubPort)
 );%0A     

use port prober in grid test, anything could be running on the default ports... need to find and clean up other tests