How to use MapConfig class of org.openqa.selenium.grid.config package

Best Selenium code snippet using org.openqa.selenium.grid.config.MapConfig

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.router.httpd;
19
20import com.google.auto.service.AutoService;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.ImmutableSet;
23import org.openqa.selenium.BuildInfo;
24import org.openqa.selenium.cli.CliCommand;
25import org.openqa.selenium.grid.TemplateGridCommand;
26import org.openqa.selenium.grid.config.Config;
27import org.openqa.selenium.grid.config.MapConfig;
28import org.openqa.selenium.grid.config.Role;
29import org.openqa.selenium.grid.distributor.Distributor;
30import org.openqa.selenium.grid.distributor.config.DistributorOptions;
31import org.openqa.selenium.grid.distributor.remote.RemoteDistributor;
32import org.openqa.selenium.grid.graphql.GraphqlHandler;
33import org.openqa.selenium.grid.log.LoggingOptions;
34import org.openqa.selenium.grid.router.ProxyCdpIntoGrid;
35import org.openqa.selenium.grid.router.Router;
36import org.openqa.selenium.grid.server.BaseServerOptions;
37import org.openqa.selenium.grid.server.NetworkOptions;
38import org.openqa.selenium.grid.server.Server;
39import org.openqa.selenium.grid.sessionmap.SessionMap;
40import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
41import org.openqa.selenium.netty.server.NettyServer;
42import org.openqa.selenium.remote.http.HttpClient;
43import org.openqa.selenium.remote.http.HttpResponse;
44import org.openqa.selenium.remote.http.Route;
45import org.openqa.selenium.remote.tracing.Tracer;
46
47import java.net.URL;
48import java.util.Collections;
49import java.util.Set;
50import java.util.logging.Logger;
51
52import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
53import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
54import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
55import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
56import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_MAP_ROLE;
57import static org.openqa.selenium.net.Urls.fromUri;
58import static org.openqa.selenium.remote.http.Route.get;
59
60@AutoService(CliCommand.class)
61public class RouterServer extends TemplateGridCommand {
62
63  private static final Logger LOG = Logger.getLogger(RouterServer.class.getName());
64
65  @Override
66  public String getName() {
67    return "router";
68  }
69
70  @Override
71  public String getDescription() {
72    return "Creates a router to front the selenium grid.";
73  }
74
75  @Override
76  public Set<Role> getConfigurableRoles() {
77    return ImmutableSet.of(DISTRIBUTOR_ROLE, HTTPD_ROLE, ROUTER_ROLE, SESSION_MAP_ROLE);
78  }
79
80  @Override
81  public Set<Object> getFlagObjects() {
82    return Collections.emptySet();
83  }
84
85  @Override
86  protected String getSystemPropertiesConfigPrefix() {
87    return "router";
88  }
89
90  @Override
91  protected Config getDefaultConfig() {
92    return new MapConfig(ImmutableMap.of("server", ImmutableMap.of("port", 4444)));
93  }
94
95  @Override
96  protected void execute(Config config) {
97    LoggingOptions loggingOptions = new LoggingOptions(config);
98    Tracer tracer = loggingOptions.getTracer();
99
100    NetworkOptions networkOptions = new NetworkOptions(config);
101    HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory(tracer);
102
103    SessionMapOptions sessionsOptions = new SessionMapOptions(config);
104    SessionMap sessions = sessionsOptions.getSessionMap();
105
106    BaseServerOptions serverOptions = new BaseServerOptions(config);
107
108    DistributorOptions distributorOptions = new DistributorOptions(config);
109    URL distributorUrl = fromUri(distributorOptions.getDistributorUri());
110    Distributor distributor = new RemoteDistributor(
111      tracer,
112      clientFactory,
113      distributorUrl,
114      serverOptions.getRegistrationSecret());
115
116    GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri());
117
118    Route handler = Route.combine(
119      new Router(tracer, clientFactory, sessions, distributor).with(networkOptions.getSpecComplianceChecks()),
120      Route.post("/graphql").to(() -> graphqlHandler),
121      get("/readyz").to(() -> req -> new HttpResponse().setStatus(HTTP_NO_CONTENT)));
122
123    Server<?> server = new NettyServer(serverOptions, handler, new ProxyCdpIntoGrid(clientFactory, sessions));
124    server.start();
125
126    BuildInfo info = new BuildInfo();
127    LOG.info(String.format(
128      "Started Selenium router %s (revision %s): %s",
129      info.getReleaseLabel(),
130      info.getBuildRevision(),
131      server.getUrl()));
132  }
133}
134
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;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.ImmutableSet;
23import org.openqa.selenium.BuildInfo;
24import org.openqa.selenium.cli.CliCommand;
25import org.openqa.selenium.events.Event;
26import org.openqa.selenium.events.EventBus;
27import org.openqa.selenium.events.EventListener;
28import org.openqa.selenium.events.EventName;
29import org.openqa.selenium.grid.TemplateGridCommand;
30import org.openqa.selenium.grid.config.CompoundConfig;
31import org.openqa.selenium.grid.config.Config;
32import org.openqa.selenium.grid.config.MapConfig;
33import org.openqa.selenium.grid.config.MemoizedConfig;
34import org.openqa.selenium.grid.config.Role;
35import org.openqa.selenium.grid.server.BaseServerOptions;
36import org.openqa.selenium.grid.server.EventBusOptions;
37import org.openqa.selenium.grid.server.Server;
38import org.openqa.selenium.internal.Require;
39import org.openqa.selenium.netty.server.NettyServer;
40import org.openqa.selenium.remote.http.HttpResponse;
41import org.openqa.selenium.remote.http.Route;
42
43import java.util.Collections;
44import java.util.Set;
45import java.util.concurrent.CountDownLatch;
46import java.util.concurrent.TimeUnit;
47import java.util.logging.Logger;
48
49import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
50import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
51import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
52import static org.openqa.selenium.json.Json.JSON_UTF_8;
53import static org.openqa.selenium.remote.http.Contents.asJson;
54
55@AutoService(CliCommand.class)
56public class EventBusCommand extends TemplateGridCommand {
57  private static final Logger LOG = Logger.getLogger(EventBusCommand.class.getName());
58
59  @Override
60  public String getName() {
61    return "event-bus";
62  }
63
64  @Override
65  public String getDescription() {
66    return "Standalone instance of the event bus.";
67  }
68
69  @Override
70  public Set<Role> getConfigurableRoles() {
71    return ImmutableSet.of(EVENT_BUS_ROLE, HTTPD_ROLE);
72  }
73
74  @Override
75  public boolean isShown() {
76    return false;
77  }
78
79  @Override
80  public Set<Object> getFlagObjects() {
81    return Collections.emptySet();
82  }
83
84  @Override
85  protected String getSystemPropertiesConfigPrefix() {
86    return "selenium";
87  }
88
89  @Override
90  protected Config getDefaultConfig() {
91    return new MapConfig(ImmutableMap.of(
92      "events", ImmutableMap.of(
93        "bind", true,
94        "publish", "tcp://*:4442",
95        "subscribe", "tcp://*:4443"),
96      "server", ImmutableMap.of(
97        "port", 5557)));
98  }
99
100  public Server<?> asServer(Config initialConfig) {
101    Require.nonNull("Config", initialConfig);
102
103    Config config = new MemoizedConfig(new CompoundConfig(initialConfig, getDefaultConfig()));
104
105    EventBusOptions events = new EventBusOptions(config);
106    EventBus bus = events.getEventBus();
107
108    BaseServerOptions serverOptions = new BaseServerOptions(config);
109
110    return new NettyServer(
111      serverOptions,
112      Route.combine(
113        Route.get("/status").to(() -> req -> {
114          CountDownLatch latch = new CountDownLatch(1);
115
116          EventName healthCheck = new EventName("healthcheck");
117          bus.addListener(new EventListener<>(healthCheck, Object.class, obj -> latch.countDown()));
118          bus.fire(new Event(healthCheck, "ping"));
119
120          try {
121            if (latch.await(5, TimeUnit.SECONDS)) {
122              return httpResponse(true, "Event bus running");
123            } else {
124              return httpResponse(false, "Event bus could not deliver a test message in 5 seconds");
125            }
126          } catch (InterruptedException e) {
127            Thread.currentThread().interrupt();
128            return httpResponse(false, "Status checking was interrupted");
129          }
130        }),
131        Route.get("/readyz").to(() -> req -> new HttpResponse().setStatus(HTTP_NO_CONTENT)))
132    );
133  }
134
135  @Override
136  protected void execute(Config config) {
137    Require.nonNull("Config", config);
138
139    Server<?> server = asServer(config);
140    server.start();
141
142    BuildInfo info = new BuildInfo();
143    LOG.info(String.format(
144      "Started Selenium event bus %s (revision %s): %s",
145      info.getReleaseLabel(),
146      info.getBuildRevision(),
147      server.getUrl()));
148  }
149
150  private HttpResponse httpResponse(boolean ready, String message) {
151    return new HttpResponse()
152        .addHeader("Content-Type", JSON_UTF_8)
153        .setContent(asJson(ImmutableMap.of(
154          "value", ImmutableMap.of(
155            "ready", ready,
156            "message", message))));
157  }
158}
159
Full Screen
copy
1Stream.of(input).toMap(e -> e.getKey().substring(subLength), 
2                       e -> AttributeType.GetByName(e.getValue()));
3
Full Screen
copy
1private Map<String, String> mapConfig(Map<String, Integer> input, String prefix) {
2    int subLength = prefix.length();
3    return EntryStream.of(input)
4            .mapKeys(key -> key.substring(subLength))
5            .mapValues(AttributeType::GetByName)
6            .toMap();
7}
8
Full Screen
copy
1private Map<String, AttributeType> mapConfig(Map<String, String> input, String prefix) {
2   int subLength = prefix.length();
3   return input.entrySet().stream().map(e -> 
4      Map.entry(e.getKey().substring(subLength), AttributeType.GetByName(e.getValue()))
5   ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
6}
7
Full Screen
copy
1private Map<String, AttributeType> mapConfig(
2    Map<String, String> input, String prefix) {
3       int subLength = prefix.length();
4       return input.entrySet()
5          .stream()
6          .map(e -> new AbstractMap.SimpleEntry<>(
7               e.getKey().substring(subLength),
8               AttributeType.GetByName(e.getValue()))
9          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
10
Full Screen
copy
1<K, V> Collector<? super Map.Entry<K, V>, ?, Map<K, V>> toMap() {
2  return Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue);
3}
4
Full Screen
copy
1private Map<String, String> mapConfig(Map<String, Integer> input, String prefix) {
2    int subLength = prefix.length();
3    return input.entrySet().stream()
4            .collect(Collectors.toMap(
5                   entry -> entry.getKey().substring(subLength), 
6                   entry -> AttributeType.GetByName(entry.getValue())));
7}
8
Full Screen
copy
1Stream.of(input).toMap(e -> e.getKey().substring(subLength), 
2                       e -> AttributeType.GetByName(e.getValue()));
3
Full Screen
copy
1private Map<String, String> mapConfig(Map<String, Integer> input, String prefix) {
2    int subLength = prefix.length();
3    return EntryStream.of(input)
4            .mapKeys(key -> key.substring(subLength))
5            .mapValues(AttributeType::GetByName)
6            .toMap();
7}
8
Full Screen
copy
1private Map<String, AttributeType> mapConfig(Map<String, String> input, String prefix) {
2   int subLength = prefix.length();
3   return input.entrySet().stream().map(e -> 
4      Map.entry(e.getKey().substring(subLength), AttributeType.GetByName(e.getValue()))
5   ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
6}
7
Full Screen
copy
1private Map<String, AttributeType> mapConfig(
2    Map<String, String> input, String prefix) {
3       int subLength = prefix.length();
4       return input.entrySet()
5          .stream()
6          .map(e -> new AbstractMap.SimpleEntry<>(
7               e.getKey().substring(subLength),
8               AttributeType.GetByName(e.getValue()))
9          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
10
Full Screen
copy
1<K, V> Collector<? super Map.Entry<K, V>, ?, Map<K, V>> toMap() {
2  return Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue);
3}
4
Full Screen
copy
1private Map<String, String> mapConfig(Map<String, Integer> input, String prefix) {
2    int subLength = prefix.length();
3    return input.entrySet().stream()
4            .collect(Collectors.toMap(
5                   entry -> entry.getKey().substring(subLength), 
6                   entry -> AttributeType.GetByName(entry.getValue())));
7}
8
Full Screen
copy
1Stream.of(input).toMap(e -> e.getKey().substring(subLength), 
2                       e -> AttributeType.GetByName(e.getValue()));
3
Full Screen
copy
1private Map<String, String> mapConfig(Map<String, Integer> input, String prefix) {
2    int subLength = prefix.length();
3    return EntryStream.of(input)
4            .mapKeys(key -> key.substring(subLength))
5            .mapValues(AttributeType::GetByName)
6            .toMap();
7}
8
Full Screen
copy
1private Map<String, AttributeType> mapConfig(Map<String, String> input, String prefix) {
2   int subLength = prefix.length();
3   return input.entrySet().stream().map(e -> 
4      Map.entry(e.getKey().substring(subLength), AttributeType.GetByName(e.getValue()))
5   ).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
6}
7
Full Screen
copy
1private Map<String, AttributeType> mapConfig(
2    Map<String, String> input, String prefix) {
3       int subLength = prefix.length();
4       return input.entrySet()
5          .stream()
6          .map(e -> new AbstractMap.SimpleEntry<>(
7               e.getKey().substring(subLength),
8               AttributeType.GetByName(e.getValue()))
9          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
10
Full Screen
copy
1<K, V> Collector<? super Map.Entry<K, V>, ?, Map<K, V>> toMap() {
2  return Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue);
3}
4
Full Screen
copy
1private Map<String, String> mapConfig(Map<String, Integer> input, String prefix) {
2    int subLength = prefix.length();
3    return input.entrySet().stream()
4            .collect(Collectors.toMap(
5                   entry -> entry.getKey().substring(subLength), 
6                   entry -> AttributeType.GetByName(entry.getValue())));
7}
8
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

Most used methods in MapConfig

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
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)