Java Code Examples for org.openqa.selenium.remote.http.HttpClient#openSocket()

The following examples show how to use org.openqa.selenium.remote.http.HttpClient#openSocket() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyCdpIntoGrid.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<Consumer<Message>> apply(String uri, Consumer<Message> downstream) {
  Objects.requireNonNull(uri);
  Objects.requireNonNull(downstream);

  Optional<SessionId> sessionId = HttpSessionId.getSessionId(uri).map(SessionId::new);
  if (!sessionId.isPresent()) {
    return Optional.empty();
  }

  try {
    Session session = sessions.get(sessionId.get());

    HttpClient client = clientFactory.createClient(ClientConfig.defaultConfig().baseUri(session.getUri()));
    WebSocket upstream = client.openSocket(new HttpRequest(GET, uri), new ForwardingListener(downstream));

    return Optional.of(upstream::send);

  } catch (NoSuchSessionException e) {
    LOG.info("Attempt to connect to non-existant session: " + uri);
    return Optional.empty();
  }
}
 
Example 2
Source File: WebSocketServingTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void webSocketHandlersShouldBeAbleToFireMoreThanOneMessage() {
  server = new NettyServer(
    defaultOptions(),
    req -> new HttpResponse(),
    (uri, sink) -> Optional.of(msg -> {
      sink.accept(new TextMessage("beyaz peynir"));
      sink.accept(new TextMessage("cheddar"));
    })).start();

  HttpClient client = HttpClient.Factory.createDefault().createClient(server.getUrl());
  List<String> messages = new LinkedList<>();
  WebSocket socket = client.openSocket(new HttpRequest(GET, "/cheese"), new WebSocket.Listener() {
    @Override
    public void onText(CharSequence data) {
      messages.add(data.toString());
    }
  });

  socket.send(new TextMessage("Hello"));

  new FluentWait<>(messages).until(msgs -> msgs.size() == 2);
}
 
Example 3
Source File: ProxyNodeCdp.java    From selenium with Apache License 2.0 5 votes vote down vote up
private Consumer<Message> createCdpEndPoint(URI uri, Consumer<Message> downstream) {
  Objects.requireNonNull(uri);

  LOG.info("Establishing CDP connection to " + uri);

  HttpClient client = clientFactory.createClient(ClientConfig.defaultConfig().baseUri(uri));
  WebSocket upstream = client.openSocket(new HttpRequest(GET, uri.toString()), new ForwardingListener(downstream));
  return upstream::send;
}
 
Example 4
Source File: WebSocketServingTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test(expected = ConnectionFailedException.class)
public void clientShouldThrowAnExceptionIfUnableToConnectToAWebSocketEndPoint() {
  server = new NettyServer(defaultOptions(), req -> new HttpResponse()).start();

  HttpClient client = HttpClient.Factory.createDefault().createClient(server.getUrl());

  client.openSocket(new HttpRequest(GET, "/does-not-exist"), new WebSocket.Listener() {});
}
 
Example 5
Source File: WebSocketServingTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUseUriToChooseWhichWebSocketHandlerToUse() throws InterruptedException {
  AtomicBoolean foo = new AtomicBoolean(false);
  AtomicBoolean bar = new AtomicBoolean(false);

  BiFunction<String, Consumer<Message>, Optional<Consumer<Message>>> factory = (str, sink) -> {
    if ("/foo".equals(str)) {
      return Optional.of(msg -> {
        foo.set(true);
        sink.accept(new TextMessage("Foo called"));
      });
    } else {
      return Optional.of(msg -> {
        bar.set(true);
        sink.accept(new TextMessage("Bar called"));
      });
    }
  };

  server = new NettyServer(
    defaultOptions(),
    req -> new HttpResponse(),
    factory
  ).start();

  CountDownLatch latch = new CountDownLatch(1);
  HttpClient client = HttpClient.Factory.createDefault().createClient(server.getUrl());
  WebSocket fooSocket = client.openSocket(new HttpRequest(GET, "/foo"), new WebSocket.Listener() {
    @Override
    public void onText(CharSequence data) {
      System.out.println("Called!");
      latch.countDown();
    }
  });
  fooSocket.sendText("Hello, World!");

  latch.await(2, SECONDS);
  assertThat(foo.get()).isTrue();
  assertThat(bar.get()).isFalse();
}
 
Example 6
Source File: Connection.java    From selenium with Apache License 2.0 4 votes vote down vote up
public Connection(HttpClient client, String url) {
  Require.nonNull("HTTP client", client);
  Require.nonNull("URL to connect to", url);

  socket = client.openSocket(new HttpRequest(GET, url), new Listener());
}