@Override public int startServer(TestSession session) throws ServerException { port = PortProber.findFreePort(); String[] args = getArgs(port); if (LOG.isLoggable(Level.INFO)) { LOG.info(String.format("Spawning a Selenium server using the arguments [%s]", Arrays.toString(args))); } ProcessBuilder pb = new ProcessBuilder(getArgs(port)); try { this.process = pb.start(); return port; } catch (Exception e) { throw new ServerException(e.getMessage(), e); } }
private int getCheckedAvailablePort(int configuredPort, String serverProtocol) { int actualPort; if (isAvailablePort(configuredPort)) { actualPort = configuredPort; } else { actualPort = PortProber.findFreePort(); LOG.debug("Configured or default port = " + configuredPort + " for" + serverProtocol.toUpperCase() + " server is not available!!!"); LOG.debug("Founded free port for " + serverProtocol.toUpperCase() + " is " + actualPort); } return actualPort; }
public static HttpServer start(String pathToServe) throws IOException { HttpServer httpServer = HttpServer.create(); httpServer.createContext("/", createHttpExchange(pathToServe)); httpServer.bind(new InetSocketAddress("localhost", PortProber.findFreePort()), 0); httpServer.start(); return httpServer; }
@Before public void beforeEachTest() throws IOException { httpServer = HttpServer.create(); httpServer.createContext("/webdriverTest", httpExchange -> { String response = "Welcome Real's HowTo test page"; httpExchange.sendResponseHeaders(200, response.length()); OutputStream os = httpExchange.getResponseBody(); os.write(response.getBytes()); os.close(); }); httpServer.bind(new InetSocketAddress("localhost", PortProber.findFreePort()), 0); httpServer.start(); }
/** * Checks out the next available unused port. Callers should send the returned port * back to {@link #releaseUnusedPort(int)} when it's no longer being used. * * @throws NoSuchElementException if no unused port is available. */ public synchronized int checkOutUnusedPort() { if (availablePorts == null) { return PortProber.findFreePort(); } else if (availablePorts.isEmpty()) { // All available ports are checked out. throw new NoSuchElementException("No unused ports are available"); } return availablePorts.pop(); }
private static void foo() throws Exception { boolean debug = Boolean.parseBoolean(System.getProperty("debug", "false")); String dockerHost = String.format("http://%s:%d", CENTOS, 2375); DockerClient docker = null; try { docker = DefaultDockerClient.builder() .uri(dockerHost).build(); Info info = docker.info(); System.err.println("Information : " + info); if (debug) { docker.pull(SELENIUM_STANDALONE_CHROME, new LoggingBuildHandler()); } else { docker.pull(SELENIUM_STANDALONE_CHROME); } final ImageInfo imageInfo = docker.inspectImage(SELENIUM_STANDALONE_CHROME); System.err.println("Information : " + imageInfo); // Bind container ports to host ports // final String[] ports = {Integer.toString(PortProber.findFreePort())}; final String[] ports = {"4444"}; final Map<String, List<PortBinding>> portBindings = new HashMap<>(); for (String port : ports) { List<PortBinding> hostPorts = new ArrayList<>(); hostPorts.add(PortBinding.of("0.0.0.0", PortProber.findFreePort())); portBindings.put(port, hostPorts); } // // Bind container port 443 to an automatically allocated available host port. // List<PortBinding> randomPort = new ArrayList<>(); // randomPort.add(PortBinding.randomPort("0.0.0.0")); // portBindings.put("443", randomPort); System.err.println("Printing the port mappings : " + portBindings); final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build(); final ContainerConfig containerConfig = ContainerConfig.builder() .hostConfig(hostConfig) .image(SELENIUM_STANDALONE_CHROME).exposedPorts(ports) .build(); final ContainerCreation creation = docker.createContainer(containerConfig); final String id = creation.id(); // Inspect container final ContainerInfo containerInfo = docker.inspectContainer(id); System.err.println("Container Information " + containerInfo); String msg = "Checking to see if the container with id [" + id + "] and name [" + containerInfo.name() + "]..."; System.err.println(msg); if (! containerInfo.state().running()) { // Start container docker.startContainer(id); System.err.println(containerInfo.name() + " is now running."); } else { System.err.println(containerInfo.name() + " was already running."); } System.err.println("Lets wait here !!!"); } finally { if (docker != null) { docker.close(); } } }
private static void createServer() { serverPort = PortProber.findFreePort(); localIP = new NetworkUtils().getPrivateLocalAddress(); initServer(); }
@Override protected void before() throws Throwable { port=PortProber.findFreePort(); log.info("found free port: {}",port); }
public FirefoxProfile getDefaultFirefoxProfile() { FirefoxProfile profile = new FirefoxProfile(); boolean generated = false; int newPort = 7055; int i = 100; while (!generated && (--i > 0)) { newPort = PortProber.findFreePort(); generated = firefoxPorts.add(newPort); } if (!generated) { newPort = 7055; } if (firefoxPorts.size() > 20) { firefoxPorts.remove(0); } LOGGER.debug(firefoxPorts); profile.setPreference(FirefoxProfile.PORT_PREFERENCE, newPort); LOGGER.debug("FireFox profile will use '" + newPort + "' port number."); profile.setPreference("dom.max_chrome_script_run_time", 0); profile.setPreference("dom.max_script_run_time", 0); if (Configuration.getBoolean(Configuration.Parameter.AUTO_DOWNLOAD) && !(Configuration.isNull(Configuration.Parameter.AUTO_DOWNLOAD_APPS) || "".equals(Configuration.get(Configuration.Parameter.AUTO_DOWNLOAD_APPS)))) { profile.setPreference("browser.download.folderList", 2); profile.setPreference("browser.download.dir", ReportContext.getArtifactsFolder().getAbsolutePath()); profile.setPreference("browser.helperApps.neverAsk.saveToDisk", Configuration.get(Configuration.Parameter.AUTO_DOWNLOAD_APPS)); profile.setPreference("browser.download.manager.showWhenStarting", false); profile.setPreference("browser.download.saveLinkAsFilenameTimeout", 1); profile.setPreference("pdfjs.disabled", true); profile.setPreference("plugin.scan.plid.all", false); profile.setPreference("plugin.scan.Acrobat", "99.0"); } else if (Configuration.getBoolean(Configuration.Parameter.AUTO_DOWNLOAD) && Configuration.isNull(Configuration.Parameter.AUTO_DOWNLOAD_APPS) || "".equals(Configuration.get(Configuration.Parameter.AUTO_DOWNLOAD_APPS))) { LOGGER.warn( "If you want to enable auto-download for FF please specify '" + Configuration.Parameter.AUTO_DOWNLOAD_APPS.getKey() + "' param"); } profile.setAcceptUntrustedCertificates(true); profile.setAssumeUntrustedCertificateIssuer(true); return profile; }