How to use LocalNode class of org.openqa.selenium.grid.node.local package

Best Selenium code snippet using org.openqa.selenium.grid.node.local.LocalNode

Run Selenium automation tests on LambdaTest cloud grid

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

copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.grid.node.httpd;
19
20import com.google.auto.service.AutoService;
21
22import com.beust.jcommander.JCommander;
23import com.beust.jcommander.ParameterException;
24
25import org.openqa.selenium.cli.CliCommand;
26import org.openqa.selenium.concurrent.Regularly;
27import org.openqa.selenium.events.EventBus;
28import org.openqa.selenium.grid.component.HealthCheck;
29import org.openqa.selenium.grid.config.AnnotatedConfig;
30import org.openqa.selenium.grid.config.CompoundConfig;
31import org.openqa.selenium.grid.config.ConcatenatingConfig;
32import org.openqa.selenium.grid.config.Config;
33import org.openqa.selenium.grid.config.EnvConfig;
34import org.openqa.selenium.grid.data.NodeStatusEvent;
35import org.openqa.selenium.grid.log.LoggingOptions;
36import org.openqa.selenium.grid.node.local.LocalNode;
37import org.openqa.selenium.grid.node.local.NodeFlags;
38import org.openqa.selenium.grid.server.BaseServer;
39import org.openqa.selenium.grid.server.BaseServerFlags;
40import org.openqa.selenium.grid.server.BaseServerOptions;
41import org.openqa.selenium.grid.server.EventBusConfig;
42import org.openqa.selenium.grid.server.EventBusFlags;
43import org.openqa.selenium.grid.server.HelpFlags;
44import org.openqa.selenium.grid.server.Server;
45import org.openqa.selenium.grid.server.W3CCommandHandler;
46import org.openqa.selenium.grid.web.Routes;
47import org.openqa.selenium.remote.http.HttpClient;
48import org.openqa.selenium.remote.tracing.DistributedTracer;
49import org.openqa.selenium.remote.tracing.GlobalDistributedTracer;
50
51import java.time.Duration;
52import java.util.logging.Logger;
53
54@AutoService(CliCommand.class)
55public class NodeServer implements CliCommand {
56
57  private static final Logger LOG = Logger.getLogger(NodeServer.class.getName());
58
59  @Override
60  public String getName() {
61    return "node";
62  }
63
64  @Override
65  public String getDescription() {
66    return "Adds this server as a node in the selenium grid.";
67  }
68
69  @Override
70  public Executable configure(String... args) {
71
72    HelpFlags help = new HelpFlags();
73    BaseServerFlags serverFlags = new BaseServerFlags(5555);
74    EventBusFlags eventBusFlags = new EventBusFlags();
75    NodeFlags nodeFlags = new NodeFlags();
76
77    JCommander commander = JCommander.newBuilder()
78        .programName(getName())
79        .addObject(help)
80        .addObject(serverFlags)
81        .addObject(eventBusFlags)
82        .addObject(nodeFlags)
83        .build();
84
85    return () -> {
86      try {
87        commander.parse(args);
88      } catch (ParameterException e) {
89        System.err.println(e.getMessage());
90        commander.usage();
91        return;
92      }
93
94      if (help.displayHelp(commander, System.out)) {
95        return;
96      }
97
98      Config config = new CompoundConfig(
99          new EnvConfig(),
100          new ConcatenatingConfig("node", '.', System.getProperties()),
101          new AnnotatedConfig(help),
102          new AnnotatedConfig(serverFlags),
103          new AnnotatedConfig(eventBusFlags),
104          new AnnotatedConfig(nodeFlags),
105          new DefaultNodeConfig());
106
107      LoggingOptions loggingOptions = new LoggingOptions(config);
108      loggingOptions.configureLogging();
109
110      DistributedTracer tracer = loggingOptions.getTracer();
111      GlobalDistributedTracer.setInstance(tracer);
112
113      EventBusConfig events = new EventBusConfig(config);
114      EventBus bus = events.getEventBus();
115
116      HttpClient.Factory clientFactory = HttpClient.Factory.createDefault();
117
118      BaseServerOptions serverOptions = new BaseServerOptions(config);
119
120      LocalNode.Builder builder = LocalNode.builder(
121          tracer,
122          bus,
123          clientFactory,
124          serverOptions.getExternalUri());
125      nodeFlags.configure(config, clientFactory, builder);
126      LocalNode node = builder.build();
127
128      Server<?> server = new BaseServer<>(serverOptions);
129      server.addRoute(Routes.matching(node).using(node).decorateWith(W3CCommandHandler.class));
130      server.start();
131
132      Regularly regularly = new Regularly("Register Node with Distributor");
133
134      regularly.submit(
135          () -> {
136            HealthCheck.Result check = node.getHealthCheck().check();
137            if (!check.isAlive()) {
138              LOG.info("Node is not alive: " + check.getMessage());
139              // Throw an exception to force another check sooner.
140              throw new UnsupportedOperationException("Node cannot be registered");
141            }
142
143            bus.fire(new NodeStatusEvent(node.getStatus()));
144          },
145          Duration.ofMinutes(5),
146          Duration.ofSeconds(30));
147    };
148  }
149}
150
Full Screen
copy
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18package org.openqa.selenium.grid.commands;
19
20import com.google.auto.service.AutoService;
21
22import com.beust.jcommander.JCommander;
23import com.beust.jcommander.ParameterException;
24
25import org.openqa.selenium.WebDriverException;
26import org.openqa.selenium.cli.CliCommand;
27import org.openqa.selenium.grid.config.AnnotatedConfig;
28import org.openqa.selenium.grid.config.CompoundConfig;
29import org.openqa.selenium.grid.config.ConcatenatingConfig;
30import org.openqa.selenium.grid.config.Config;
31import org.openqa.selenium.grid.config.EnvConfig;
32import org.openqa.selenium.grid.distributor.Distributor;
33import org.openqa.selenium.grid.distributor.local.LocalDistributor;
34import org.openqa.selenium.grid.node.local.LocalNode;
35import org.openqa.selenium.grid.node.local.NodeFlags;
36import org.openqa.selenium.grid.router.Router;
37import org.openqa.selenium.grid.server.BaseServer;
38import org.openqa.selenium.grid.server.BaseServerFlags;
39import org.openqa.selenium.grid.server.BaseServerOptions;
40import org.openqa.selenium.grid.server.HelpFlags;
41import org.openqa.selenium.grid.server.Server;
42import org.openqa.selenium.grid.server.W3CCommandHandler;
43import org.openqa.selenium.grid.sessionmap.SessionMap;
44import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;
45import org.openqa.selenium.grid.web.Routes;
46import org.openqa.selenium.net.NetworkUtils;
47import org.openqa.selenium.remote.tracing.DistributedTracer;
48
49import java.net.URI;
50import java.net.URISyntaxException;
51
52@AutoService(CliCommand.class)
53public class Standalone implements CliCommand {
54
55  @Override
56  public String getName() {
57    return "standalone";
58  }
59
60  @Override
61  public String getDescription() {
62    return "The selenium server, running everything in-process.";
63  }
64
65  @Override
66  public Executable configure(String... args) {
67    HelpFlags help = new HelpFlags();
68    BaseServerFlags baseFlags = new BaseServerFlags(4444);
69    NodeFlags nodeFlags = new NodeFlags();
70
71    JCommander commander = JCommander.newBuilder()
72        .programName("standalone")
73        .addObject(baseFlags)
74        .addObject(help)
75        .addObject(nodeFlags)
76        .build();
77
78    return () -> {
79      try {
80        commander.parse(args);
81      } catch (ParameterException e) {
82        System.err.println(e.getMessage());
83        commander.usage();
84        return;
85      }
86
87      if (help.displayHelp(commander, System.out)) {
88        return;
89      }
90
91      Config config = new CompoundConfig(
92          new AnnotatedConfig(help),
93          new AnnotatedConfig(baseFlags),
94          new EnvConfig(),
95          new ConcatenatingConfig("selenium", '.', System.getProperties()));
96
97      DistributedTracer tracer = DistributedTracer.builder()
98          .registerDetectedTracers()
99          .build();
100      DistributedTracer.setInstance(tracer);
101
102      SessionMap sessions = new LocalSessionMap();
103      Distributor distributor = new LocalDistributor(tracer);
104      Router router = new Router(sessions, distributor);
105
106      String hostName;
107      try {
108        hostName = new NetworkUtils().getNonLoopbackAddressOfThisMachine();
109      } catch (WebDriverException e) {
110        hostName = "localhost";
111      }
112
113      int port = config.getInt("server", "port")
114          .orElseThrow(() -> new IllegalArgumentException("No port to use configured"));
115      URI localhost = null;
116      try {
117        localhost = new URI("http", null, hostName, port, null, null, null);
118      } catch (URISyntaxException e) {
119        throw new RuntimeException(e);
120      }
121
122      LocalNode.Builder node = LocalNode.builder(tracer, localhost, sessions)
123          .maximumConcurrentSessions(Runtime.getRuntime().availableProcessors() * 3);
124      nodeFlags.configure(node);
125
126      distributor.add(node.build());
127
128      Server<?> server = new BaseServer<>(new BaseServerOptions(config));
129      server.addRoute(Routes.matching(router).using(router).decorateWith(W3CCommandHandler.class));
130      server.start();
131    };
132  }
133}
134
Full Screen
copy
1final Set<ClusterNode> nodes = new HashSet<>(ignite.cluster().forDataNodes("myCache") 
2                .forHost(ignite.cluster().localNode()).nodes()); 
3
4qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<String, Long>() { 
5        @Override 
6        public CacheEntryEventFilter<String, Long> create() { 
7           return new CacheEntryEventFilter<String, Long>() { 
8               @Override 
9               public boolean evaluate( 
10                    CacheEntryEvent<? extends String, ? extends Long> event) throws CacheEntryListenerException { 
11                    return nodes.contains(ignite.cluster().localNode()); 
12               } 
13            }; 
14        } 
15    }); 
16
Full Screen
copy
1val appDependencies = Seq(
2      "com.typesafe.akka" % "akka-remote" % "2.0.2"
3)
4
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)