Best Selenium code snippet using org.openqa.selenium.grid.data.NodeStatusEvent
Source:AddingNodesTest.java
...30import org.openqa.selenium.grid.data.CreateSessionResponse;31import org.openqa.selenium.grid.data.DefaultSlotMatcher;32import org.openqa.selenium.grid.data.NodeId;33import org.openqa.selenium.grid.data.NodeStatus;34import org.openqa.selenium.grid.data.NodeStatusEvent;35import org.openqa.selenium.grid.data.Session;36import org.openqa.selenium.grid.data.SessionClosedEvent;37import org.openqa.selenium.grid.data.Slot;38import org.openqa.selenium.grid.data.SlotId;39import org.openqa.selenium.grid.distributor.local.LocalDistributor;40import org.openqa.selenium.grid.distributor.remote.RemoteDistributor;41import org.openqa.selenium.grid.distributor.selector.DefaultSlotSelector;42import org.openqa.selenium.grid.node.CapabilityResponseEncoder;43import org.openqa.selenium.grid.node.HealthCheck;44import org.openqa.selenium.grid.node.Node;45import org.openqa.selenium.grid.node.local.LocalNode;46import org.openqa.selenium.grid.security.Secret;47import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;48import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;49import org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue;50import org.openqa.selenium.grid.testing.TestSessionFactory;51import org.openqa.selenium.grid.web.CombinedHandler;52import org.openqa.selenium.grid.web.RoutableHttpClientFactory;53import org.openqa.selenium.internal.Either;54import org.openqa.selenium.remote.SessionId;55import org.openqa.selenium.remote.http.HttpClient;56import org.openqa.selenium.remote.http.HttpRequest;57import org.openqa.selenium.remote.http.HttpResponse;58import org.openqa.selenium.remote.tracing.DefaultTestTracer;59import org.openqa.selenium.remote.tracing.Tracer;60import org.openqa.selenium.support.ui.FluentWait;61import org.openqa.selenium.support.ui.Wait;62import java.net.MalformedURLException;63import java.net.URI;64import java.net.URISyntaxException;65import java.net.URL;66import java.time.Duration;67import java.time.Instant;68import java.util.HashMap;69import java.util.Map;70import java.util.Objects;71import java.util.Optional;72import java.util.Set;73import java.util.UUID;74import java.util.function.Function;75import static com.google.common.collect.Iterables.getOnlyElement;76import static org.junit.Assert.assertEquals;77import static org.openqa.selenium.grid.data.Availability.UP;78import static org.openqa.selenium.remote.Dialect.W3C;79public class AddingNodesTest {80 private static final Capabilities CAPS = new ImmutableCapabilities("cheese", "gouda");81 private static final Secret registrationSecret = new Secret("caerphilly");82 private Distributor distributor;83 private Tracer tracer;84 private EventBus bus;85 private Wait<Object> wait;86 private URL externalUrl;87 private CombinedHandler handler;88 private Capabilities stereotype;89 @Before90 public void setUpDistributor() throws MalformedURLException {91 tracer = DefaultTestTracer.createTracer();92 bus = new GuavaEventBus();93 handler = new CombinedHandler();94 externalUrl = new URL("http://example.com");95 HttpClient.Factory clientFactory = new RoutableHttpClientFactory(96 externalUrl,97 handler,98 HttpClient.Factory.createDefault());99 LocalSessionMap sessions = new LocalSessionMap(tracer, bus);100 NewSessionQueue queue = new LocalNewSessionQueue(101 tracer,102 bus,103 new DefaultSlotMatcher(),104 Duration.ofSeconds(2),105 Duration.ofSeconds(2),106 registrationSecret);107 Distributor local = new LocalDistributor(108 tracer,109 bus,110 clientFactory,111 sessions,112 queue,113 new DefaultSlotSelector(),114 registrationSecret,115 Duration.ofMinutes(5),116 false);117 handler.addHandler(local);118 distributor = new RemoteDistributor(tracer, clientFactory, externalUrl, registrationSecret);119 stereotype = new ImmutableCapabilities("browserName", "gouda");120 wait = new FluentWait<>(121 new Object()).ignoring(Throwable.class).withTimeout(Duration.ofSeconds(2));122 }123 @Test124 public void shouldBeAbleToRegisterALocalNode() throws URISyntaxException {125 URI sessionUri = new URI("http://example:1234");126 Node node = LocalNode127 .builder(tracer, bus, externalUrl.toURI(), externalUrl.toURI(), registrationSecret)128 .add(129 CAPS,130 new TestSessionFactory(131 (id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))132 .build();133 handler.addHandler(node);134 distributor.add(node);135 wait.until(obj -> distributor.getStatus().hasCapacity());136 NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());137 assertEquals(1, getStereotypes(status).get(CAPS).intValue());138 }139 @Test140 public void shouldBeAbleToRegisterACustomNode() throws URISyntaxException {141 URI sessionUri = new URI("http://example:1234");142 Node node = new CustomNode(143 bus,144 new NodeId(UUID.randomUUID()),145 externalUrl.toURI(),146 c -> new Session(147 new SessionId(UUID.randomUUID()), sessionUri, stereotype, c, Instant.now()));148 handler.addHandler(node);149 distributor.add(node);150 wait.until(obj -> distributor.getStatus().hasCapacity());151 NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());152 assertEquals(1, getStereotypes(status).get(CAPS).intValue());153 }154 @Test155 public void shouldBeAbleToRegisterNodesByListeningForEvents() throws URISyntaxException {156 URI sessionUri = new URI("http://example:1234");157 Node node = LocalNode158 .builder(tracer, bus, externalUrl.toURI(), externalUrl.toURI(), registrationSecret)159 .add(160 CAPS,161 new TestSessionFactory(162 (id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))163 .build();164 handler.addHandler(node);165 bus.fire(new NodeStatusEvent(node.getStatus()));166 wait.until(obj -> distributor.getStatus().hasCapacity());167 NodeStatus status = getOnlyElement(distributor.getStatus().getNodes());168 assertEquals(1, getStereotypes(status).get(CAPS).intValue());169 }170 @Test171 public void shouldKeepOnlyOneNodeWhenTwoRegistrationsHaveTheSameUriByListeningForEvents()172 throws URISyntaxException {173 URI sessionUri = new URI("http://example:1234");174 Node firstNode = LocalNode175 .builder(tracer, bus, externalUrl.toURI(), externalUrl.toURI(), registrationSecret)176 .add(177 CAPS,178 new TestSessionFactory(179 (id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))180 .build();181 Node secondNode = LocalNode182 .builder(tracer, bus, externalUrl.toURI(), externalUrl.toURI(), registrationSecret)183 .add(184 CAPS,185 new TestSessionFactory(186 (id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))187 .build();188 handler.addHandler(firstNode);189 handler.addHandler(secondNode);190 bus.fire(new NodeStatusEvent(firstNode.getStatus()));191 bus.fire(new NodeStatusEvent(secondNode.getStatus()));192 wait.until(obj -> distributor.getStatus());193 Set<NodeStatus> nodes = distributor.getStatus().getNodes();194 assertEquals(1, nodes.size());195 }196 @Test197 public void distributorShouldUpdateStateOfExistingNodeWhenNodePublishesStateChange()198 throws URISyntaxException {199 URI sessionUri = new URI("http://example:1234");200 Node node = LocalNode201 .builder(tracer, bus, externalUrl.toURI(), externalUrl.toURI(), registrationSecret)202 .add(203 CAPS,204 new TestSessionFactory(205 (id, caps) -> new Session(id, sessionUri, stereotype, caps, Instant.now())))206 .build();207 handler.addHandler(node);208 bus.fire(new NodeStatusEvent(node.getStatus()));209 // Start empty210 wait.until(obj -> distributor.getStatus().hasCapacity());211 NodeStatus nodeStatus = getOnlyElement(distributor.getStatus().getNodes());212 assertEquals(1, getStereotypes(nodeStatus).get(CAPS).intValue());213 // Craft a status that makes it look like the node is busy, and post it on the bus.214 NodeStatus status = node.getStatus();215 NodeStatus crafted = new NodeStatus(216 status.getId(),217 status.getUri(),218 status.getMaxSessionCount(),219 ImmutableSet.of(220 new Slot(221 new SlotId(status.getId(), UUID.randomUUID()),222 CAPS,223 Instant.now(),224 Optional.of(new Session(225 new SessionId(UUID.randomUUID()), sessionUri, CAPS, CAPS, Instant.now())))),226 UP,227 Duration.ofSeconds(10),228 status.getVersion(),229 status.getOsInfo());230 bus.fire(new NodeStatusEvent(crafted));231 // We claimed the only slot is filled. Life is good.232 wait.until(obj -> !distributor.getStatus().hasCapacity());233 }234 private Map<Capabilities, Integer> getStereotypes(NodeStatus status) {235 Map<Capabilities, Integer> stereotypes = new HashMap<>();236 for (Slot slot : status.getSlots()) {237 int count = stereotypes.getOrDefault(slot.getStereotype(), 0);238 count++;239 stereotypes.put(slot.getStereotype(), count);240 }241 return ImmutableMap.copyOf(stereotypes);242 }243 static class CustomNode extends Node {244 private final EventBus bus;...
Source:LocalDistributor.java
...30import org.openqa.selenium.grid.data.NodeDrainComplete;31import org.openqa.selenium.grid.data.NodeId;32import org.openqa.selenium.grid.data.NodeRemovedEvent;33import org.openqa.selenium.grid.data.NodeStatus;34import org.openqa.selenium.grid.data.NodeStatusEvent;35import org.openqa.selenium.grid.data.Slot;36import org.openqa.selenium.grid.data.SlotId;37import org.openqa.selenium.grid.distributor.Distributor;38import org.openqa.selenium.grid.distributor.selector.DefaultSlotSelector;39import org.openqa.selenium.grid.log.LoggingOptions;40import org.openqa.selenium.grid.node.HealthCheck;41import org.openqa.selenium.grid.node.Node;42import org.openqa.selenium.grid.node.remote.RemoteNode;43import org.openqa.selenium.grid.security.Secret;44import org.openqa.selenium.grid.server.BaseServerOptions;45import org.openqa.selenium.grid.server.EventBusOptions;46import org.openqa.selenium.grid.server.NetworkOptions;47import org.openqa.selenium.grid.sessionmap.SessionMap;48import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;49import org.openqa.selenium.internal.Require;50import org.openqa.selenium.remote.http.HttpClient;51import org.openqa.selenium.remote.tracing.Tracer;52import org.openqa.selenium.status.HasReadyState;53import java.time.Duration;54import java.util.ArrayList;55import java.util.HashMap;56import java.util.List;57import java.util.Map;58import java.util.Optional;59import java.util.Set;60import java.util.concurrent.locks.Lock;61import java.util.concurrent.locks.ReadWriteLock;62import java.util.concurrent.locks.ReentrantReadWriteLock;63import java.util.function.Supplier;64import java.util.logging.Level;65import java.util.logging.Logger;66import static com.google.common.collect.ImmutableSet.toImmutableSet;67import static org.openqa.selenium.grid.data.Availability.DOWN;68import static org.openqa.selenium.grid.data.Availability.DRAINING;69public class LocalDistributor extends Distributor {70 private static final Logger LOG = Logger.getLogger(LocalDistributor.class.getName());71 private final Tracer tracer;72 private final EventBus bus;73 private final HttpClient.Factory clientFactory;74 private final SessionMap sessions;75 private final Secret registrationSecret;76 private final Regularly hostChecker = new Regularly("distributor host checker");77 private final Map<NodeId, Runnable> allChecks = new HashMap<>();78 private final ReadWriteLock lock = new ReentrantReadWriteLock(/* fair */ true);79 private final GridModel model;80 private final Map<NodeId, Node> nodes;81 public LocalDistributor(82 Tracer tracer,83 EventBus bus,84 HttpClient.Factory clientFactory,85 SessionMap sessions,86 Secret registrationSecret) {87 super(tracer, clientFactory, new DefaultSlotSelector(), sessions, registrationSecret);88 this.tracer = Require.nonNull("Tracer", tracer);89 this.bus = Require.nonNull("Event bus", bus);90 this.clientFactory = Require.nonNull("HTTP client factory", clientFactory);91 this.sessions = Require.nonNull("Session map", sessions);92 this.model = new GridModel(bus, registrationSecret);93 this.nodes = new HashMap<>();94 this.registrationSecret = Require.nonNull("Registration secret", registrationSecret);95 bus.addListener(NodeStatusEvent.listener(this::register));96 bus.addListener(NodeStatusEvent.listener(model::refresh));97 bus.addListener(NodeDrainComplete.listener(this::remove));98 }99 public static Distributor create(Config config) {100 Tracer tracer = new LoggingOptions(config).getTracer();101 EventBus bus = new EventBusOptions(config).getEventBus();102 HttpClient.Factory clientFactory = new NetworkOptions(config).getHttpClientFactory(tracer);103 SessionMap sessions = new SessionMapOptions(config).getSessionMap();104 BaseServerOptions serverOptions = new BaseServerOptions(config);105 return new LocalDistributor(tracer, bus, clientFactory, sessions, serverOptions.getRegistrationSecret());106 }107 @Override108 public boolean isReady() {109 try {110 return ImmutableSet.of(bus, sessions).parallelStream()...
NodeStatusEvent
Using AI Code Generation
1import org.openqa.selenium.grid.data.NodeStatusEvent;2import org.openqa.selenium.grid.data.Session;3import org.openqa.selenium.grid.data.SessionClosedEvent;4import org.openqa.selenium.grid.data.SessionCreatedEvent;5import org.openqa.selenium.grid.data.SessionEvent;6import org.openqa.selenium.grid.data.SessionInfo;7import org.openqa.selenium.grid.data.SessionRequest;8import org.openqa.selenium.grid.data.SessionRequestEvent;9import org.openqa.selenium.grid.data.SessionRequestRejectedEvent;10import org.openqa.selenium.grid.data.SessionRequestTimedOutEvent;11import org.openqa.selenium.grid.data.SessionStartedEvent;12import org.openqa.selenium.grid.data.SessionTerminatedEvent;13import org.openqa.selenium.grid.data.SessionTerminatedEvent.Reason;14import org.openqa.selenium.grid.data.Slot;15import org.openqa.selenium.grid.data.SlotEvent;16import org.openqa.selenium.grid.data.SlotId;17import org.openqa.selenium.grid.data.SlotState;18import org.openqa.selenium.grid.data.StandaloneSession;19import org.openqa.selenium.grid.data.StandaloneSessionId;20import org.openqa.selenium.grid.data.StandaloneSessionRequest;21import org.openqa.selenium.grid.data.StandaloneSessionRequestEvent;22import org.openqa.selenium.grid.data.StandaloneSessionRequestRejectedEvent;23import org.openqa.selenium.grid.data.StandaloneSessionRequestTimedOutEvent;24import org.openqa.selenium.grid.data.StandaloneSessionStartedEvent;25import org.openqa.selenium.grid.data.StandaloneSessionTerminatedEvent;26import org.openqa.selenium.grid.data.StandaloneSlot;27import org.openqa.selenium.grid.data.StandaloneSlotEvent;28import org.openqa.selenium.grid.data.StandaloneSlotId;29import org.openqa.selenium.grid.data.StandaloneSlotState;30import org.openqa.selenium.grid.data.StandaloneTestSession;31import org.openqa.selenium.grid.data.StandaloneTestSessionId;32import org.openqa.selenium.grid.data.StandaloneTestSessionRequest;33import org.openqa.selenium.grid.data.StandaloneTestSessionRequestEvent;34import org.openqa.selenium.grid.data.StandaloneTestSessionRequestRejectedEvent;35import org.openqa.selenium.grid.data.StandaloneTestSessionRequestTimedOutEvent;36import org.openqa.selenium.grid.data.StandaloneTestSessionStartedEvent;37import org.openqa.selenium.grid.data.StandaloneTestSessionTerminatedEvent;38import org.openqa.selenium.grid.data.StandaloneTestSlot;39import org.openqa.selenium.grid.data.StandaloneTestSlotEvent;40import org.openqa.selenium.grid.data.StandaloneTestSlotId;41import org.openqa.selenium.grid.data.StandaloneTestSlotState;42import org.openqa.selenium.grid.data.TestSession;43import org.openqa.selenium.grid.data.TestSessionId;44import org.openqa.selenium.grid.data.TestSessionRequest;45import org.openqa.selenium.grid
NodeStatusEvent
Using AI Code Generation
1import org.openqa.selenium.grid.data.NodeStatusEvent;2import org.openqa.selenium.grid.data.Session;3import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;4import org.openqa.selenium.grid.sessionmap.remote.RemoteSessionMap;5import org.openqa.selenium.internal.Require;6import org.openqa.selenium.remote.http.HttpClient;7import org.openqa.selenium.remote.tracing.Tracer;8import org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer;9import java.net.URI;10import java.time.Duration;11import java.util.Objects;12import java.util.Optional;13import java.util.Set;14import java.util.logging.Logger;15import java.util.stream.Collectors;16import static java.util.logging.Level.INFO;17import static java.util.logging.Level.WARNING;18public class SessionMap {19 private static final Logger LOG = Logger.getLogger(SessionMap.class.getName());20 private final RemoteSessionMap delegate;21 private final Tracer tracer;22 private final HttpClient.Factory clientFactory;23 public SessionMap(Tracer tracer, HttpClient.Factory clientFactory, URI uri) {24 this.tracer = Require.nonNull("Tracer", tracer);25 this.clientFactory = Require.nonNull("HTTP client factory", clientFactory);26 this.delegate = new RemoteSessionMap(tracer, clientFactory, uri);27 }28 public static SessionMap create(SessionMapOptions options) {29 return new SessionMap(30 new OpenTelemetryTracer(),31 HttpClient.Factory.createDefault(),32 options.getUri());33 }34 public Optional<Session> get(SessionId id) {35 Objects.requireNonNull(id, "Session ID to retrieve must be set.");36 return delegate.get(id);37 }38 public Set<Session> getAll() {39 return delegate.getAll();40 }41 public void add(Session session) {42 Objects.requireNonNull(session, "Session to add must be set.");43 delegate.add(session);44 }45 public void remove(SessionId id) {46 Objects.requireNonNull(id, "Session ID to remove must be set.");47 delegate.remove(id);48 }49 public void add(NodeStatusEvent event) {50 Objects.requireNonNull(event, "Node status event to add must be set.");51 delegate.add(event);52 }53 public void remove(NodeId id) {54 Objects.requireNonNull(id, "Node ID to remove must be set.");55 delegate.remove(id);56 }57 public void prune() {58 delegate.prune();59 }60 public void prune(Duration maxSessionAge) {61 Objects.requireNonNull(maxSessionAge, "Max session age to prune must be set.");
NodeStatusEvent
Using AI Code Generation
1import java.util.List;2import java.util.stream.Collectors;3import org.openqa.selenium.Capabilities;4import org.openqa.selenium.grid.data.NodeStatusEvent;5import org.openqa.selenium.grid.data.Session;6import org.openqa.selenium.grid.data.SessionRequest;7import org.openqa.selenium.grid.distributor.Distributor;8import org.openqa.selenium.grid.distributor.local.LocalDistributor;9import org.openqa.selenium.grid.node.local.LocalNode;10import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;11import org.openqa.selenium.grid.web.Routable;12import org.openqa.selenium.internal.Require;13import org.openqa.selenium.json.Json;14import org.openqa.selenium.remote.http.HttpMethod;15import org.openqa.selenium.remote.http.HttpRequest;16import org.openqa.selenium.remote.http.HttpResponse;17import com.google.common.collect.ImmutableMap;18public class DistributorExample {19 public static void main(String[] args) {20 Distributor distributor = new LocalDistributor(new SessionMapOptions());21 LocalNode node = LocalNode.builder(distributor).add(22 Capabilities.chrome(),23 Capabilities.firefox()).build();24 SessionRequest request = new SessionRequest(25 Capabilities.firefox(),26 ImmutableMap.of());27 Session session = distributor.newSession(request);28 List<NodeStatusEvent> nodeStatus = distributor.getStatus();29 nodeStatus.forEach(System.out::println);30 String sessionId = session.getId().toString();31 String nodeId = session.getSlot().get().getExternalKey();32 distributor.removeSession(sessionId);33 distributor.remove(nodeId);34 node.close();35 }36}37[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ example ---
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!!