How to use Interface HasNetworkConditions class of org.openqa.selenium.chromium package

Best Selenium code snippet using org.openqa.selenium.chromium.Interface HasNetworkConditions

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.chromium;
19
20import com.google.auto.service.AutoService;
21import com.google.common.collect.ImmutableMap;
22import org.openqa.selenium.Capabilities;
23import org.openqa.selenium.internal.Require;
24import org.openqa.selenium.remote.AdditionalHttpCommands;
25import org.openqa.selenium.remote.AugmenterProvider;
26import org.openqa.selenium.remote.CommandInfo;
27import org.openqa.selenium.remote.ExecuteMethod;
28import org.openqa.selenium.remote.http.HttpMethod;
29
30import java.time.Duration;
31import java.util.Map;
32import java.util.function.Predicate;
33
34import static org.openqa.selenium.chromium.ChromiumDriver.IS_CHROMIUM_BROWSER;
35
36@AutoService({AdditionalHttpCommands.class, AugmenterProvider.class})
37public class AddHasNetworkConditions implements AugmenterProvider<HasNetworkConditions>, AdditionalHttpCommands {
38
39  public static final String GET_NETWORK_CONDITIONS = "getNetworkConditions";
40  public static final String SET_NETWORK_CONDITIONS = "setNetworkConditions";
41  public static final String DELETE_NETWORK_CONDITIONS = "deleteNetworkConditions";
42
43
44  private static final Map<String, CommandInfo> COMMANDS = ImmutableMap.of(
45    GET_NETWORK_CONDITIONS, new CommandInfo("/session/:sessionId/chromium/network_conditions", HttpMethod.GET),
46    SET_NETWORK_CONDITIONS, new CommandInfo("/session/:sessionId/chromium/network_conditions", HttpMethod.POST),
47    DELETE_NETWORK_CONDITIONS, new CommandInfo("/session/:sessionId/chromium/network_conditions", HttpMethod.DELETE));
48
49  @Override
50  public Map<String, CommandInfo> getAdditionalCommands() {
51    return COMMANDS;
52  }
53
54  @Override
55  public Predicate<Capabilities> isApplicable() {
56    return caps -> IS_CHROMIUM_BROWSER.test(caps.getBrowserName());
57  }
58
59  @Override
60  public Class<HasNetworkConditions> getDescribedInterface() {
61    return HasNetworkConditions.class;
62  }
63
64  @Override
65  public HasNetworkConditions getImplementation(Capabilities capabilities, ExecuteMethod executeMethod) {
66    return new HasNetworkConditions() {
67      @Override
68      public ChromiumNetworkConditions getNetworkConditions() {
69        @SuppressWarnings("unchecked")
70        Map<String, Object> result = (Map<String, Object>) executeMethod.execute(GET_NETWORK_CONDITIONS, null);
71        ChromiumNetworkConditions networkConditions = new ChromiumNetworkConditions();
72        networkConditions.setOffline((Boolean) result.getOrDefault(ChromiumNetworkConditions.OFFLINE, false));
73        networkConditions.setLatency(Duration.ofMillis((Long) result.getOrDefault(ChromiumNetworkConditions.LATENCY, 0)));
74        networkConditions.setDownloadThroughput(((Number) result.getOrDefault(ChromiumNetworkConditions.DOWNLOAD_THROUGHPUT, -1)).intValue());
75        networkConditions.setDownloadThroughput(((Number) result.getOrDefault(ChromiumNetworkConditions.UPLOAD_THROUGHPUT, -1)).intValue());
76        return networkConditions;
77      }
78
79      @Override
80      public void setNetworkConditions(ChromiumNetworkConditions networkConditions) {
81        Require.nonNull("Network Conditions", networkConditions);
82
83        Map<String, Object> conditions = ImmutableMap.of(ChromiumNetworkConditions.OFFLINE, networkConditions.getOffline(),
84          ChromiumNetworkConditions.LATENCY, networkConditions.getLatency().toMillis(),
85          ChromiumNetworkConditions.DOWNLOAD_THROUGHPUT, networkConditions.getDownloadThroughput(),
86          ChromiumNetworkConditions.UPLOAD_THROUGHPUT, networkConditions.getUploadThroughput());
87        executeMethod.execute(SET_NETWORK_CONDITIONS, ImmutableMap.of("network_conditions", conditions));
88      }
89
90      @Override
91      public void deleteNetworkConditions() {
92        executeMethod.execute(DELETE_NETWORK_CONDITIONS, null);
93      }
94    };
95  }
96}
97
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.chromium;
19
20import org.openqa.selenium.Beta;
21
22/**
23 * Used by classes to indicate that they can simulate different network conditions.
24 */
25@Beta
26public interface HasNetworkConditions {
27
28  /**
29   * Gets map of network conditions.
30   * These have to be set before they can be retrieved.
31   *
32   * @return the current network condition values.
33   */
34  ChromiumNetworkConditions getNetworkConditions();
35
36  /**
37   * Set network limitations
38   *
39   * @param networkConditions object containing valid network condition settings.
40   */
41  void setNetworkConditions(ChromiumNetworkConditions networkConditions);
42
43  /**
44   * Resets the network conditions to the default settings.
45   */
46  void deleteNetworkConditions();
47
48}
49
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 popular Stackoverflow questions on Interface-HasNetworkConditions

    No relevant questions found for this class 😞

Most used methods in Interface-HasNetworkConditions

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)