Best Selenium code snippet using org.openqa.selenium.internal.Require.nonNull
Source:Connection.java
...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 {...
Source:PointerInput.java
...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:SessionNotCreated.java
...33 private final Tracer tracer;34 private final NewSessionQueue queue;35 private final RequestId requestId;36 public SessionNotCreated(Tracer tracer, NewSessionQueue queue, RequestId requestId) {37 this.tracer = Require.nonNull("Tracer", tracer);38 this.queue = Require.nonNull("New Session Queue", queue);39 this.requestId = Require.nonNull("Request ID", requestId);40 }41 @Override42 public HttpResponse execute(HttpRequest req) throws UncheckedIOException {43 try (Span span = newSpanAsChildOf(tracer, req, "sessionqueue.created_bad")) {44 HTTP_REQUEST.accept(span, req);45 String message = Contents.fromJson(req, String.class);46 SessionNotCreatedException exception = new SessionNotCreatedException(message);47 queue.complete(requestId, Either.left(exception));48 HttpResponse res = new HttpResponse();49 HTTP_RESPONSE.accept(span, res);50 return res;51 }52 }53}...
Source:NewNodeSession.java
...32class NewNodeSession implements HttpHandler {33 private final Node node;34 private final Json json;35 NewNodeSession(Node node, Json json) {36 this.node = Require.nonNull("Node", node);37 this.json = Require.nonNull("Json converter", json);38 }39 @Override40 public HttpResponse execute(HttpRequest req) throws UncheckedIOException {41 CreateSessionRequest incoming = json.toType(string(req), CreateSessionRequest.class);42 Either<WebDriverException, CreateSessionResponse> result =43 node.newSession(incoming);44 HashMap<String, Object> value = new HashMap<>();45 HashMap<String, Object> response = new HashMap<>();46 if (result.isRight()) {47 response.put("sessionResponse", result.right());48 } else {49 WebDriverException exception = result.left();50 response.put("exception", ImmutableMap.of(51 "error", exception.getClass(),...
Source:SessionCreated.java
...33 private final Tracer tracer;34 private final NewSessionQueue queue;35 private final RequestId requestId;36 public SessionCreated(Tracer tracer, NewSessionQueue queue, RequestId requestId) {37 this.tracer = Require.nonNull("Tracer", tracer);38 this.queue = Require.nonNull("New Session Queue", queue);39 this.requestId = Require.nonNull("Request ID", requestId);40 }41 @Override42 public HttpResponse execute(HttpRequest req) throws UncheckedIOException {43 try (Span span = newSpanAsChildOf(tracer, req, "sessionqueue.created_ok")) {44 HTTP_REQUEST.accept(span, req);45 CreateSessionResponse response = Contents.fromJson(req, CreateSessionResponse.class);46 queue.complete(requestId, Either.right(response));47 HttpResponse res = new HttpResponse();48 HTTP_RESPONSE.accept(span, res);49 return res;50 }51 }52}...
Source:CreateSession.java
...30import static java.util.Collections.singletonMap;31class CreateSession implements HttpHandler {32 private final Distributor distributor;33 CreateSession(Distributor distributor) {34 this.distributor = Require.nonNull("Distributor", distributor);35 }36 @Override37 public HttpResponse execute(HttpRequest req) throws UncheckedIOException {38 SessionRequest request = Contents.fromJson(req, SessionRequest.class);39 Either<SessionNotCreatedException, CreateSessionResponse> result = distributor.newSession(request);40 HttpResponse res = new HttpResponse();41 Map<String, Object> value;42 if (result.isLeft()) {43 res.setStatus(HTTP_INTERNAL_ERROR);44 value = singletonMap("value", result.left());45 } else {46 value = singletonMap("value", result.right());47 }48 res.setContent(Contents.asJson(value));...
Source:NullSpan.java
...22import java.util.Map;23public class NullSpan extends NullContext implements Span {24 @Override25 public Span setName(String name) {26 Require.nonNull("Name", name);27 return this;28 }29 @Override30 public Span setAttribute(String key, boolean value) {31 Require.nonNull("Key", key);32 return this;33 }34 @Override35 public Span setAttribute(String key, Number value) {36 Require.nonNull("Key", key);37 Require.nonNull("Value", value);38 return this;39 }40 @Override41 public Span setAttribute(String key, String value) {42 Require.nonNull("Key", key);43 Require.nonNull("Value", value);44 return this;45 }46 @Override47 public Span addEvent(String name) {48 Require.nonNull("Name", name);49 return this;50 }51 @Override52 public Span addEvent(String name, Map<String, EventAttributeValue> attributeMap) {53 Require.nonNull("Name", name);54 Require.nonNull("Event Attribute Map", attributeMap);55 return this;56 }57 @Override58 public Span setStatus(Status status) {59 Require.nonNull("Status", status);60 return this;61 }62 @Override63 public void close() {64 // no-op65 }66}...
Source:TraceSessionRequest.java
...6 private TraceSessionRequest() {7 // Utility methods8 }9 public static TraceContext extract(Tracer tracer, SessionRequest sessionRequest) {10 Require.nonNull("Tracer", tracer);11 Require.nonNull("Session request", sessionRequest);12 return tracer.getPropagator()13 .extractContext(14 tracer.getCurrentContext(),15 sessionRequest,16 SessionRequest::getTraceHeader);17 }18}...
nonNull
Using AI Code Generation
1requireNonNull(driver, "driver must not be null");2requireNonNull(element, "element must not be null");3requireNonNull(by, "by must not be null");4requireNonNull(timeout, "timeout must not be null");5Objects.requireNonNull(driver, "driver must not be null");6Objects.requireNonNull(element, "element must not be null");7Objects.requireNonNull(by, "by must not be null");8Objects.requireNonNull(timeout, "timeout must not be null");9requireNonNull(driver, "driver must not be null");10requireNonNull(element, "element must not be null");11requireNonNull(by, "by must not be null");12requireNonNull(timeout, "timeout must not be null");
nonNull
Using AI Code Generation
1public class NonNullExample {2 public static void main(String[] args) {3 String str = null;4 String str1 = "Selenium";5 System.out.println(Require.nonNull(str, "String is null"));6 System.out.println(Require.nonNull(str1, "String is not null"));7 }8}9 at org.openqa.selenium.internal.Require.nonNull(Require.java:42)10 at NonNullExample.main(NonNullExample.java:15)
nonNull
Using AI Code Generation
1public void clickElement(WebElement element) {2 Require.nonNull("Element", element).click();3}4Objects.requireNonNull(element).click();5Objects.requireNonNull(element).click();6Objects.requireNonNull(element).click();7Objects.requireNonNull(element).click();8Objects.requireNonNull(element).click();9Objects.requireNonNull(element).click();10Objects.requireNonNull(element).click();11Objects.requireNonNull(element).click();12Objects.requireNonNull(element).click();
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!!