How to use refresh method of org.openqa.selenium.grid.distributor.GridModel class

Best Selenium code snippet using org.openqa.selenium.grid.distributor.GridModel.refresh

Source:GridModel.java Github

copy

Full Screen

...57 this.events = Require.nonNull("Event bus", events);58 events.addListener(NodeDrainStarted.listener(nodeId -> setAvailability(nodeId, DRAINING)));59 events.addListener(NodeDrainComplete.listener(this::remove));60 events.addListener(NodeRemovedEvent.listener(this::remove));61 events.addListener(NodeStatusEvent.listener(status -> refresh(registrationSecret, status)));62 events.addListener(SessionClosedEvent.listener(this::release));63 }64 public GridModel add(NodeStatus node) {65 Require.nonNull("Node", node);66 Lock writeLock = lock.writeLock();67 writeLock.lock();68 try {69 // If we've already added the node, remove it.70 for (Set<NodeStatus> nodes : nodes.values()) {71 Iterator<NodeStatus> iterator = nodes.iterator();72 while (iterator.hasNext()) {73 NodeStatus next = iterator.next();74 // If the ID is the same, we're re-adding a node. If the URI is the same a node probably restarted75 if (next.getId().equals(node.getId()) || next.getUri().equals(node.getUri())) {76 LOG.info(String.format("Re-adding node with id %s and URI %s.", node.getId(), node.getUri()));77 iterator.remove();78 }79 }80 }81 // Nodes are initially added in the "down" state until something changes their availability82 nodes(DOWN).add(node);83 } finally {84 writeLock.unlock();85 }86 return this;87 }88 public GridModel refresh(Secret registrationSecret, NodeStatus status) {89 Require.nonNull("Node status", status);90 Secret statusSecret = status.getRegistrationSecret() == null ? null : new Secret(status.getRegistrationSecret());91 if (!Objects.equals(registrationSecret, statusSecret)) {92 LOG.severe(String.format("Node at %s failed to send correct registration secret. Node NOT refreshed.", status.getUri()));93 events.fire(new NodeRejectedEvent(status.getUri()));94 return this;95 }96 Lock writeLock = lock.writeLock();97 writeLock.lock();98 try {99 AvailabilityAndNode availabilityAndNode = findNode(status.getId());100 if (availabilityAndNode == null) {101 return this;102 }103 // if the node was marked as "down", keep it down until a healthcheck passes:104 // just because the node can hit the event bus doesn't mean it's reachable105 if (DOWN.equals(availabilityAndNode.availability)) {106 nodes(DOWN).remove(availabilityAndNode.status);...

Full Screen

Full Screen

Source:LocalDistributor.java Github

copy

Full Screen

...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()111 .map(HasReadyState::isReady)112 .reduce(true, Boolean::logicalAnd);113 } catch (RuntimeException e) {114 return false;115 }116 }117 private void register(NodeStatus status) {118 Require.nonNull("Node", status);119 Lock writeLock = lock.writeLock();120 writeLock.lock();121 try {122 if (nodes.containsKey(status.getId())) {123 return;124 }125 Set<Capabilities> capabilities = status.getSlots().stream()126 .map(Slot::getStereotype)127 .map(ImmutableCapabilities::copyOf)128 .collect(toImmutableSet());129 // A new node! Add this as a remote node, since we've not called add130 RemoteNode remoteNode = new RemoteNode(131 tracer,132 clientFactory,133 status.getId(),134 status.getUri(),135 registrationSecret,136 capabilities);137 add(remoteNode);138 } finally {139 writeLock.unlock();140 }141 }142 @Override143 public LocalDistributor add(Node node) {144 Require.nonNull("Node", node);145 LOG.info(String.format("Added node %s at %s.", node.getId(), node.getUri()));146 nodes.put(node.getId(), node);147 model.add(node.getStatus());148 // Extract the health check149 Runnable runnableHealthCheck = asRunnableHealthCheck(node);150 allChecks.put(node.getId(), runnableHealthCheck);151 hostChecker.submit(runnableHealthCheck, Duration.ofMinutes(5), Duration.ofSeconds(30));152 bus.fire(new NodeAddedEvent(node.getId()));153 return this;154 }155 private Runnable asRunnableHealthCheck(Node node) {156 HealthCheck healthCheck = node.getHealthCheck();157 NodeId id = node.getId();158 return () -> {159 HealthCheck.Result result;160 try {161 result = healthCheck.check();162 } catch (Exception e) {163 LOG.log(Level.WARNING, "Unable to process node " + id, e);164 result = new HealthCheck.Result(DOWN, "Unable to run healthcheck. Assuming down");165 }166 Lock writeLock = lock.writeLock();167 writeLock.lock();168 try {169 model.setAvailability(id, result.getAvailability());170 } finally {171 writeLock.unlock();172 }173 };174 }175 @Override176 public boolean drain(NodeId nodeId) {177 Node node = nodes.get(nodeId);178 if (node == null) {179 LOG.info("Asked to drain unregistered node " + nodeId);180 return false;181 }182 Lock writeLock = lock.writeLock();183 writeLock.lock();184 try {185 node.drain();186 model.setAvailability(nodeId, DRAINING);187 } finally {188 writeLock.unlock();189 }190 return node.isDraining();191 }192 public void remove(NodeId nodeId) {193 Lock writeLock = lock.writeLock();194 writeLock.lock();195 try {196 model.remove(nodeId);197 Runnable runnable = allChecks.remove(nodeId);198 if (runnable != null) {199 hostChecker.remove(runnable);200 }201 } finally {202 writeLock.unlock();203 bus.fire(new NodeRemovedEvent(nodeId));204 }205 }206 @Override207 public DistributorStatus getStatus() {208 Lock readLock = this.lock.readLock();209 readLock.lock();210 try {211 return new DistributorStatus(model.getSnapshot());212 } finally {213 readLock.unlock();214 }215 }216 @Beta217 public void refresh() {218 List<Runnable> allHealthChecks = new ArrayList<>();219 Lock readLock = this.lock.readLock();220 readLock.lock();221 try {222 allHealthChecks.addAll(allChecks.values());223 } finally {224 readLock.unlock();225 }226 allHealthChecks.parallelStream().forEach(Runnable::run);227 }228 @Override229 protected Set<NodeStatus> getAvailableNodes() {230 Lock readLock = this.lock.readLock();231 readLock.lock();...

Full Screen

Full Screen

refresh

Using AI Code Generation

copy

Full Screen

1package com.seleniumgrid;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.chrome.ChromeDriver;9import org.openqa.selenium.chrome.ChromeOptions;10import org.openqa.selenium.devtools.DevTools;11import org.openqa.selenium.devtools.v90.network.Network;12import org.openqa.selenium.devtools.v90.network.model.ConnectionType;13import org.openqa.selenium.devtools.v90.network.model.ConnectionTypeChanged;14import org.openqa.selenium.devtools.v90.network.model.Headers;15import org.openqa.selenium.devtools.v90.network.model.Request;16import org.openqa.selenium.devtools.v90.network.model.RequestWillBeSent;17import org.openqa.selenium.devtools.v90.network.model.Response;18import org.openqa.selenium.devtools.v90.network.model.ResponseReceived;19import org.openqa.selenium.devtools.v90.network.model.ResourceType;20import org.openqa.selenium.devtools.v90.network.model.WillSendRequest;21import org.openqa.selenium.devtools.v90.page.Page;22import org.openqa.selenium.grid.distributor.GridModel;23import org.openqa.selenium.grid.distributor.local.LocalDistributor;24import org.openqa.selenium.grid.node.local.LocalNode;25import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;26import org.openqa.selenium.grid.web.Routable;27import org.openqa.selenium.grid.web.Routes;28import org.openqa.selenium.grid.web.Values;29import org.openqa.selenium.remote.http.HttpMethod;30import org.openqa.selenium.remote.http.HttpRequest;31import org.openqa.selenium.remote.http.HttpResponse;32public class GridModelRefresh {33 public static void main(String[] args) throws MalformedURLException, InterruptedException {34 ChromeOptions options = new ChromeOptions();35 options.setExperimentalOption("w3c", true);36 LocalNode.Builder nodeBuilder = LocalNode.builder(options);37 nodeBuilder.add(new Routable() {38 public void bindTo(Routes routes) {39 routes.get("/ping", (req, res) -> {40 Values values = new Values(req);41 String message = values.get("message").orElse("pong");42 res.setStatus(HttpResponse.Code.OK);43 res.setContent(HttpResponse.asJson(message));44 return res;45 });46 }47 });48 LocalNode node = nodeBuilder.build();49 node.start();50 LocalDistributor.Builder distributorBuilder = LocalDistributor.builder();51 distributorBuilder.add(node);

Full Screen

Full Screen

refresh

Using AI Code Generation

copy

Full Screen

1import java.io.IOException;2import java.net.URI;3import java.util.List;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.Capabilities;6import org.openqa.selenium.ImmutableCapabilities;7import org.openqa.selenium.SessionNotCreatedException;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.grid.config.Config;10import org.openqa.selenium.grid.config.MapConfig;11import org.openqa.selenium.grid.config.TomlConfig;12import org.openqa.selenium.grid.distributor.Distributor;13import org.openqa.selenium.grid.distributor.local.LocalDistributor;14import org.openqa.selenium.grid.node.local.LocalNode;15import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;16import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;17import org.openqa.selenium.grid.web.Routable;18import org.openqa.selenium.internal.Require;19import org.openqa.selenium.net.PortProber;20import org.openqa.selenium.remote.http.HttpClient;21import org.openqa.selenium.remote.tracing.Tracer;22public class GridModelRefresh {23 public static void main(String[] args) throws IOException {24 Tracer tracer = Tracer.getDefault();25 HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();26 Config config = new TomlConfig("config.toml");27 SessionMapOptions sessionMapOptions = new SessionMapOptions(config);28 LocalSessionMap sessions = new LocalSessionMap(tracer, sessionMapOptions);29 LocalDistributor distributor = new LocalDistributor(tracer, sessions, clientFactory);30 int port = PortProber.findFreePort();31 LocalNode node = builder.build();32 distributor.add(node);33 System.out.println("Node status before refresh: " + node.getStatus());34 distributor.refresh(node);35 System.out.println("Node status after refresh: " + node.getStatus());36 node.stop();37 }38}39import java.io.IOException;40import java.net.URI;41import java.util.List;42import java.util.concurrent.TimeUnit;43import org.openqa.selenium.Capabilities;44import org.openqa.selenium.ImmutableCapabilities;45import org.openqa.selenium.SessionNotCreatedException;46import org.openqa.selenium.WebDriver;47import org.openqa.selenium.grid

Full Screen

Full Screen

refresh

Using AI Code Generation

copy

Full Screen

1GridModel gridModel = new GridModel();2gridModel.refresh();3List<RemoteProxy> nodes = gridModel.getNodes();4List<TestSession> sessions = gridModel.getSessions();5List<TestSession> activeSessions = gridModel.getActiveSessions();6List<TestSession> pendingSessions = gridModel.getPendingSessions();7List<TestSession> completedSessions = gridModel.getCompletedSessions();8List<RemoteProxy> proxies = gridModel.getAllProxies();9List<RemoteProxy> activeProxies = gridModel.getActiveProxies();10List<RemoteProxy> inactiveProxies = gridModel.getInactiveProxies();11List<RemoteProxy> busyProxies = gridModel.getBusyProxies();12List<RemoteProxy> availableProxies = gridModel.getAvailableProxies();13RemoteProxy proxyById = gridModel.getProxyById("id");14TestSession sessionById = gridModel.getSessionById("id");15List<TestSession> sessionsByProxyId = gridModel.getSessionsByProxyId("id");16List<TestSession> sessionsByCapabilities = gridModel.getSessionsByCapabilities(new DesiredCapabilities());

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful