Best Selenium code snippet using org.openqa.selenium.internal.Require.state
Source:Connection.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.devtools;18import com.google.common.collect.HashMultimap;19import com.google.common.collect.ImmutableMap;20import com.google.common.collect.Multimap;21import org.openqa.selenium.WebDriverException;22import org.openqa.selenium.devtools.idealized.target.model.SessionID;23import org.openqa.selenium.internal.Either;24import org.openqa.selenium.internal.Require;25import org.openqa.selenium.json.Json;26import org.openqa.selenium.json.JsonInput;27import org.openqa.selenium.json.JsonOutput;28import org.openqa.selenium.remote.http.HttpClient;29import org.openqa.selenium.remote.http.HttpRequest;30import org.openqa.selenium.remote.http.WebSocket;31import java.io.Closeable;32import java.io.StringReader;33import java.time.Duration;34import java.util.LinkedHashMap;35import java.util.Map;36import java.util.concurrent.CompletableFuture;37import java.util.concurrent.ExecutionException;38import java.util.concurrent.Executor;39import java.util.concurrent.Executors;40import java.util.concurrent.TimeoutException;41import java.util.concurrent.atomic.AtomicLong;42import java.util.function.Consumer;43import java.util.logging.Level;44import java.util.logging.Logger;45import static java.util.concurrent.TimeUnit.MILLISECONDS;46import static org.openqa.selenium.internal.Debug.getDebugLogLevel;47import static org.openqa.selenium.json.Json.MAP_TYPE;48import static org.openqa.selenium.remote.http.HttpMethod.GET;49public class Connection implements Closeable {50 private static final Logger LOG = Logger.getLogger(Connection.class.getName());51 private static final Json JSON = new Json();52 private static final Executor EXECUTOR = Executors.newCachedThreadPool(r -> {53 Thread thread = new Thread(r, "CDP Connection");54 thread.setDaemon(true);55 return thread;56 });57 private static final AtomicLong NEXT_ID = new AtomicLong(1L);58 private final WebSocket socket;59 private final Map<Long, Consumer<Either<Throwable, JsonInput>>> methodCallbacks = new LinkedHashMap<>();60 private final Multimap<Event<?>, Consumer<?>> eventCallbacks = HashMultimap.create();61 public Connection(HttpClient client, String url) {62 Require.nonNull("HTTP client", client);63 Require.nonNull("URL to connect to", url);64 socket = client.openSocket(new HttpRequest(GET, url), new Listener());65 }66 private static class NamedConsumer<X> implements Consumer<X> {67 private final String name;68 private final Consumer<X> delegate;69 private NamedConsumer(String name, Consumer<X> delegate) {70 this.name = name;71 this.delegate = delegate;72 }73 public static <X> Consumer<X> of(String name, Consumer<X> delegate) {74 return new NamedConsumer<>(name, delegate);75 }76 @Override77 public void accept(X x) {78 delegate.accept(x);79 }80 @Override81 public String toString() {82 return "Consumer for " + name;83 }84 }85 public <X> CompletableFuture<X> send(SessionID sessionId, Command<X> command) {86 long id = NEXT_ID.getAndIncrement();87 CompletableFuture<X> result = new CompletableFuture<>();88 if (command.getSendsResponse()) {89 methodCallbacks.put(id, NamedConsumer.of(command.getMethod(), inputOrException -> {90 if (inputOrException.isRight()) {91 try {92 X value = command.getMapper().apply(inputOrException.right());93 result.complete(value);94 } catch (Throwable e) {95 LOG.log(Level.WARNING, String.format("Unable to map result for %s", command.getMethod()), e);96 result.completeExceptionally(e);97 }98 } else {99 result.completeExceptionally(inputOrException.left());100 }101 }));102 }103 ImmutableMap.Builder<String, Object> serialized = ImmutableMap.builder();104 serialized.put("id", id);105 serialized.put("method", command.getMethod());106 serialized.put("params", command.getParams());107 if (sessionId != null) {108 serialized.put("sessionId", sessionId);109 }110 StringBuilder json = new StringBuilder();111 try (JsonOutput out = JSON.newOutput(json).writeClassName(false)) {112 out.write(serialized.build());113 }114 LOG.log(getDebugLogLevel(), () -> String.format("-> %s", json));115 socket.sendText(json);116 if (!command.getSendsResponse() ) {117 result.complete(null);118 }119 return result;120 }121 public <X> X sendAndWait(SessionID sessionId, Command<X> command, Duration timeout) {122 try {123 CompletableFuture<X> future = send(sessionId, command);124 return future.get(timeout.toMillis(), MILLISECONDS);125 } catch (InterruptedException e) {126 Thread.currentThread().interrupt();127 throw new IllegalStateException("Thread has been interrupted", e);128 } catch (ExecutionException e) {129 Throwable cause = e;130 if (e.getCause() != null) {131 cause = e.getCause();132 }133 throw new DevToolsException(cause);134 } catch (TimeoutException e) {135 throw new org.openqa.selenium.TimeoutException(e);136 }137 }138 public <X> void addListener(Event<X> event, Consumer<X> handler) {139 Require.nonNull("Event to listen for", event);140 Require.nonNull("Handler to call", handler);141 synchronized (eventCallbacks) {142 eventCallbacks.put(event, handler);143 }144 }145 public void clearListeners() {146 synchronized (eventCallbacks) {147 eventCallbacks.clear();148 }149 }150 @Override151 public void close() {152 socket.close();153 }154 private class Listener implements WebSocket.Listener {155 @Override156 public void onText(CharSequence data) {157 EXECUTOR.execute(() -> {158 try {159 handle(data);160 } catch (Throwable t) {161 LOG.log(Level.WARNING, "Unable to process: " + data, t);162 throw new DevToolsException(t);163 }164 });165 }166 }167 private void handle(CharSequence data) {168 // It's kind of gross to decode the data twice, but this lets us get started on something169 // that feels nice to users.170 // TODO: decode once, and once only171 String asString = String.valueOf(data);172 LOG.log(getDebugLogLevel(), () -> String.format("<- %s", asString));173 Map<String, Object> raw = JSON.toType(asString, MAP_TYPE);174 if (raw.get("id") instanceof Number175 && (raw.get("result") != null || raw.get("error") != null)) {176 Consumer<Either<Throwable, JsonInput>> consumer = methodCallbacks.remove(((Number) raw.get("id")).longValue());177 if (consumer == null) {178 return;179 }180 try (StringReader reader = new StringReader(asString);181 JsonInput input = JSON.newInput(reader)) {182 input.beginObject();183 while (input.hasNext()) {184 switch (input.nextName()) {185 case "result":186 consumer.accept(Either.right(input));187 break;188 case "error":189 consumer.accept(Either.left(new WebDriverException(asString)));190 input.skipValue();191 break;192 default:193 input.skipValue();194 }195 }196 input.endObject();197 }198 } else if (raw.get("method") instanceof String && raw.get("params") instanceof Map) {199 LOG.log(200 getDebugLogLevel(),201 String.format("Method %s called with %d callbacks available", raw.get("method"), eventCallbacks.keySet().size()));202 synchronized (eventCallbacks) {203 // TODO: Also only decode once.204 eventCallbacks.keySet().stream()205 .peek(event -> LOG.log(206 getDebugLogLevel(),207 String.format("Matching %s with %s", raw.get("method"), event.getMethod())))208 .filter(event -> raw.get("method").equals(event.getMethod()))209 .forEach(event -> {210 // TODO: This is grossly inefficient. I apologise, and we should fix this.211 try (StringReader reader = new StringReader(asString);212 JsonInput input = JSON.newInput(reader)) {213 Object value = null;214 input.beginObject();215 while (input.hasNext()) {216 switch (input.nextName()) {217 case "params":218 value = event.getMapper().apply(input);219 break;220 default:221 input.skipValue();222 break;223 }224 }225 input.endObject();226 if (value == null) {227 // Do nothing.228 return;229 }230 final Object finalValue = value;231 for (Consumer<?> action : eventCallbacks.get(event)) {232 @SuppressWarnings("unchecked") Consumer<Object> obj = (Consumer<Object>) action;233 LOG.log(234 getDebugLogLevel(),235 String.format("Calling callback for %s using %s being passed %s", event, obj, finalValue));236 obj.accept(finalValue);237 }238 }239 });240 }241 } else {242 LOG.warning("Unhandled type: " + data);243 }244 }245}...
Source:SessionSlot.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.grid.node.local;18import org.openqa.selenium.Capabilities;19import org.openqa.selenium.ImmutableCapabilities;20import org.openqa.selenium.NoSuchSessionException;21import org.openqa.selenium.RetrySessionRequestException;22import org.openqa.selenium.SessionNotCreatedException;23import org.openqa.selenium.WebDriverException;24import org.openqa.selenium.WebDriverInfo;25import org.openqa.selenium.events.EventBus;26import org.openqa.selenium.grid.data.CreateSessionRequest;27import org.openqa.selenium.grid.data.SessionClosedEvent;28import org.openqa.selenium.grid.node.ActiveSession;29import org.openqa.selenium.grid.node.SessionFactory;30import org.openqa.selenium.internal.Either;31import org.openqa.selenium.internal.Require;32import org.openqa.selenium.remote.SessionId;33import org.openqa.selenium.remote.http.HttpHandler;34import org.openqa.selenium.remote.http.HttpRequest;35import org.openqa.selenium.remote.http.HttpResponse;36import java.io.UncheckedIOException;37import java.util.ServiceLoader;38import java.util.UUID;39import java.util.concurrent.atomic.AtomicBoolean;40import java.util.function.Function;41import java.util.function.Predicate;42import java.util.logging.Level;43import java.util.logging.Logger;44import java.util.stream.StreamSupport;45public class SessionSlot implements46 HttpHandler,47 Function<CreateSessionRequest, Either<WebDriverException, ActiveSession>>,48 Predicate<Capabilities> {49 private static final Logger LOG = Logger.getLogger(SessionSlot.class.getName());50 private final EventBus bus;51 private final UUID id;52 private final Capabilities stereotype;53 private final SessionFactory factory;54 private final AtomicBoolean reserved = new AtomicBoolean(false);55 private final boolean supportingCdp;56 private ActiveSession currentSession;57 public SessionSlot(EventBus bus, Capabilities stereotype, SessionFactory factory) {58 this.bus = Require.nonNull("Event bus", bus);59 this.id = UUID.randomUUID();60 this.stereotype = ImmutableCapabilities.copyOf(Require.nonNull("Stereotype", stereotype));61 this.factory = Require.nonNull("Session factory", factory);62 this.supportingCdp = isSlotSupportingCdp(this.stereotype);63 }64 public UUID getId() {65 return id;66 }67 public Capabilities getStereotype() {68 return stereotype;69 }70 public void reserve() {71 if (reserved.getAndSet(true)) {72 throw new IllegalStateException("Attempt to reserve a slot that is already reserved");73 }74 }75 public void release() {76 reserved.set(false);77 }78 public boolean isAvailable() {79 return !reserved.get();80 }81 public ActiveSession getSession() {82 if (isAvailable()) {83 throw new NoSuchSessionException("Session is not running");84 }85 return currentSession;86 }87 public void stop() {88 if (isAvailable()) {89 return;90 }91 SessionId id = currentSession.getId();92 try {93 currentSession.stop();94 } catch (Exception e) {95 LOG.log(Level.WARNING, "Unable to cleanly close session", e);96 }97 currentSession = null;98 release();99 bus.fire(new SessionClosedEvent(id));100 }101 @Override102 public HttpResponse execute(HttpRequest req) throws UncheckedIOException {103 if (currentSession == null) {104 throw new NoSuchSessionException("No session currently running: " + req.getUri());105 }106 return currentSession.execute(req);107 }108 @Override109 public boolean test(Capabilities capabilities) {110 return factory.test(capabilities);111 }112 @Override113 public Either<WebDriverException, ActiveSession> apply(CreateSessionRequest sessionRequest) {114 if (currentSession != null) {115 return Either.left(new RetrySessionRequestException("Slot is busy. Try another slot."));116 }117 if (!test(sessionRequest.getDesiredCapabilities())) {118 return Either.left(new SessionNotCreatedException("New session request capabilities do not "119 + "match the stereotype."));120 }121 try {122 Either<WebDriverException, ActiveSession> possibleSession = factory.apply(sessionRequest);123 if (possibleSession.isRight()) {124 ActiveSession session = possibleSession.right();125 currentSession = session;126 return Either.right(session);127 } else {128 return Either.left(possibleSession.left());129 }130 } catch (Exception e) {131 LOG.log(Level.WARNING, "Unable to create session", e);132 return Either.left(new SessionNotCreatedException(e.getMessage()));133 }134 }135 public boolean isSupportingCdp() {136 return supportingCdp;137 }138 private boolean isSlotSupportingCdp(Capabilities stereotype) {139 return StreamSupport.stream(ServiceLoader.load(WebDriverInfo.class).spliterator(), false)140 .filter(webDriverInfo -> webDriverInfo.isSupporting(stereotype))141 .anyMatch(WebDriverInfo::isSupportingCdp);142 }143}...
Source:PointerInput.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.interactions;18import static org.openqa.selenium.internal.Require.nonNegative;19import org.openqa.selenium.WebElement;20import org.openqa.selenium.WrapsElement;21import org.openqa.selenium.internal.Require;22import java.time.Duration;23import java.util.HashMap;24import java.util.Map;25import java.util.Optional;26import java.util.UUID;27/**28 * Models a <a href="https://www.w3.org/TR/webdriver/#dfn-pointer-input-source">pointer input29 * source</a>.30 */31public class PointerInput implements InputSource, Encodable {32 private final Kind kind;33 private final String name;34 public PointerInput(Kind kind, String name) {35 this.kind = Require.nonNull("Kind of pointer device", kind);36 this.name = Optional.ofNullable(name).orElse(UUID.randomUUID().toString());37 }38 @Override39 public SourceType getInputType() {40 return SourceType.POINTER;41 }42 @Override43 public Map<String, Object> encode() {44 Map<String, Object> toReturn = new HashMap<>();45 toReturn.put("type", getInputType().getType());46 toReturn.put("id", name);47 Map<String, Object> parameters = new HashMap<>();48 parameters.put("pointerType", kind.getWireName());49 toReturn.put("parameters", parameters);50 return toReturn;51 }52 public Interaction createPointerMove(Duration duration, Origin origin, int x, int y) {53 return new Move(this, duration, origin, x, y);54 }55 public Interaction createPointerDown(int button) {56 return new PointerPress(this, PointerPress.Direction.DOWN, button);57 }58 public Interaction createPointerUp(int button) {59 return new PointerPress(this, PointerPress.Direction.UP, button);60 }61 private static class PointerPress extends Interaction implements Encodable {62 private final Direction direction;63 private final int button;64 public PointerPress(InputSource source, Direction direction, int button) {65 super(source);66 if (button < 0) {67 throw new IllegalStateException(68 String.format("Button must be greater than or equal to 0: %d", button));69 }70 this.direction = Require.nonNull("Direction of move", direction);71 this.button = button;72 }73 @Override74 public Map<String, Object> encode() {75 Map<String, Object> toReturn = new HashMap<>();76 toReturn.put("type", direction.getType());77 toReturn.put("button", button);78 return toReturn;79 }80 enum Direction {81 DOWN("pointerDown"),82 UP("pointerUp");83 private final String type;84 Direction(String type) {85 this.type = type;86 }87 public String getType() {88 return type;89 }90 }91 }92 private static class Move extends Interaction implements Encodable {93 private final Origin origin;94 private final int x;95 private final int y;96 private final Duration duration;97 protected Move(98 InputSource source,99 Duration duration,100 Origin origin,101 int x,102 int y) {103 super(source);104 this.origin = Require.nonNull("Origin of move", origin);105 this.x = x;106 this.y = y;107 this.duration = nonNegative(duration);108 }109 @Override110 protected boolean isValidFor(SourceType sourceType) {111 return SourceType.POINTER == sourceType;112 }113 @Override114 public Map<String, Object> encode() {115 Map<String, Object> toReturn = new HashMap<>();116 toReturn.put("type", "pointerMove");117 toReturn.put("duration", duration.toMillis());118 toReturn.put("origin", origin.asArg());119 toReturn.put("x", x);120 toReturn.put("y", y);121 return toReturn;122 }123 }124 public enum Kind {125 MOUSE("mouse"),126 PEN("pen"),127 TOUCH("touch"),;128 private final String wireName;129 Kind(String pointerSubType) {130 this.wireName = pointerSubType;131 }132 public String getWireName() {133 return wireName;134 }135 }136 public enum MouseButton {137 LEFT(0),138 MIDDLE(1),139 RIGHT(2),140 ;141 private final int button;142 MouseButton(int button) {143 this.button = button;144 }145 public int asArg() {146 return button;147 }148 }149 public static final class Origin {150 private final Object originObject;151 public Object asArg() {152 Object arg = originObject;153 while (arg instanceof WrapsElement) {154 arg = ((WrapsElement) arg).getWrappedElement();155 }156 return arg;157 }158 private Origin(Object originObject) {159 this.originObject = originObject;160 }161 public static Origin pointer() {162 return new Origin("pointer");163 }164 public static Origin viewport() {165 return new Origin("viewport");166 }167 public static Origin fromElement(WebElement element) {168 return new Origin(Require.nonNull("Element", element));169 }170 }171}...
Source:Distributor.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.grid.distributor;18import org.openqa.selenium.SessionNotCreatedException;19import org.openqa.selenium.grid.data.CreateSessionResponse;20import org.openqa.selenium.grid.data.DistributorStatus;21import org.openqa.selenium.grid.data.NodeId;22import org.openqa.selenium.grid.data.Session;23import org.openqa.selenium.grid.data.SessionRequest;24import org.openqa.selenium.grid.node.Node;25import org.openqa.selenium.grid.security.RequiresSecretFilter;26import org.openqa.selenium.grid.security.Secret;27import org.openqa.selenium.internal.Either;28import org.openqa.selenium.internal.Require;29import org.openqa.selenium.json.Json;30import org.openqa.selenium.remote.http.HttpClient;31import org.openqa.selenium.remote.http.HttpRequest;32import org.openqa.selenium.remote.http.HttpResponse;33import org.openqa.selenium.remote.http.Routable;34import org.openqa.selenium.remote.http.Route;35import org.openqa.selenium.remote.tracing.SpanDecorator;36import org.openqa.selenium.remote.tracing.Tracer;37import org.openqa.selenium.status.HasReadyState;38import java.io.UncheckedIOException;39import java.util.Map;40import java.util.UUID;41import java.util.function.Predicate;42import static org.openqa.selenium.remote.http.Route.delete;43import static org.openqa.selenium.remote.http.Route.get;44import static org.openqa.selenium.remote.http.Route.post;45/**46 * Responsible for being the central place where the {@link Node}s47 * on which {@link Session}s run48 * are determined.49 * <p>50 * This class responds to the following URLs:51 * <table summary="HTTP commands the Distributor understands">52 * <tr>53 * <th>Verb</th>54 * <th>URL Template</th>55 * <th>Meaning</th>56 * </tr>57 * <tr>58 * <td>POST</td>59 * <td>/session</td>60 * <td>This is exactly the same as the New Session command61 * from the WebDriver spec.</td>62 * </tr>63 * <tr>64 * <td>POST</td>65 * <td>/se/grid/distributor/node</td>66 * <td>Adds a new {@link Node} to this distributor.67 * Please read the javadocs for {@link Node} for68 * how the Node should be serialized.</td>69 * </tr>70 * <tr>71 * <td>DELETE</td>72 * <td>/se/grid/distributor/node/{nodeId}</td>73 * <td>Remove the {@link Node} identified by {@code nodeId}74 * from this distributor. It is expected75 * that any sessions running on the Node are allowed to complete:76 * this simply means that no new77 * sessions will be scheduled on this Node.</td>78 * </tr>79 * </table>80 */81public abstract class Distributor implements HasReadyState, Predicate<HttpRequest>, Routable {82 private final Route routes;83 protected final Tracer tracer;84 protected Distributor(85 Tracer tracer,86 HttpClient.Factory httpClientFactory,87 Secret registrationSecret) {88 this.tracer = Require.nonNull("Tracer", tracer);89 Require.nonNull("HTTP client factory", httpClientFactory);90 Require.nonNull("Registration secret", registrationSecret);91 RequiresSecretFilter requiresSecret = new RequiresSecretFilter(registrationSecret);92 Json json = new Json();93 routes = Route.combine(94 post("/se/grid/distributor/node")95 .to(() ->96 new AddNode(tracer, this, json, httpClientFactory, registrationSecret))97 .with(requiresSecret),98 post("/se/grid/distributor/node/{nodeId}/drain")99 .to((Map<String, String> params) ->100 new DrainNode(this, new NodeId(UUID.fromString(params.get("nodeId")))))101 .with(requiresSecret),102 delete("/se/grid/distributor/node/{nodeId}")103 .to(params ->104 new RemoveNode(this, new NodeId(UUID.fromString(params.get("nodeId")))))105 .with(requiresSecret),106 post("/se/grid/distributor/session")107 .to(() -> new CreateSession(this))108 .with(requiresSecret),109 get("/se/grid/distributor/status")110 .to(() -> new GetDistributorStatus(this))111 .with(new SpanDecorator(tracer, req -> "distributor.status")));112 }113 public abstract Either<SessionNotCreatedException, CreateSessionResponse> newSession(SessionRequest request)114 throws SessionNotCreatedException;115 public abstract Distributor add(Node node);116 public abstract boolean drain(NodeId nodeId);117 public abstract void remove(NodeId nodeId);118 public abstract DistributorStatus getStatus();119 @Override120 public boolean test(HttpRequest httpRequest) {121 return matches(httpRequest);122 }123 @Override124 public boolean matches(HttpRequest req) {125 return routes.matches(req);126 }127 @Override128 public HttpResponse execute(HttpRequest req) throws UncheckedIOException {129 return routes.execute(req);130 }131}...
Source:NewSessionQueue.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.grid.sessionqueue;18import org.openqa.selenium.Capabilities;19import org.openqa.selenium.SessionNotCreatedException;20import org.openqa.selenium.grid.data.CreateSessionResponse;21import org.openqa.selenium.grid.data.RequestId;22import org.openqa.selenium.grid.data.SessionRequest;23import org.openqa.selenium.grid.data.SessionRequestCapability;24import org.openqa.selenium.grid.security.RequiresSecretFilter;25import org.openqa.selenium.grid.security.Secret;26import org.openqa.selenium.internal.Either;27import org.openqa.selenium.internal.Require;28import org.openqa.selenium.remote.http.HttpRequest;29import org.openqa.selenium.remote.http.HttpResponse;30import org.openqa.selenium.remote.http.Routable;31import org.openqa.selenium.remote.http.Route;32import org.openqa.selenium.remote.tracing.Tracer;33import org.openqa.selenium.status.HasReadyState;34import java.time.Instant;35import java.util.List;36import java.util.Map;37import java.util.Optional;38import java.util.Set;39import java.util.UUID;40import static org.openqa.selenium.remote.http.Route.combine;41import static org.openqa.selenium.remote.http.Route.delete;42import static org.openqa.selenium.remote.http.Route.get;43import static org.openqa.selenium.remote.http.Route.post;44public abstract class NewSessionQueue implements HasReadyState, Routable {45 protected final Tracer tracer;46 private final Route routes;47 protected NewSessionQueue(Tracer tracer, Secret registrationSecret) {48 this.tracer = Require.nonNull("Tracer", tracer);49 Require.nonNull("Registration secret", registrationSecret);50 RequiresSecretFilter requiresSecret = new RequiresSecretFilter(registrationSecret);51 routes = combine(52 post("/session")53 .to(() -> req -> {54 SessionRequest sessionRequest = new SessionRequest(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 }103}...
Source:SeleniumCdpConnection.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.devtools;18import org.openqa.selenium.Capabilities;19import org.openqa.selenium.HasCapabilities;20import org.openqa.selenium.WebDriver;21import org.openqa.selenium.internal.Require;22import org.openqa.selenium.remote.http.ClientConfig;23import org.openqa.selenium.remote.http.HttpClient;24import java.net.URI;25import java.net.URISyntaxException;26import java.util.Optional;27public class SeleniumCdpConnection extends Connection {28 private SeleniumCdpConnection(HttpClient client, String url) {29 super(client, url);30 }31 public static Optional<Connection> create(WebDriver driver) {32 if (!(driver instanceof HasCapabilities)) {33 throw new IllegalStateException("Given webdriver instance must have capabilities");34 }35 return create(((HasCapabilities) driver).getCapabilities());36 }37 public static Optional<Connection> create(Capabilities capabilities) {38 Require.nonNull("Capabilities", capabilities);39 return create(HttpClient.Factory.createDefault(), capabilities);40 }41 public static Optional<Connection> create(HttpClient.Factory clientFactory, Capabilities capabilities) {42 Require.nonNull("HTTP client factory", clientFactory);43 Require.nonNull("Capabilities", capabilities);44 return getCdpUri(capabilities).map(uri -> new SeleniumCdpConnection(45 clientFactory.createClient(ClientConfig.defaultConfig().baseUri(uri)),46 uri.toString()));47 }48 public static Optional<URI> getCdpUri(Capabilities capabilities) {49 Object cdp = capabilities.getCapability("se:cdp");50 if (!(cdp instanceof String)) {51 return Optional.empty();52 }53 try {54 return Optional.of(new URI((String) cdp));55 } catch (URISyntaxException e) {56 return Optional.empty();57 }58 }59}...
Source:WebDriverHandler.java
...56 WebDriver toReturn = getDriver();57 while (toReturn instanceof WrapsDriver) {58 toReturn = ((WrapsDriver) toReturn).getWrappedDriver();59 }60 return Require.state("Unwrapped driver", toReturn).nonNull();61 }62}...
Source:RemoteRotatable.java
1// Licensed to the Software Freedom Conservancy (SFC) under one2// or more contributor license agreements. See the NOTICE file3// distributed with this work for additional information4// regarding copyright ownership. The SFC licenses this file5// to you under the Apache License, Version 2.0 (the6// "License"); you may not use this file except in compliance7// with the License. You may obtain a copy of the License at8//9// http://www.apache.org/licenses/LICENSE-2.010//11// Unless required by applicable law or agreed to in writing,12// software distributed under the License is distributed on an13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.remote;18import com.google.common.collect.ImmutableMap;19import org.openqa.selenium.DeviceRotation;20import org.openqa.selenium.Rotatable;21import org.openqa.selenium.ScreenOrientation;22import org.openqa.selenium.internal.Require;23import java.util.Map;24class RemoteRotatable implements Rotatable {25 private final ExecuteMethod executeMethod;26 public RemoteRotatable(ExecuteMethod executeMethod) {27 this.executeMethod = Require.nonNull("Execute method", executeMethod);28 }29 @Override30 public void rotate(ScreenOrientation orientation) {31 executeMethod.execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of("orientation", orientation));32 }33 @Override34 public ScreenOrientation getOrientation() {35 return ScreenOrientation.valueOf(36 (String) executeMethod.execute(DriverCommand.GET_SCREEN_ORIENTATION, null));37 }38 @Override39 public void rotate(DeviceRotation rotation) {40 executeMethod.execute(DriverCommand.SET_SCREEN_ORIENTATION, rotation.parameters());41 }42 @Override43 public DeviceRotation rotation() {44 Object result = executeMethod.execute(DriverCommand.GET_SCREEN_ROTATION, null);45 if (!(result instanceof Map)) {46 throw new IllegalStateException("Unexpected return value: " + result);47 }48 @SuppressWarnings("unchecked") Map<String, Number> raw = (Map<String, Number>) result;49 return new DeviceRotation(raw);50 }51}...
state
Using AI Code Generation
1if (Require.state(driver.findElement(By.id("elementId")), "Element not found")) {2 System.out.println("Element found");3} else {4 System.out.println("Element not found");5}6if (Require.state(driver.findElement(By.id("elementId")), "Element not found")) {7 System.out.println("Element found");8} else {9 System.out.println("Element not found");10}11if (Require.state(driver.findElement(By.id("elementId")), "Element not found")) {12 System.out.println("Element found");13} else {14 System.out.println("Element not found");15}16if (Require.state(driver.findElement(By.id("elementId")), "Element not found")) {17 System.out.println("Element found");18} else {19 System.out.println("Element not found");20}21if (Require.state(driver.findElement(By.id("elementId")), "Element not found")) {22 System.out.println("Element found");23} else {24 System.out.println("Element not found");25}26if (Require.state(driver.findElement(By.id("elementId")), "Element not found")) {27 System.out.println("Element found");28} else {29 System.out.println("Element not found");30}31if (Require.state(driver.findElement(By.id("elementId")), "Element not found")) {32 System.out.println("Element found");33} else {34 System.out.println("Element not found");35}36if (Require.state(driver.findElement(By.id("elementId")), "Element not found")) {37 System.out.println("Element found");38} else {
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.
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.
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.
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.
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.
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.
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.
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.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!