How to use addToQueue method of org.openqa.selenium.grid.sessionqueue.NewSessionQueue class

Best Selenium code snippet using org.openqa.selenium.grid.sessionqueue.NewSessionQueue.addToQueue

Source:NewSessionQueuerTest.java Github

copy

Full Screen

...132 new NewSessionRejectedEvent(133 new NewSessionErrorResponse(new RequestId(UUID.randomUUID()), "Error")));134 }135 }));136 HttpResponse httpResponse = local.addToQueue(request);137 assertThat(isPresent.get()).isTrue();138 assertEquals(httpResponse.getStatus(), HTTP_OK);139 }140 @Test141 public void shouldBeAbleToAddToQueueAndGetErrorResponse() {142 AtomicBoolean isPresent = new AtomicBoolean(false);143 bus.addListener(NewSessionRequestEvent.listener(reqId -> {144 Optional<HttpRequest> sessionRequest = this.local.remove();145 isPresent.set(sessionRequest.isPresent());146 bus.fire(147 new NewSessionRejectedEvent(148 new NewSessionErrorResponse(reqId, "Error")));149 }));150 HttpResponse httpResponse = local.addToQueue(request);151 assertThat(isPresent.get()).isTrue();152 assertEquals(httpResponse.getStatus(), HTTP_INTERNAL_ERROR);153 }154 @Test155 public void shouldBeAbleToAddToQueueRemotelyAndGetErrorResponse() {156 AtomicBoolean isPresent = new AtomicBoolean(false);157 bus.addListener(NewSessionRequestEvent.listener(reqId -> {158 Optional<HttpRequest> sessionRequest = this.remote.remove();159 isPresent.set(sessionRequest.isPresent());160 bus.fire(161 new NewSessionRejectedEvent(162 new NewSessionErrorResponse(reqId, "Could not poll the queue")));163 }));164 HttpResponse httpResponse = remote.addToQueue(request);165 assertThat(isPresent.get()).isTrue();166 assertEquals(httpResponse.getStatus(), HTTP_INTERNAL_ERROR);167 }168 @Test169 public void shouldBeAbleToRemoveFromQueue() {170 Optional<HttpRequest> httpRequest = local.remove();171 assertFalse(httpRequest.isPresent());172 }173 @Test174 public void shouldBeClearQueue() {175 RequestId requestId = new RequestId(UUID.randomUUID());176 sessionQueue.offerLast(request, requestId);177 int count = local.clearQueue();178 assertEquals(count, 1);179 assertFalse(local.remove().isPresent());180 }181 @Test182 public void shouldBeClearQueueRemotely() {183 RequestId requestId = new RequestId(UUID.randomUUID());184 sessionQueue.offerLast(request, requestId);185 int count = remote.clearQueue();186 assertEquals(count, 1);187 assertFalse(remote.remove().isPresent());188 }189 @Test190 public void shouldBeClearQueueAndFireRejectedEvent() {191 AtomicBoolean result = new AtomicBoolean(false);192 RequestId requestId = new RequestId(UUID.randomUUID());193 bus.addListener(NewSessionRejectedEvent.listener(response ->194 result.set(response.getRequestId()195 .equals(requestId))));196 sessionQueue.offerLast(request, requestId);197 int count = remote.clearQueue();198 assertThat(result.get()).isTrue();199 assertEquals(count, 1);200 assertFalse(remote.remove().isPresent());201 }202 @Test203 public void shouldBeAbleToRemoveFromQueueRemotely() {204 Optional<HttpRequest> httpRequest = remote.remove();205 assertFalse(httpRequest.isPresent());206 }207 @Test208 public void shouldBeAbleToAddAgainToQueue() {209 boolean added = local.retryAddToQueue(request, new RequestId(UUID.randomUUID()));210 assertTrue(added);211 }212 @Test213 public void shouldBeAbleToAddAgainToQueueRemotely() {214 HttpRequest request = createRequest(payload, POST, "/se/grid/newsessionqueuer/session");215 boolean added = remote.retryAddToQueue(request, new RequestId(UUID.randomUUID()));216 assertTrue(added);217 }218 @Test219 public void shouldBeAbleToRetryRequest() {220 AtomicBoolean isPresent = new AtomicBoolean(false);221 AtomicBoolean retrySuccess = new AtomicBoolean(false);222 bus.addListener(NewSessionRequestEvent.listener(reqId -> {223 // Keep a count of event fired224 count++;225 Optional<HttpRequest> sessionRequest = this.remote.remove();226 isPresent.set(sessionRequest.isPresent());227 if (count == 1) {228 retrySuccess.set(remote.retryAddToQueue(sessionRequest.get(), reqId));229 }230 // Only if it was retried after an interval, the count is 2231 if (count == 2) {232 ImmutableCapabilities capabilities = new ImmutableCapabilities("browserName", "chrome");233 try {234 SessionId sessionId = new SessionId("123");235 Session session =236 new Session(237 sessionId,238 new URI("http://example.com"),239 caps,240 capabilities,241 Instant.now());242 CreateSessionResponse sessionResponse = new CreateSessionResponse(243 session,244 JSON.toJson(245 ImmutableMap.of(246 "value", ImmutableMap.of(247 "sessionId", sessionId,248 "capabilities", capabilities)))249 .getBytes(UTF_8));250 NewSessionResponse newSessionResponse =251 new NewSessionResponse(reqId, sessionResponse.getSession(),252 sessionResponse.getDownstreamEncodedResponse());253 bus.fire(new NewSessionResponseEvent(newSessionResponse));254 } catch (URISyntaxException e) {255 bus.fire(256 new NewSessionRejectedEvent(257 new NewSessionErrorResponse(new RequestId(UUID.randomUUID()), "Error")));258 }259 }260 }));261 HttpResponse httpResponse = remote.addToQueue(request);262 assertThat(isPresent.get()).isTrue();263 assertThat(retrySuccess.get()).isTrue();264 assertEquals(httpResponse.getStatus(), HTTP_OK);265 }266 @Test267 public void shouldBeAbleToHandleMultipleSessionRequestsAtTheSameTime() {268 bus.addListener(NewSessionRequestEvent.listener(reqId -> {269 Optional<HttpRequest> sessionRequest = this.local.remove();270 ImmutableCapabilities capabilities = new ImmutableCapabilities("browserName", "chrome");271 try {272 SessionId sessionId = new SessionId(UUID.randomUUID());273 Session session =274 new Session(275 sessionId,276 new URI("http://example.com"),277 caps,278 capabilities,279 Instant.now());280 CreateSessionResponse sessionResponse = new CreateSessionResponse(281 session,282 JSON.toJson(283 ImmutableMap.of(284 "value", ImmutableMap.of(285 "sessionId", sessionId,286 "capabilities", capabilities)))287 .getBytes(UTF_8));288 NewSessionResponse newSessionResponse =289 new NewSessionResponse(reqId, sessionResponse.getSession(),290 sessionResponse.getDownstreamEncodedResponse());291 bus.fire(new NewSessionResponseEvent(newSessionResponse));292 } catch (URISyntaxException e) {293 bus.fire(294 new NewSessionRejectedEvent(295 new NewSessionErrorResponse(new RequestId(UUID.randomUUID()), "Error")));296 }297 }));298 ExecutorService executor = Executors.newFixedThreadPool(2);299 Callable<HttpResponse> callable = () -> remote.addToQueue(request);300 Future<HttpResponse> firstRequest = executor.submit(callable);301 Future<HttpResponse> secondRequest = executor.submit(callable);302 try {303 HttpResponse firstResponse = firstRequest.get(30, TimeUnit.SECONDS);304 HttpResponse secondResponse = secondRequest.get(30, TimeUnit.SECONDS);305 String firstResponseContents = Contents.string(firstResponse);306 String secondResponseContents = Contents.string(secondResponse);307 assertEquals(firstResponse.getStatus(), HTTP_OK);308 assertEquals(secondResponse.getStatus(), HTTP_OK);309 assertNotEquals(firstResponseContents, secondResponseContents);310 } catch (InterruptedException | ExecutionException | TimeoutException e) {311 fail("Could not create session");312 }313 executor.shutdown();314 }315 @Test316 public void shouldBeAbleToTimeoutARequestOnRetry() {317 Tracer tracer = DefaultTestTracer.createTracer();318 LocalNewSessionQueue sessionQueue = new LocalNewSessionQueue(319 tracer,320 bus,321 Duration.ofSeconds(4),322 Duration.ofSeconds(2));323 local = new LocalNewSessionQueuer(tracer, bus, sessionQueue);324 HttpClient client = new PassthroughHttpClient(local);325 remote = new RemoteNewSessionQueuer(tracer, client);326 HttpRequest request = createRequest(payload, POST, "/session");327 request.addHeader(SESSIONREQUEST_TIMESTAMP_HEADER,328 Long.toString(1539091064));329 AtomicInteger count = new AtomicInteger();330 bus.addListener(NewSessionRequestEvent.listener(reqId -> {331 // Add to front of queue, when retry is triggered it will check if request timed out332 count.incrementAndGet();333 remote.retryAddToQueue(request, reqId);334 }));335 HttpResponse httpResponse = remote.addToQueue(request);336 assertEquals(count.get(),1);337 assertEquals(httpResponse.getStatus(), HTTP_INTERNAL_ERROR);338 }339 @Test340 public void shouldBeAbleToTimeoutARequestOnPoll() {341 Tracer tracer = DefaultTestTracer.createTracer();342 LocalNewSessionQueue sessionQueue = new LocalNewSessionQueue(343 tracer,344 bus,345 Duration.ofSeconds(4),346 Duration.ofSeconds(0));347 local = new LocalNewSessionQueuer(tracer, bus, sessionQueue);348 HttpClient client = new PassthroughHttpClient(local);349 remote = new RemoteNewSessionQueuer(tracer, client);350 AtomicBoolean isPresent = new AtomicBoolean();351 bus.addListener(NewSessionRequestEvent.listener(reqId -> {352 Optional<HttpRequest> request = remote.remove();353 isPresent.set(request.isPresent());354 bus.fire(355 new NewSessionRejectedEvent(356 new NewSessionErrorResponse(reqId, "Error")));357 }));358 HttpResponse httpResponse = remote.addToQueue(request);359 assertEquals(httpResponse.getStatus(), HTTP_INTERNAL_ERROR);360 assertThat(isPresent.get()).isFalse();361 }362 @Test363 public void shouldBeAbleToClearQueueAndRejectMultipleRequests() {364 ExecutorService executor = Executors.newFixedThreadPool(2);365 Callable<HttpResponse> callable = () -> remote.addToQueue(request);366 Future<HttpResponse> firstRequest = executor.submit(callable);367 Future<HttpResponse> secondRequest = executor.submit(callable);368 int count = 0;369 while (count < 2) {370 count += remote.clearQueue();371 }372 try {373 HttpResponse firstResponse = firstRequest.get(30, TimeUnit.SECONDS);374 HttpResponse secondResponse = secondRequest.get(30, TimeUnit.SECONDS);375 assertEquals(firstResponse.getStatus(), HTTP_INTERNAL_ERROR);376 assertEquals(secondResponse.getStatus(), HTTP_INTERNAL_ERROR);377 } catch (InterruptedException | ExecutionException | TimeoutException e) {378 fail("Could not create session");379 }...

Full Screen

Full Screen

Source:RemoteNewSessionQueue.java Github

copy

Full Screen

...77 throw new UncheckedIOException(e);78 }79 }80 @Override81 public HttpResponse addToQueue(SessionRequest request) {82 HttpRequest upstream = new HttpRequest(POST, "/se/grid/newsessionqueue/session");83 HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);84 upstream.setContent(Contents.asJson(request));85 return client.with(addSecret).execute(upstream);86 }87 @Override88 public boolean retryAddToQueue(SessionRequest request) {89 Require.nonNull("Session request", request);90 HttpRequest upstream =91 new HttpRequest(POST, String.format("/se/grid/newsessionqueue/session/%s/retry", request.getRequestId()));92 HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);93 upstream.setContent(Contents.asJson(request));94 HttpResponse response = client.with(addSecret).execute(upstream);95 return Values.get(response, Boolean.class);...

Full Screen

Full Screen

Source:RemoteNewSessionQueuer.java Github

copy

Full Screen

...69 throw new UncheckedIOException(e);70 }71 }72 @Override73 public HttpResponse addToQueue(HttpRequest request) {74 HttpRequest upstream = new HttpRequest(POST, "/se/grid/newsessionqueuer/session");75 HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);76 upstream.setContent(request.getContent());77 return client.execute(upstream);78 }79 @Override80 public boolean retryAddToQueue(HttpRequest request, RequestId reqId) {81 HttpRequest upstream =82 new HttpRequest(POST, "/se/grid/newsessionqueuer/session/retry/" + reqId.toString());83 HttpTracing.inject(tracer, tracer.getCurrentContext(), upstream);84 upstream.setContent(request.getContent());85 upstream.setHeader(timestampHeader, request.getHeader(timestampHeader));86 upstream.setHeader(reqIdHeader, reqId.toString());87 HttpResponse response = client.with(addSecret).execute(upstream);...

Full Screen

Full Screen

Source:NewSessionQueue.java Github

copy

Full Screen

...55 new RequestId(UUID.randomUUID()),56 req,57 Instant.now()58 );59 return addToQueue(sessionRequest);60 }),61 post("/se/grid/newsessionqueue/session")62 .to(() -> new AddToSessionQueue(tracer, this))63 .with(requiresSecret),64 post("/se/grid/newsessionqueue/session/{requestId}/retry")65 .to(params -> new AddBackToSessionQueue(tracer, this, requestIdFrom(params)))66 .with(requiresSecret),67 post("/se/grid/newsessionqueue/session/{requestId}/failure")68 .to(params -> new SessionNotCreated(tracer, this, requestIdFrom(params)))69 .with(requiresSecret),70 post("/se/grid/newsessionqueue/session/{requestId}/success")71 .to(params -> new SessionCreated(tracer, this, requestIdFrom(params)))72 .with(requiresSecret),73 post("/se/grid/newsessionqueue/session/{requestId}")74 .to(params -> new RemoveFromSessionQueue(tracer, this, requestIdFrom(params)))75 .with(requiresSecret),76 post("/se/grid/newsessionqueue/session/next")77 .to(() -> new GetNextMatchingRequest(tracer, this))78 .with(requiresSecret),79 get("/se/grid/newsessionqueue/queue")80 .to(() -> new GetSessionQueue(tracer, this)),81 delete("/se/grid/newsessionqueue/queue")82 .to(() -> new ClearSessionQueue(tracer, this))83 .with(requiresSecret));84 }85 private RequestId requestIdFrom(Map<String, String> params) {86 return new RequestId(UUID.fromString(params.get("requestId")));87 }88 public abstract HttpResponse addToQueue(SessionRequest request);89 public abstract boolean retryAddToQueue(SessionRequest request);90 public abstract Optional<SessionRequest> remove(RequestId reqId);91 public abstract Optional<SessionRequest> getNextAvailable(Set<Capabilities> stereotypes);92 public abstract void complete(RequestId reqId, Either<SessionNotCreatedException, CreateSessionResponse> result);93 public abstract int clearQueue();94 public abstract List<SessionRequestCapability> getQueueContents();95 @Override96 public boolean matches(HttpRequest req) {97 return routes.matches(req);98 }99 @Override100 public HttpResponse execute(HttpRequest req) {101 return routes.execute(req);102 }...

Full Screen

Full Screen

Source:LocalNewSessionQueuer.java Github

copy

Full Screen

...48 NewSessionQueue sessionRequests = new LocalNewSessionQueue(tracer, bus, retryInterval);49 return new LocalNewSessionQueuer(tracer, bus, sessionRequests);50 }51 @Override52 public HttpResponse addToQueue(HttpRequest request) {53 validateSessionRequest(request);54 GetNewSessionResponse55 getNewSessionResponse = new GetNewSessionResponse(tracer, bus, sessionRequests);56 return getNewSessionResponse.add(request);57 }58 @Override59 public boolean retryAddToQueue(HttpRequest request, RequestId reqId) {60 return sessionRequests.offerFirst(request, reqId);61 }62 @Override63 public Optional<HttpRequest> remove() {64 return sessionRequests.poll();65 }66 @Override...

Full Screen

Full Screen

Source:AddToSessionQueue.java Github

copy

Full Screen

...36 @Override37 public HttpResponse execute(HttpRequest req) {38 try (Span span = newSpanAsChildOf(tracer, req, "sessionqueue.add")) {39 HTTP_REQUEST.accept(span, req);40 HttpResponse response = newSessionQueue.addToQueue(Contents.fromJson(req, SessionRequest.class));41 HTTP_RESPONSE.accept(span, response);42 return response;43 }44 }45}...

Full Screen

Full Screen

addToQueue

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;2import org.openqa.selenium.remote.NewSessionPayload;3import org.openqa.selenium.remote.http.HttpRequest;4import org.openqa.selenium.grid.data.SessionRequest;5import org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue;6import org.openqa.selenium.grid.sessionqueue.config.SessionQueueOptions;7import org.openqa.selenium.grid.config.Config;8import org.openqa.selenium.grid.config.MapConfig;9import java.util.Map;10import java.util.HashMap;11import java.util.UUID;12public class AddToQueueExample {13 public static void main(String[] args) {14 Map<String, String> config = new HashMap<>();15 config.put("session-queue", "local");16 config.put("session-queue.local", "true");17 Config config = new MapConfig(config);18 SessionQueueOptions sessionQueueOptions = new SessionQueueOptions(config);19 NewSessionQueue queue = new LocalNewSessionQueue(sessionQueueOptions);20 HttpRequest request = new HttpRequest("POST", "/session");21 NewSessionPayload payload = new NewSessionPayload(UUID.randomUUID(), request);22 SessionRequest sessionRequest = new SessionRequest(UUID.randomUUID(), payload);23 queue.addToQueue(sessionRequest);24 }25}

Full Screen

Full Screen

addToQueue

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;2import org.openqa.selenium.remote.NewSessionPayload;3NewSessionPayload payload = NewSessionPayload.create("browserName", "chrome");4NewSessionQueue.addToQueue(payload);5return NewSessionQueue.getQueueLength();6NewSessionQueue.addToQueue(NewSessionPayload) method7NewSessionQueue.getQueueLength() method8import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;9import org.openqa.selenium.remote.NewSessionPayload;10NewSessionPayload payload = NewSessionPayload.create("browserName", "chrome");11NewSessionQueue.addToQueue(payload);12NewSessionQueue.removeFromQueue(payload);13return NewSessionQueue.getQueueLength();14NewSessionQueue.removeFromQueue(NewSessionPayload) method15NewSessionQueue.getQueueLength() method16import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;17import org.openqa.selenium.remote.NewSessionPayload;18NewSessionPayload payload = NewSessionPayload.create("browserName", "chrome");19NewSessionQueue.addToQueue(payload);20NewSessionQueue.clearQueue();21return NewSessionQueue.getQueueLength();22NewSessionQueue.clearQueue() method23NewSessionQueue.getQueueLength() method24import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;25import org.openqa.selenium.remote.NewSessionPayload;26NewSessionPayload payload = NewSessionPayload.create("browserName", "chrome");27NewSessionQueue.addToQueue(payload);28return NewSessionQueue.isEmpty();

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful