How to use ConnectionFailedException class of org.openqa.selenium.remote.http package

Best Selenium code snippet using org.openqa.selenium.remote.http.ConnectionFailedException

ConnectionFailedException org.openqa.selenium.remote.http.ConnectionFailedException

The ConnectionFailedException occured when client is not able to establish connection with selenium hub or webdriver endpoints.

Example

It is an example code for setting up the chrome browser. If the browser has crashed during the webdriver initialization process then client throws the ConnectionFailedException

copy
1System.setProperty("webdriver.chrome.driver", FilePath + "\drivers\chromedriver.exe"); 2HashMap<String, Object> chromePrefs = new HashMap<String, Object>(); 3chromePrefs.put("profile.default_content_settings.popups", 0); 4chromePrefs.put("download.default_directory", FilePath); 5ChromeOptions options = new ChromeOptions(); 6HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>(); 7options.setExperimentalOption("prefs", chromePrefs); 8options.addArguments("--disable-notifications"); 9options.addArguments("--disable-popup-blocking"); 10options.addArguments("--headless"); 11options.addArguments("--disable-gpu"); 12options.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap); 13options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); 14options.setCapability(ChromeOptions.CAPABILITY, options); 15options.setPageLoadStrategy(PageLoadStrategy.NORMAL); 16driver = new ChromeDriver(options);

the output of error will looks as follow

copy
1org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:60650/devtools/browser/9faa33dd-c789-47f4-acab-261acc7045e7 2Build info: version: '4.0.0-beta-3', revision: '5d108f9a67' 3System info: host: 'xxx', ip: 'xxx', os.name: 'Windows Server 2016', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_202' 4Driver info: driver.version: ChromeDriver 5 at org.openqa.selenium.remote.http.netty.NettyWebSocket.<init>(NettyWebSocket.java:104) 6 at org.openqa.selenium.remote.http.netty.NettyWebSocket.lambda$create$3(NettyWebSocket.java:136) 7 at org.openqa.selenium.remote.http.netty.NettyClient.openSocket(NettyClient.java:111) 8 at org.openqa.selenium.devtools.Connection.<init>(Connection.java:73) 9 at org.openqa.selenium.chromium.ChromiumDriver.lambda$new$1(ChromiumDriver.java:102) 10 at java.util.Optional.map(Unknown Source) 11 at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:100) 12 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:99) 13 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:86) 14 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:75) 15 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 16 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 17 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 18 at java.lang.reflect.Method.invoke(Unknown Source) 19 org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:60650/devtools/browser/9faa33dd-c789-47f4-acab-261acc7045e7 20Build info: version: '4.0.0-beta-3', revision: '5d108f9a67' 21System info: host: 'xxx', ip: 'xxx', os.name: 'Windows Server 2016', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_202' 22Driver info: driver.version: ChromeDriver 23 at org.openqa.selenium.remote.http.netty.NettyWebSocket.<init>(NettyWebSocket.java:104) 24 at org.openqa.selenium.remote.http.netty.NettyWebSocket.lambda$create$3(NettyWebSocket.java:136) 25 at org.openqa.selenium.remote.http.netty.NettyClient.openSocket(NettyClient.java:111) 26 at org.openqa.selenium.devtools.Connection.<init>(Connection.java:73) 27 at org.openqa.selenium.chromium.ChromiumDriver.lambda$new$1(ChromiumDriver.java:102) 28 at java.util.Optional.map(Unknown Source) 29 at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:100) 30 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:99) 31 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:86) 32 at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:75) 33 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 34 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 35 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 36 at java.lang.reflect.Method.invoke(Unknown Source)

Solutions

  • Check browser crash logs
  • Check /dev/shm partition size, Small sizes often crash browsers especially in docker
  • Try to execute with below flags
copy
1options.addArguments("--no-sandbox"); 2options.addArguments("--disable-dev-shm-usage");

Code Snippets

Here are code snippets that can help you understand more how developers are using

Source:WebSocketServingTest.java Github

copy

Full Screen

...21import org.openqa.selenium.grid.config.MapConfig;22import org.openqa.selenium.grid.server.BaseServerOptions;23import org.openqa.selenium.grid.server.Server;24import org.openqa.selenium.net.PortProber;25import org.openqa.selenium.remote.http.ConnectionFailedException;26import org.openqa.selenium.remote.http.Contents;27import org.openqa.selenium.remote.http.HttpClient;28import org.openqa.selenium.remote.http.HttpRequest;29import org.openqa.selenium.remote.http.HttpResponse;30import org.openqa.selenium.remote.http.Message;31import org.openqa.selenium.remote.http.TextMessage;32import org.openqa.selenium.remote.http.WebSocket;33import org.openqa.selenium.support.ui.FluentWait;34import org.openqa.selenium.testing.Safely;35import java.util.LinkedList;36import java.util.List;37import java.util.Optional;38import java.util.concurrent.CountDownLatch;39import java.util.concurrent.Executors;40import java.util.concurrent.ScheduledExecutorService;41import java.util.concurrent.atomic.AtomicBoolean;42import java.util.function.BiFunction;43import java.util.function.Consumer;44import static java.util.concurrent.TimeUnit.MILLISECONDS;45import static java.util.concurrent.TimeUnit.SECONDS;46import static org.assertj.core.api.Assertions.assertThat;47import static org.openqa.selenium.remote.http.Contents.utf8String;48import static org.openqa.selenium.remote.http.HttpMethod.GET;49public class WebSocketServingTest {50 private Server<?> server;51 @After52 public void shutDown() {53 Safely.safelyCall(() -> server.stop());54 }55 @Test(expected = ConnectionFailedException.class)56 public void clientShouldThrowAnExceptionIfUnableToConnectToAWebSocketEndPoint() {57 server = new NettyServer(defaultOptions(), req -> new HttpResponse()).start();58 HttpClient client = HttpClient.Factory.createDefault().createClient(server.getUrl());59 client.openSocket(new HttpRequest(GET, "/does-not-exist"), new WebSocket.Listener() {});60 }61 @Test62 public void shouldUseUriToChooseWhichWebSocketHandlerToUse() throws InterruptedException {63 AtomicBoolean foo = new AtomicBoolean(false);64 AtomicBoolean bar = new AtomicBoolean(false);65 BiFunction<String, Consumer<Message>, Optional<Consumer<Message>>> factory = (str, sink) -> {66 if ("/foo".equals(str)) {67 return Optional.of(msg -> {68 foo.set(true);69 sink.accept(new TextMessage("Foo called"));...

Full Screen

Full Screen

Source:NettyWebSocket.java Github

copy

Full Screen

...24import org.openqa.selenium.internal.Require;25import org.openqa.selenium.remote.http.BinaryMessage;26import org.openqa.selenium.remote.http.ClientConfig;27import org.openqa.selenium.remote.http.CloseMessage;28import org.openqa.selenium.remote.http.ConnectionFailedException;29import org.openqa.selenium.remote.http.Filter;30import org.openqa.selenium.remote.http.HttpRequest;31import org.openqa.selenium.remote.http.HttpResponse;32import org.openqa.selenium.remote.http.Message;33import org.openqa.selenium.remote.http.TextMessage;34import org.openqa.selenium.remote.http.WebSocket;35import java.net.MalformedURLException;36import java.net.URI;37import java.net.URISyntaxException;38import java.net.URL;39import java.util.concurrent.ExecutionException;40import java.util.concurrent.atomic.AtomicReference;41import java.util.function.BiFunction;42import java.util.function.Function;43import java.util.logging.Level;44import java.util.logging.Logger;45class NettyWebSocket implements WebSocket {46 private static final Logger log = Logger.getLogger(NettyWebSocket.class.getName());47 private final org.asynchttpclient.ws.WebSocket socket;48 private NettyWebSocket(AsyncHttpClient client, Request request, Listener listener) {49 Require.nonNull("HTTP client", client);50 Require.nonNull("WebSocket listener", listener);51 try {52 URL origUrl = new URL(request.getUrl());53 String wsScheme = "https".equalsIgnoreCase(origUrl.getProtocol()) ? "wss" : "ws";54 URI wsUri = new URI(wsScheme, null, origUrl.getHost(), origUrl.getPort(), origUrl.getPath(), null, null);55 ListenableFuture<org.asynchttpclient.netty.ws.NettyWebSocket> future =56 client.prepareGet(wsUri.toString()).execute(57 new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() {58 @Override59 public void onOpen(org.asynchttpclient.ws.WebSocket websocket) {60 }61 @Override62 public void onClose(org.asynchttpclient.ws.WebSocket websocket, int code, String reason) {63 listener.onClose(code, reason);64 }65 @Override66 public void onError(Throwable t) {67 listener.onError(t);68 }69 @Override70 public void onBinaryFrame(byte[] payload, boolean finalFragment, int rsv) {71 if (payload != null) {72 listener.onBinary(payload);73 }74 }75 @Override76 public void onTextFrame(String payload, boolean finalFragment, int rsv) {77 if (payload != null) {78 listener.onText(payload);79 }80 }81 }).build());82 socket = future.toCompletableFuture()83 .exceptionally(t -> {84 log.log(Level.WARNING, t.getMessage(), t);85 return null;86 })87 .get();88 if (socket == null) {89 throw new ConnectionFailedException("Unable to establish websocket connection to " + request.getUrl());90 }91 } catch (InterruptedException e) {92 Thread.currentThread().interrupt();93 log.log(Level.WARNING, "NettyWebSocket initial request interrupted", e);94 throw new ConnectionFailedException("NettyWebSocket initial request interrupted", e);95 } catch (ExecutionException | MalformedURLException | URISyntaxException e) {96 throw new ConnectionFailedException("NettyWebSocket initial request execution error", e);97 }98 }99 static BiFunction<HttpRequest, Listener, WebSocket> create(ClientConfig config, AsyncHttpClient client) {100 Filter filter = config.filter();101 Function<HttpRequest, HttpRequest> filterRequest = req -> {102 AtomicReference<HttpRequest> ref = new AtomicReference<>();103 filter.andFinally(in -> {104 ref.set(in);105 return new HttpResponse();106 }).execute(req);107 return ref.get();108 };109 return (req, listener) -> {110 HttpRequest filtered = filterRequest.apply(req);...

Full Screen

Full Screen

ConnectionFailedException

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.ConnectionFailedException;2import org.openqa.selenium.remote.http.HttpHandler;3import org.openqa.selenium.remote.http.HttpRequest;4import org.openqa.selenium.remote.http.HttpResponse;5import org.openqa.selenium.remote.http.HttpClient;6import org.openqa.selenium.remote.http.HttpTimeouts;7import org.openqa.selenium.remote.http.HttpClient.Factory;8import org.openqa.selenium.remote.http.HttpClient.Builder;9import org.openqa.selenium.remote.http.HttpClient.Builder;10public class ConnectionFailedExceptionDemo {11 public static void main(String[] args) {12 ConnectionFailedException cfe = new ConnectionFailedException("Connection Failed");13 System.out.println(cfe.getMessage());14 System.out.println(cfe.getCause());15 cfe.printStackTrace();16 HttpHandler handler = new HttpHandler() {17 public HttpResponse execute(HttpRequest request) throws IOException {18 return null;19 }20 };21 HttpClient client = new HttpClient() {22 public HttpResponse execute(HttpRequest request) throws IOException {23 return null;24 }25 };26 HttpTimeouts timeouts = new HttpTimeouts() {27 public void setReadTimeout(Duration readTimeout) {28 }29 public void setConnectionTimeout(Duration connectionTimeout) {30 }31 public Duration getReadTimeout() {

Full Screen

Full Screen

ConnectionFailedException

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.ConnectionFailedException;2import org.openqa.selenium.remote.http.HttpHandler;3import org.openqa.selenium.remote.http.HttpRequest;4import org.openqa.selenium.remote.http.HttpResponse;5import org.openqa.selenium.remote.http.UrlFilter;6import org.openqa.selenium.WebDriverException;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebDriverException;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.WebDriverException;11import org.openqa.selenium.WebDriver;12import org.openqa.selenium.WebDriverException;13import org.openqa.selenium.WebDriver;14import org.openqa.selenium.WebDriverException;15import org.openqa.selenium.WebDriver;16import org.openqa.selenium.WebDriverException;17import org.openqa.selenium.WebDriver;18import org.openqa.selenium.WebDriverException;19import org.openqa.selenium.WebDriver;20import org.openqa.selenium.WebDriverException;21import org.openqa.selenium.WebDriver;22import org.openqa.selenium.WebDriverException;23import org.openqa.selenium.WebDriver;24import org.openqa.selenium.WebDriverException;25import org.openqa.selenium.WebDriver;26import

Full Screen

Full Screen

ConnectionFailedException

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.ConnectionFailedException;2import org.openqa.selenium.WebDriverException;3import org.openqa.selenium.NoSuchElementException;4import org.openqa.selenium.support.ui.TimeoutException;5import org.openqa.selenium.support.ui.FluentWait;6import org.openqa.selenium.By;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeDriver;9import org.openqa.selenium.chrome.ChromeOptions;10import org.openqa.selenium.remote.DesiredCapabilities;11import java.io.File;12import java.util.concurrent.TimeUnit;13import java.util.concurrent.TimeUnit;14import org.openqa.selenium.support.ui.WebDriverWait;15import org.openqa.selenium.support.ui.ExpectedConditions;16import org.openqa.selenium.interactions.Actions;17import org.openqa.selenium.Keys;18import org.openqa.selenium.JavascriptExecutor;19import org.openqa.selenium.Alert;20import org.openqa.selenium.Dimension;21import org.openqa.selenium.Point;22import org.openqa.selenium.WebElement;23import org.openqa.selenium.support.ui.WebDriverWait;24import org.openqa.selenium.support.ui.ExpectedConditions;25import org.openqa

Full Screen

Full Screen

ConnectionFailedException

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.ConnectionFailedException;2import org.openqa.selenium.remote.http.ConnectionClosedException;3import java.io.IOException;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.remote.RemoteWebDriver;6import org.openqa.selenium.remote.DesiredCapabilities;7import java.net.URL;8import org.openqa.selenium.BrowserType;9import org.openqa.selenium.By;10import org.openqa.selenium.WebElement;11public class RemoteWebDriverExample {12 public static void main(String[] args) throws IOException {13 DesiredCapabilities caps = new DesiredCapabilities();14 caps.setBrowserName(BrowserType.CHROME);15 caps.setPlatform(org.openqa.selenium.Platform.WINDOWS);16 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Dell\\Downloads\\chromedriver_win32\\chromedriver.exe");17 WebElement element = driver.findElement(By.name("q"));18 element.sendKeys("Selenium");19 element.submit();20 System.out.println("Page title is: " + driver.getTitle());21 driver.quit();22 }23}

Full Screen

Full Screen

ConnectionFailedException

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.ConnectionFailedException;2import org.openqa.selenium.WebDriverException;3import org.openqa.selenium.By;4import org.openqa.selenium.support.ui.ExpectedConditions;5import org.openqa.selenium.support.ui.WebDriverWait;6import org.openqa.selenium.remote.DesiredCapabilities;7import io.appium.java_client.android.AndroidDriver;8import java.net.URL;9import java.net.MalformedURLException;10import java.util.concurrent.TimeUnit;11import org.openqa.selenium.WebElement;12import io.appium.java_client.TouchAction;13import io.appium.java_client.android.AndroidElement;14import io.appium.java_client.MobileElement;15import io.appium.java_client.MobileBy;16import io.appium.java_client.MobileDriver;17import io.appium.java_client.MobileElement;18import io.appium.java_client.TouchAction;19import io.appium.java_client.AppiumDriver;20import io.appium.java_client.TouchAction;21import io.appium.java_client.touch.offset.PointOption;22import io.appium.java_client.TouchAction;

Full Screen

Full Screen

ConnectionFailedException

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.remote.http.ConnectionFailedException;2import org.openqa.selenium.WebDriverException;3import org.openqa.selenium.remote.RemoteWebDriver;4import org.openqa.selenium.remote.DesiredCapabilities;5import java.net.URL;6import java.net.MalformedURLException;7import org.openqa.selenium.chrome.ChromeDriver;8import org.openqa.selenium.firefox.FirefoxDriver;9import org.openqa.selenium.ie.InternetExplorerDriver;10import org.openqa.selenium.edge.EdgeDriver;11import org.openqa.selenium.safari.SafariDriver;12import org.openqa.selenium.opera.OperaDriver;13import org.openqa.selenium.phantomjs.PhantomJSDriver;14import org.openqa.selenium.edge.EdgeOptions;15import org.openqa.selenium.opera.OperaOptions;16import org.openqa.selenium.phantomjs.PhantomJSOptions;17import org.openqa.selenium.chrome.ChromeOptions;18import org.openqa.selenium.firefox.FirefoxOptions;19import org.openqa.selenium.ie.InternetExplorerOptions;20import org.openqa.selenium.safari.SafariOptions;21import org.openqa.selenium.remote.DesiredCapabilities;22import org

Full Screen

Full Screen

ConnectionFailedException

Using AI Code Generation

copy

Full Screen

1package org.example;2import org.openqa.selenium.remote.http.ConnectionFailedException;3import org.openqa.selenium.remote.http.HttpClient;4import org.openqa.selenium.remote.http.HttpRequest;5import org.openqa.selenium.remote.http.HttpResponse;6import java.io.IOException;7public class ConnectionFailedExceptionExample {8 public static void main(String[] args) {9 HttpClient client = new HttpClient();10 try {11 HttpResponse response = client.execute(request);12 System.out.println("Response Status Code: " + response.getStatus());13 } catch (IOException e) {14 if (e instanceof ConnectionFailedException) {15 System.out.println("Connection Failed Exception: " + e.getMessage());16 }17 }18 }19}

Full Screen

Full Screen
copy
1import com.google.common.base.Charsets;2import com.google.common.io.Files;34// ...56String text = Files.toString(new File(path), Charsets.UTF_8);7
Full Screen
copy
1import java.nio.file.Files;2
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.

...Most popular Stackoverflow questions on ConnectionFailedException

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