How to use NetworkUtils class of org.openqa.selenium.net package

Best Selenium code snippet using org.openqa.selenium.net.NetworkUtils

Source:OutOfProcessSeleniumServer.java Github

copy

Full Screen

...16// under the License.17package org.openqa.selenium.testing.drivers;18import static java.util.concurrent.TimeUnit.SECONDS;19import org.openqa.selenium.build.BuckBuild;20import org.openqa.selenium.net.NetworkUtils;21import org.openqa.selenium.net.PortProber;22import org.openqa.selenium.net.UrlChecker;23import org.openqa.selenium.os.CommandLine;24import org.openqa.selenium.build.InProject;25import java.io.IOException;26import java.net.MalformedURLException;27import java.net.URL;28import java.nio.file.Path;29import java.util.Arrays;30import java.util.LinkedList;31import java.util.List;32import java.util.logging.Logger;33public class OutOfProcessSeleniumServer {34 private static final Logger log = Logger.getLogger(OutOfProcessSeleniumServer.class.getName());35 private String baseUrl;36 private CommandLine command;37 @SuppressWarnings("unused")38 private boolean captureLogs = false;39 public void enableLogCapture() {40 captureLogs = true;41 }42 /**43 * Creates an out of process server with log capture enabled.44 *45 * @return The new server.46 */47 public OutOfProcessSeleniumServer start(String... extraFlags) throws IOException {48 log.info("Got a request to start a new selenium server");49 if (command != null) {50 log.info("Server already started");51 throw new RuntimeException("Server already started");52 }53 String serverJar = buildServerAndClasspath();54 int port = PortProber.findFreePort();55 String localAddress = new NetworkUtils().getPrivateLocalAddress();56 baseUrl = String.format("http://%s:%d", localAddress, port);57 List<String> cmdLine = new LinkedList<>();58 cmdLine.add("java");59 cmdLine.add("-jar");60 cmdLine.add(serverJar);61 cmdLine.add("-port");62 cmdLine.add(String.valueOf(port));63 cmdLine.addAll(Arrays.asList(extraFlags));64 command = new CommandLine(cmdLine.toArray(new String[cmdLine.size()]));65 if (Boolean.getBoolean("webdriver.development")) {66 command.copyOutputTo(System.err);67 }68 command.setWorkingDirectory(69 InProject.locate("Rakefile").getParent().toAbsolutePath().toString());...

Full Screen

Full Screen

Source:TestServerUtils.java Github

copy

Full Screen

...14\*-------------------------------------------------------------------------------------------------------------------*/15package ${package}.utilities.server;16import org.seleniumhq.jetty9.server.Server;17import org.seleniumhq.jetty9.server.handler.ResourceHandler;18import org.openqa.selenium.net.NetworkUtils;19import org.openqa.selenium.net.PortProber;20import ${package}.logging.AppLogger;21/**22 * Leverages Jetty to create a locally running HTTP server. The server will bind to a dynamically available port.23 * Once bound, it is used by the example test classes for performing Web interactions.24 * 25 * Note: This server does not bind to any public IP address.26 */27public class TestServerUtils {28 static int serverPort;29 static String localIP;30 private static String TEST_APP_FILE = "/testapp.html";31 public static final String TEST_PAGE_DIR = "src/test/resources/testPages";32 static Server server;33 private static void createServer() {34 serverPort = PortProber.findFreePort();35 localIP = new NetworkUtils().getPrivateLocalAddress();36 initServer();37 }38 private static void initServer() {39 server = new Server(serverPort);40 ResourceHandler handler = new ResourceHandler();41 handler.setDirectoriesListed(true);42 handler.setResourceBase(TEST_PAGE_DIR);43 server.setHandler(handler);44 }45 public static void startServer() throws Exception {46 if (server == null) {47 createServer();48 }49 if (!server.isRunning()) {...

Full Screen

Full Screen

Source:PhabricatorAppServer.java Github

copy

Full Screen

1package org.phabricator.sprint.selenium.environment.webserver;2import org.openqa.selenium.net.NetworkUtils;3import java.io.File;4import javax.servlet.Filter;5import javax.servlet.Servlet;6import static org.openqa.selenium.net.PortProber.findFreePort;7public class PhabricatorAppServer implements AppServer {8 private static final String HOSTNAME_FOR_TEST_ENV_NAME = "HOSTNAME";9 private static final String ALTERNATIVE_HOSTNAME_FOR_TEST_ENV_NAME = "ALTERNATIVE_HOSTNAME";10 private static final String FIXED_HTTP_PORT_ENV_NAME = "TEST_HTTP_PORT";11 private static final String FIXED_HTTPS_PORT_ENV_NAME = "TEST_HTTPS_PORT";12 private static final int DEFAULT_HTTP_PORT = 80;13 private static final int DEFAULT_HTTPS_PORT = 2410;14 private static final String DEFAULT_CONTEXT_PATH = "/";15 private static final String JS_SRC_CONTEXT_PATH = "/javascript";16 private static final String CLOSURE_CONTEXT_PATH = "/third_party/closure/goog";17 private static final String THIRD_PARTY_JS_CONTEXT_PATH = "/third_party/js";18 private static final NetworkUtils networkUtils = new NetworkUtils();19 private int port;20 private int securePort;21 private File path;22 private File jsSrcRoot;23 private final String hostName;24 public PhabricatorAppServer() {25 this(detectHostname());26 }27 public static String detectHostname() {28 String hostnameFromProperty = System.getenv(HOSTNAME_FOR_TEST_ENV_NAME);29 return hostnameFromProperty == null ? "localhost" : hostnameFromProperty;30 }31 public PhabricatorAppServer(String hostName) {32 this.hostName = "phab08.wmflabs.org";...

Full Screen

Full Screen

Source:SelendroidJarSpawnerTest.java Github

copy

Full Screen

...14\*-------------------------------------------------------------------------------------------------------------------*/15package com.paypal.selion.grid;16import static org.testng.Assert.assertFalse;17import static org.testng.Assert.fail;18import org.openqa.selenium.net.NetworkUtils;19import org.openqa.selenium.net.PortProber;20import org.testng.annotations.BeforeClass;21import org.testng.annotations.Test;22@Test(singleThreaded = true, groups = { "android-only", "downloads-dependencies" })23public class SelendroidJarSpawnerTest {24 private Thread thread;25 private RunnableLauncher spawner;26 @BeforeClass27 public void beforeClass() {28 String host = new NetworkUtils().getIpOfLoopBackIp4();29 int port = PortProber.findFreePort();30 spawner = new SelendroidJarSpawner(new String[] { "-host", host, "-port", String.valueOf(port) },31 new ProcessLauncherConfiguration().setContinuouslyRestart(false).setIncludeJavaSystemProperties(false)32 .setIncludeParentProcessClassPath(false));33 thread = new Thread(spawner);34 }35 @Test36 public void testStartServer() throws Exception {37 thread.start();38 // wait for it to start, max 120 seconds39 int attempts = 0;40 while (!spawner.isRunning() && (attempts < 12)) {41 Thread.sleep(10000);42 attempts += 1;...

Full Screen

Full Screen

Source:IOSDriverJarSpawnerTest.java Github

copy

Full Screen

...14\*-------------------------------------------------------------------------------------------------------------------*/15package com.paypal.selion.grid;16import static org.testng.Assert.assertFalse;17import static org.testng.Assert.fail;18import org.openqa.selenium.net.NetworkUtils;19import org.openqa.selenium.net.PortProber;20import org.testng.annotations.BeforeClass;21import org.testng.annotations.Test;22@Test(singleThreaded = true, groups = { "ios-only", "downloads-dependencies" })23public class IOSDriverJarSpawnerTest {24 private Thread thread;25 private RunnableLauncher spawner;26 @BeforeClass27 public void beforeClass() {28 String host = new NetworkUtils().getIpOfLoopBackIp4();29 int port = PortProber.findFreePort();30 spawner = new IOSDriverJarSpawner(new String[] { "-host", host, "-port", String.valueOf(port) },31 new ProcessLauncherConfiguration().setContinuouslyRestart(false).setIncludeJavaSystemProperties(false)32 .setIncludeParentProcessClassPath(false));33 thread = new Thread(spawner);34 }35 @Test36 public void testStartServer() throws Exception {37 thread.start();38 // wait for it to start, max 120 seconds39 int attempts = 0;40 while (!spawner.isRunning() && (attempts < 12)) {41 Thread.sleep(10000);42 attempts += 1;...

Full Screen

Full Screen

Source:SeleniumServerStarter.java Github

copy

Full Screen

...89import org.openqa.selenium.Build;10import org.openqa.selenium.os.CommandLine;11import org.openqa.selenium.net.PortProber;12import org.openqa.selenium.net.NetworkUtils;1314import static java.util.concurrent.TimeUnit.*;15import static org.openqa.selenium.TestWaiter.waitFor;16import static org.openqa.selenium.net.PortProber.freeLocalPort;17import static org.openqa.selenium.net.PortProber.pollPort;1819@SuppressWarnings({"UnusedDeclaration"})20public class SeleniumServerStarter extends TestSetup {2122 private static final NetworkUtils networkUtils = new NetworkUtils();23 private static final String SELENIUM_JAR = "build/java/server/test/org/openqa/selenium/server-with-tests-standalone.jar";24 private CommandLine command;2526 public SeleniumServerStarter(Test test) {27 super(test);28 }2930 @Override31 protected void setUp() throws Exception {32 // Walk up the path until we find the "third_party/selenium" directory33 File seleniumJar = findSeleniumJar();3435 if (!seleniumJar.exists()) {36 new Build().of("//java/server/test/org/openqa/selenium:server-with-tests:uber").go(); ...

Full Screen

Full Screen

Source:ProxyServer.java Github

copy

Full Screen

...19import org.jboss.netty.handler.codec.http.HttpRequest;20import org.littleshoot.proxy.DefaultHttpProxyServer;21import org.littleshoot.proxy.HttpRequestFilter;22import org.openqa.selenium.Proxy;23import org.openqa.selenium.net.NetworkUtils;24import org.openqa.selenium.net.PortProber;25import java.util.List;26public class ProxyServer {27 private DefaultHttpProxyServer proxyServer;28 private final String baseUrl;29 private final List<String> uris = Lists.newLinkedList();30 public ProxyServer() {31 int port = PortProber.findFreePort();32 String address = new NetworkUtils().getPrivateLocalAddress();33 baseUrl = String.format("%s:%d", address, port);34 proxyServer = new DefaultHttpProxyServer(port, new HttpRequestFilter() {35 @Override36 public void filter(HttpRequest httpRequest) {37 String uri = httpRequest.getUri();38 String[] parts = uri.split("/");39 if (parts.length == 0) {40 return;41 }42 String finalPart = parts[parts.length - 1];43 uris.add(finalPart);44 }45 });46 proxyServer.start();...

Full Screen

Full Screen

Source:BaseServerOptions.java Github

copy

Full Screen

...16// under the License.17package org.openqa.selenium.grid.server;18import org.openqa.selenium.grid.config.Config;19import org.openqa.selenium.grid.config.ConfigException;20import org.openqa.selenium.net.NetworkUtils;21import org.openqa.selenium.net.PortProber;22import java.net.URI;23import java.net.URISyntaxException;24import java.util.Optional;25public class BaseServerOptions {26 private final Config config;27 private int port = -1;28 public BaseServerOptions(Config config) {29 this.config = config;30 }31 public Optional<String> getHostname() {32 return config.get("server", "hostname");33 }34 public int getPort() {35 if (port != -1) {36 return port;37 }38 int port = config.getInt("server", "port")39 .orElseGet(PortProber::findFreePort);40 if (port < 0) {41 throw new ConfigException("Port cannot be less than 0: " + port);42 }43 this.port = port;44 return port;45 }46 public int getMaxServerThreads() {47 int count = config.getInt("server", "max-threads")48 .orElse(200);49 if (count < 0) {50 throw new ConfigException("Maximum number of server threads cannot be less than 0: " + count);51 }52 return count;53 }54 public URI getExternalUri() {55 // Assume the host given is addressable if it's been set56 String host = getHostname()57 .orElseGet(() -> new NetworkUtils().getNonLoopbackAddressOfThisMachine());58 int port = getPort();59 try {60 return new URI("http", null, host, port, null, null, null);61 } catch (URISyntaxException e) {62 throw new ConfigException("Cannot determine external URI: " + e.getMessage());63 }64 }65}...

Full Screen

Full Screen

NetworkUtils

Using AI Code Generation

copy

Full Screen

1import java.net.InetAddress;2import java.net.UnknownHostException;3public class GetIPAddress {4public static void main(String[] args) {5try {6InetAddress myIP=InetAddress.getLocalHost();7System.out.println("My IP Address is:");8System.out.println(myIP.getHostAddress());9} catch (UnknownHostException e) {10e.printStackTrace();11}12}13}

Full Screen

Full Screen

NetworkUtils

Using AI Code Generation

copy

Full Screen

1public class AppiumServer {2 public static void main(String[] args) throws Exception {3 .buildDefaultService();4 service.start();5 System.out.println("Appium server started at port " + service.getPort());6 System.out.println("Appium server started at ip " + NetworkUtils7 .getIpOfLoopBackIp4());8 System.in.read();9 service.stop();10 }11}

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful