Best Selenium code snippet using org.openqa.selenium.grid.data.NodeStatus.getSlots
Source:GridModel.java
...171 slotId.getOwningNodeId(),172 node.availability));173 return false;174 }175 Optional<Slot> maybeSlot = node.status.getSlots().stream()176 .filter(slot -> slotId.equals(slot.getId()))177 .findFirst();178 if (!maybeSlot.isPresent()) {179 LOG.warning(String.format(180 "Asked to reserve slot on node %s, but no slot with id %s found",181 node.status.getId(),182 slotId));183 return false;184 }185 reserve(node.status, maybeSlot.get());186 return true;187 } finally {188 writeLock.unlock();189 }190 }191 public Set<NodeStatus> getSnapshot() {192 Lock readLock = this.lock.readLock();193 readLock.lock();194 try {195 ImmutableSet.Builder<NodeStatus> snapshot = ImmutableSet.builder();196 for (Map.Entry<Availability, Set<NodeStatus>> entry : nodes.entrySet()) {197 entry.getValue().stream()198 .map(status -> rewrite(status, entry.getKey()))199 .forEach(snapshot::add);200 }201 return snapshot.build();202 } finally {203 readLock.unlock();204 }205 }206 private Set<NodeStatus> nodes(Availability availability) {207 return nodes.computeIfAbsent(availability, ignored -> new HashSet<>());208 }209 private AvailabilityAndNode findNode(NodeId id) {210 for (Map.Entry<Availability, Set<NodeStatus>> entry : nodes.entrySet()) {211 for (NodeStatus nodeStatus : entry.getValue()) {212 if (id.equals(nodeStatus.getId())) {213 return new AvailabilityAndNode(entry.getKey(), nodeStatus);214 }215 }216 }217 return null;218 }219 private NodeStatus rewrite(NodeStatus status, Availability availability) {220 return new NodeStatus(221 status.getId(),222 status.getUri(),223 status.getMaxSessionCount(),224 status.getSlots(),225 availability,226 status.getRegistrationSecret() == null ? null : new Secret(status.getRegistrationSecret()));227 }228 private void release(SessionId id) {229 if (id == null) {230 return;231 }232 Lock writeLock = lock.writeLock();233 writeLock.lock();234 try {235 for (Map.Entry<Availability, Set<NodeStatus>> entry : nodes.entrySet()) {236 for (NodeStatus node : entry.getValue()) {237 for (Slot slot : node.getSlots()) {238 if (!slot.getSession().isPresent()) {239 continue;240 }241 if (id.equals(slot.getSession().get().getId())) {242 Slot released = new Slot(243 slot.getId(),244 slot.getStereotype(),245 slot.getLastStarted(),246 Optional.empty());247 amend(entry.getKey(), node, released);248 return;249 }250 }251 }252 }253 } finally {254 writeLock.unlock();255 }256 }257 private void reserve(NodeStatus status, Slot slot) {258 Instant now = Instant.now();259 Slot reserved = new Slot(260 slot.getId(),261 slot.getStereotype(),262 now,263 Optional.of(new Session(264 RESERVED,265 status.getUri(),266 slot.getStereotype(),267 slot.getStereotype(),268 now)));269 amend(UP, status, reserved);270 }271 public void setSession(SlotId slotId, Session session) {272 Require.nonNull("Slot ID", slotId);273 AvailabilityAndNode node = findNode(slotId.getOwningNodeId());274 if (node == null) {275 LOG.warning("Grid model and reality have diverged. Unable to find node " + slotId.getOwningNodeId());276 return;277 }278 Optional<Slot> maybeSlot = node.status.getSlots().stream()279 .filter(slot -> slotId.equals(slot.getId()))280 .findFirst();281 if (!maybeSlot.isPresent()) {282 LOG.warning("Grid model and reality have diverged. Unable to find slot " + slotId);283 return;284 }285 Slot slot = maybeSlot.get();286 Optional<Session> maybeSession = slot.getSession();287 if (!maybeSession.isPresent()) {288 LOG.warning("Grid model and reality have diverged. Slot is not reserved. " + slotId);289 return;290 }291 Session current = maybeSession.get();292 if (!RESERVED.equals(current.getId())) {293 LOG.warning("Gid model and reality have diverged. Slot has session and is not reserved. " + slotId);294 return;295 }296 Slot updated = new Slot(297 slot.getId(),298 slot.getStereotype(),299 session == null ? slot.getLastStarted() : session.getStartTime(),300 Optional.ofNullable(session));301 amend(node.availability, node.status, updated);302 }303 private void amend(Availability availability, NodeStatus status, Slot slot) {304 Set<Slot> newSlots = new HashSet<>(status.getSlots());305 newSlots.removeIf(s -> s.getId().equals(slot.getId()));306 newSlots.add(slot);307 nodes(availability).remove(status);308 nodes(availability).add(new NodeStatus(309 status.getId(),310 status.getUri(),311 status.getMaxSessionCount(),312 newSlots,313 status.getAvailability(),314 status.getRegistrationSecret() == null ? null : new Secret(status.getRegistrationSecret())));315 }316 private static class AvailabilityAndNode {317 public final Availability availability;318 public final NodeStatus status;...
Source:DefaultSlotSelectorTest.java
...109 NodeStatus heavy = createNode(Collections.singletonList(caps), 10, 6);110 NodeStatus massive = createNode(Collections.singletonList(caps), 10, 8);111 Set<SlotId> ids = selector.selectSlot(caps, ImmutableSet.of(heavy, medium, lightest, massive));112 SlotId expected = ids.iterator().next();113 assertThat(lightest.getSlots().stream()).anyMatch(slot -> expected.equals(slot.getId()));114 }115 @Test116 public void nodesAreOrderedByNumberOfSupportedBrowsersAndLoad() {117 Capabilities chrome = new ImmutableCapabilities("browserName", "chrome");118 Capabilities firefox = new ImmutableCapabilities("browserName", "firefox");119 Capabilities safari = new ImmutableCapabilities("browserName", "safari");120 NodeStatus lightLoadAndThreeBrowsers =121 createNode(ImmutableList.of(chrome, firefox, safari), 12, 2);122 NodeStatus mediumLoadAndTwoBrowsers =123 createNode(ImmutableList.of(chrome, firefox), 12, 5);124 NodeStatus mediumLoadAndOtherTwoBrowsers =125 createNode(ImmutableList.of(safari, chrome), 12, 6);126 NodeStatus highLoadAndOneBrowser =127 createNode(ImmutableList.of(chrome), 12, 8);128 Set<SlotId> ids = selector.selectSlot(129 chrome,130 ImmutableSet.of(131 lightLoadAndThreeBrowsers,132 mediumLoadAndTwoBrowsers,133 mediumLoadAndOtherTwoBrowsers,134 highLoadAndOneBrowser));135 // The slot should belong to the Node with high load because it only supports Chrome, leaving136 // the other Nodes with more availability for other browsers137 SlotId expected = ids.iterator().next();138 assertThat(highLoadAndOneBrowser.getSlots().stream())139 .anyMatch(slot -> expected.equals(slot.getId()));140 // Nodes are ordered by the diversity of supported browsers, then by load141 ImmutableSet<NodeId> nodeIds = ids.stream()142 .map(SlotId::getOwningNodeId)143 .distinct()144 .collect(toImmutableSet());145 assertThat(nodeIds)146 .containsSequence(147 highLoadAndOneBrowser.getId(),148 mediumLoadAndTwoBrowsers.getId(),149 mediumLoadAndOtherTwoBrowsers.getId(),150 lightLoadAndThreeBrowsers.getId());151 }152 private NodeStatus createNode(List<Capabilities> stereotypes, int count, int currentLoad) {...
Source:Grid.java
...68 ImmutableList.Builder<Node> toReturn = ImmutableList.builder();69 for (NodeStatus status : distributorStatus.get().getNodes()) {70 Map<Capabilities, Integer> stereotypes = new HashMap<>();71 Map<org.openqa.selenium.grid.data.Session, Slot> sessions = new HashMap<>();72 for (Slot slot : status.getSlots()) {73 slot.getSession().ifPresent(session -> sessions.put(session, slot));74 int count = stereotypes.getOrDefault(slot.getStereotype(), 0);75 count++;76 stereotypes.put(slot.getStereotype(), count);77 }78 OsInfo osInfo = new OsInfo(79 status.getOsInfo().get("arch"),80 status.getOsInfo().get("name"),81 status.getOsInfo().get("version"));82 toReturn.add(new Node(83 status.getId(),84 status.getUri(),85 status.getAvailability(),86 status.getMaxSessionCount(),87 status.getSlots().size(),88 stereotypes,89 sessions,90 status.getVersion(),91 osInfo));92 }93 return toReturn.build();94 }95 public int getNodeCount() {96 return distributorStatus.get().getNodes().size();97 }98 public int getSessionCount() {99 return distributorStatus.get().getNodes().stream()100 .map(NodeStatus::getSlots)101 .flatMap(Collection::stream)102 .filter(slot -> slot.getSession().isPresent())103 .mapToInt(slot -> 1)104 .sum();105 }106 public int getTotalSlots() {107 return distributorStatus.get().getNodes().stream()108 .mapToInt(status -> status.getSlots().size())109 .sum();110 }111 public int getMaxSession() {112 return distributorStatus.get().getNodes().stream()113 .mapToInt(NodeStatus::getMaxSessionCount)114 .sum();115 }116 public int getSessionQueueSize() {117 return queueInfoList.size();118 }119 public List<String> getSessionQueueRequests() {120 // TODO: The Grid UI expects there to be a single capability per new session request, which is not correct121 return queueInfoList.stream()122 .map(set -> set.isEmpty() ? new ImmutableCapabilities() : set.iterator().next())123 .map(JSON::toJson)124 .collect(Collectors.toList());125 }126 public List<Session> getSessions() {127 List<Session> sessions = new ArrayList<>();128 for (NodeStatus status : distributorStatus.get().getNodes()) {129 for (Slot slot : status.getSlots()) {130 if (slot.getSession().isPresent()) {131 org.openqa.selenium.grid.data.Session session = slot.getSession().get();132 sessions.add(133 new org.openqa.selenium.grid.graphql.Session(134 session.getId().toString(),135 session.getCapabilities(),136 session.getStartTime(),137 session.getUri(),138 status.getId().toString(),139 status.getUri(),140 slot)141 );142 }143 }...
Source:SessionData.java
...55 }56 }57 private SessionInSlot findSession(String sessionId, Set<NodeStatus> nodeStatuses) {58 for (NodeStatus status : nodeStatuses) {59 for (Slot slot : status.getSlots()) {60 Optional<org.openqa.selenium.grid.data.Session> session = slot.getSession();61 if (session.isPresent() && sessionId.equals(session.get().getId().toString())) {62 return new SessionInSlot(session.get(), status, slot);63 }64 }65 }66 return null;67 }68 private static class SessionInSlot {69 private final org.openqa.selenium.grid.data.Session session;70 private final NodeStatus node;71 private final Slot slot;72 SessionInSlot(org.openqa.selenium.grid.data.Session session, NodeStatus node, Slot slot) {73 this.session = session;...
Source:DefaultSlotSelector.java
...43 // Then last session created (oldest first), so natural ordering again44 .thenComparingLong(NodeStatus::getLastSessionCreated)45 // And use the node id as a tie-breaker.46 .thenComparing(NodeStatus::getId))47 .flatMap(node -> node.getSlots().stream()48 .filter(slot -> !slot.getSession().isPresent())49 .filter(slot -> slot.isSupporting(capabilities))50 .map(Slot::getId))51 .collect(toImmutableSet());52 }53 @VisibleForTesting54 long getNumberOfSupportedBrowsers(NodeStatus nodeStatus) {55 return nodeStatus.getSlots()56 .stream()57 .map(slot -> slot.getStereotype().getBrowserName().toLowerCase())58 .distinct()59 .count();60 }61 public static SlotSelector create(Config config) {62 return new DefaultSlotSelector();63 }64}...
getSlots
Using AI Code Generation
1import org.openqa.selenium.grid.data.NodeStatus;2import org.openqa.selenium.grid.data.Slot;3import org.openqa.selenium.grid.data.SlotStatus;4import org.openqa.selenium.grid.node.local.LocalNode;5import org.openqa.selenium.grid.node.local.LocalNodeConfig;6import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;7import org.openqa.selenium.grid.web.Routable;8import org.openqa.selenium.remote.http.HttpClient;9import org.openqa.selenium.remote.tracing.Tracer;10import org.openqa.selenium.remote.tracing.global.GlobalTracer;11import java.net.URI;12import java.util.Map;13import java.util.concurrent.TimeUnit;14public class GetNodeSlots {15 public static void main(String[] args) {16 Tracer tracer = GlobalTracer.get();17 SessionMapOptions sessionMapOptions = new SessionMapOptions();18 sessionMapOptions.setSessionMap("in-memory");19 LocalNodeConfig localNodeConfig = new LocalNodeConfig();20 localNodeConfig.setMaxSession(1);21 localNodeConfig.setRegisterCycle(5);22 localNodeConfig.setRegister(true);23 localNodeConfig.setTimeout(30);24 localNodeConfig.setSessionMapOptions(sessionMapOptions);25 LocalNode localNode = new LocalNode(tracer, localNodeConfig, HttpClient.Factory.createDefault());26 localNode.start();27 NodeStatus nodeStatus = localNode.getStatus();28 Map<Slot, SlotStatus> slots = nodeStatus.getSlots();29 System.out.println(slots);30 localNode.stop();31 tracer.close();32 }33}
getSlots
Using AI Code Generation
1NodeStatus status = node.getStatus();2List<Slot> slots = status.getSlots();3for (Slot slot : slots) {4 SlotId slotId = slot.getId();5 SlotStatus slotStatus = slot.getStatus();6 Map<String, Object> slotCapabilities = slot.getCapabilities();7}8LocalDistributor distributor = new LocalDistributor(new DefaultDistributorConfig());9List<Slot> slots = distributor.getSlots();10for (Slot slot : slots) {11 SlotId slotId = slot.getId();12 SlotStatus slotStatus = slot.getStatus();13 Map<String, Object> slotCapabilities = slot.getCapabilities();14}15List<Slot> slots = distributor.getSlots();16for (Slot slot : slots) {17 SlotId slotId = slot.getId();18 SlotStatus slotStatus = slot.getStatus();19 Map<String, Object> slotCapabilities = slot.getCapabilities();20}21RoundRobinDistributor distributor = new RoundRobinDistributor(new DefaultDistributorConfig());22List<Slot> slots = distributor.getSlots();23for (Slot slot : slots) {24 SlotId slotId = slot.getId();25 SlotStatus slotStatus = slot.getStatus();26 Map<String, Object> slotCapabilities = slot.getCapabilities();27}
getSlots
Using AI Code Generation
1import org.openqa.selenium.grid.data.NodeStatus;2import org.openqa.selenium.grid.data.Slot;3NodeStatus nodeStatus = new NodeStatus();4List<Slot> slots = nodeStatus.getSlots();5System.out.println(slots);6import org.openqa.selenium.grid.data.NodeStatus;7import org.openqa.selenium.grid.data.Slot;8NodeStatus nodeStatus = new NodeStatus();9List<Slot> slots = nodeStatus.getSlots();10System.out.println(slots);11import org.openqa.selenium.grid.data.NodeStatus;12import org.openqa.selenium.grid.data.Slot;13NodeStatus nodeStatus = new NodeStatus();14List<Slot> slots = nodeStatus.getSlots();15System.out.println(slots);16NodeStatus nodeStatus = new NodeStatus();17List<Slot> slots = nodeStatus.getSlots();18System.out.println(slots);19import org.openqa.selenium.grid.data.NodeStatus;20import org.openqa.selenium.grid.data.Slot;21NodeStatus nodeStatus = new NodeStatus();22List<Slot> slots = nodeStatus.getSlots();23System.out.println(slots);24import org.openqa.selenium.grid.data.NodeStatus;25import org.openqa.selenium.grid.data.Slot;26NodeStatus nodeStatus = new NodeStatus();27List<Slot> slots = nodeStatus.getSlots();28System.out.println(slots);29import org.openqa.selenium.grid.data.NodeStatus;30import org.openqa.selenium.grid.data.Slot;31NodeStatus nodeStatus = new NodeStatus();32List<Slot> slots = nodeStatus.getSlots();33System.out.println(slots);34import org.openqa.selenium.grid.data.NodeStatus;35import org.openqa.selenium.grid.data.Slot;36NodeStatus nodeStatus = new NodeStatus();37List<Slot> slots = nodeStatus.getSlots();38System.out.println(slots);39import org.openqa.selenium.grid.data.NodeStatus;40import org.openqa.selenium.grid.data.Slot;41NodeStatus nodeStatus = new NodeStatus();42List<Slot> slots = nodeStatus.getSlots();43System.out.println(slots);44import org.openqa.selenium.grid.data.NodeStatus;45import org.openqa.selenium.grid.data.Slot;46NodeStatus nodeStatus = new NodeStatus();47List<Slot> slots = nodeStatus.getSlots();48System.out.println(slots);49import org.openqa.selenium.grid.data.NodeStatus;50import org.openqa.selenium.grid.data.Slot;51NodeStatus nodeStatus = new NodeStatus();
getSlots
Using AI Code Generation
1import org.openqa.selenium.grid.data.NodeStatus;2import org.openqa.selenium.grid.data.Slot;3import org.openqa.selenium.grid.data.SlotId;4import org.openqa.selenium.grid.data.SlotStatus;5import org.openqa.selenium.grid.data.SlotType;6import org.openqa.selenium.grid.data.Stoppable;7import org.openqa.selenium.grid.data.TestSlot;8import org.openqa.selenium.grid.data.TestSession;9import org.openqa.selenium.grid.web.Routable;10import org.openqa.selenium.internal.Require;11import org.openqa.selenium.json.Json;12import org.openqa.selenium.remote.http.HttpHandler;13import org.openqa.selenium.remote.http.HttpRequest;14import org.openqa.selenium.remote.http.HttpResponse;15import java.io.IOException;16import java.io.UncheckedIOException;17import java.net.URI;18import java.util.ArrayList;19import java.util.Collection;20import java.util.Collections;21import java.util.List;22import java.util.Map;23import java.util.Objects;24import java.util.Optional;25import java.util.Set;26import java.util.UUID;27import java.util.concurrent.ConcurrentHashMap;28import java.util.concurrent.CopyOnWriteArrayList;29import java.util.concurrent.CopyOnWriteArraySet;30import java.util.function.Predicate;31import java.util.logging.Logger;32import java.util.stream.Collectors;33import static org.openqa.selenium.remote.http.Contents.asJson;34import static org.openqa.selenium.remote.http.Contents.utf8String;35import static org.openqa.selenium.remote.http.HttpMethod.GET;36import static org.openqa.selenium.remote.http.HttpMethod.POST;37public class GetSlots {38 public static void main(String[] args) throws Exception {39 System.out.println("GetSlots");40 NodeStatus nodeStatus = new NodeStatus();41 List<Slot> slots = nodeStatus.getSlots();42 System.out.println("Number of slots: " + slots.size());43 System.out.println("Number of slots used: " + slots.stream().filter(Slot::isUsed).count());44 System.out.println("Number of slots free: " + slots.stream().filter(s -> !s.isUsed()).count());45 System.out.println("Total number of slots in a node: " + slots.size());46 System.out.println("Number of slots used in a node: " + slots.stream().filter(Slot::is
getSlots
Using AI Code Generation
1import org.openqa.selenium.grid.data.NodeStatus;2import org.openqa.selenium.grid.data.Slot;3import org.openqa.selenium.grid.data.SlotId;4import org.openqa.selenium.remote.http.HttpClient;5import org.openqa.selenium.remote.http.HttpRequest;6import org.openqa.selenium.remote.http.HttpResponse;7import org.openqa.selenium.remote.tracing.DefaultTestTracer;8import org.openqa.selenium.remote.tracing.Tracer;9import java.io.IOException;10import java.net.URI;11import java.net.URISyntaxException;12import java.util.List;13public class NodeStatusGetSlots {14 public static void main(String[] args) throws URISyntaxException, IOException {15 Tracer tracer = DefaultTestTracer.createTracer();16 HttpRequest request = new HttpRequest("GET", "/status");17 HttpResponse response = client.execute(request);18 NodeStatus status = NodeStatus.create(response, tracer);19 List<Slot> slots = status.getSlots();20 int slotsInUse = 0;21 int slotsFree = 0;22 for (Slot slot : slots) {23 SlotId slotId = slot.getId();24 if (slotId.isInUse()) {
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!!