|
| 1 | +package com.lambdatest; |
| 2 | + |
| 3 | + |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Optional; |
| 8 | + |
| 9 | +import org.openqa.selenium.By; |
| 10 | +import org.openqa.selenium.JavascriptExecutor; |
| 11 | +import org.openqa.selenium.WebDriver; |
| 12 | +import org.openqa.selenium.devtools.DevTools; |
| 13 | +import org.openqa.selenium.devtools.HasDevTools; |
| 14 | +import org.openqa.selenium.devtools.v96.network.Network; |
| 15 | +import org.openqa.selenium.remote.Augmenter; |
| 16 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 17 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 18 | + |
| 19 | + |
| 20 | +public class EmulateNetworkConditions { |
| 21 | + public static String hubURL = "https://hub.lambdatest.com/wd/hub"; |
| 22 | + |
| 23 | + private WebDriver driver; |
| 24 | + |
| 25 | + |
| 26 | + public void setup() throws MalformedURLException { |
| 27 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 28 | + capabilities.setCapability("browserName", "Chrome"); |
| 29 | + capabilities.setCapability("browserVersion", "latest"); |
| 30 | + HashMap<String, Object> ltOptions = new HashMap<String, Object>(); |
| 31 | + ltOptions.put("user", System.getenv("LT_USERNAME")); |
| 32 | + ltOptions.put("accessKey", System.getenv("LT_ACCESS_KEY")); |
| 33 | + ltOptions.put("build", "Selenium 4"); |
| 34 | + ltOptions.put("name",this.getClass().getName()); |
| 35 | + ltOptions.put("platformName", "Windows 10"); |
| 36 | + ltOptions.put("seCdp", true); |
| 37 | + ltOptions.put("selenium_version", "4.0.0"); |
| 38 | + capabilities.setCapability("LT:Options", ltOptions); |
| 39 | + |
| 40 | + driver = new RemoteWebDriver(new URL(hubURL), capabilities); |
| 41 | + |
| 42 | + System.out.println(driver); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + public void emulateNetworkConditions() throws InterruptedException { |
| 47 | + Augmenter augmenter = new Augmenter(); |
| 48 | + driver = augmenter.augment(driver); |
| 49 | + DevTools devTools = ((HasDevTools) driver).getDevTools(); |
| 50 | + devTools.createSession(); |
| 51 | + |
| 52 | + devTools.send(Network.emulateNetworkConditions(false, 0, 2 * 1024 * 1024, 2 * 1024 * 1024, Optional.empty())); |
| 53 | + driver.get("https://fast.com"); |
| 54 | + Thread.sleep(15000); |
| 55 | + String speed = driver.findElement(By.id("speed-value")).getText(); |
| 56 | + String units = driver.findElement(By.id("speed-units")).getText(); |
| 57 | + System.out.println(speed); |
| 58 | + System.out.println(units); |
| 59 | + if (Float.parseFloat(speed) < 2 && units.equals("Mbps")) { |
| 60 | + markStatus("passed", "Speed is in the limit of 2 Mbps", driver); |
| 61 | + } else { |
| 62 | + markStatus("failed", "Speed is not in the limit of 2 Mbp", driver); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + public void tearDown() { |
| 69 | + try { |
| 70 | + driver.quit(); |
| 71 | + |
| 72 | + } catch ( |
| 73 | + |
| 74 | + Exception e) { |
| 75 | + markStatus("failed", "Got exception!", driver); |
| 76 | + e.printStackTrace(); |
| 77 | + driver.quit(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void markStatus(String status, String reason, WebDriver driver) { |
| 82 | + JavascriptExecutor jsExecute = (JavascriptExecutor) driver; |
| 83 | + jsExecute.executeScript("lambda-status=" + status); |
| 84 | + System.out.println(reason); |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) throws MalformedURLException, InterruptedException { |
| 88 | + EmulateNetworkConditions test= new EmulateNetworkConditions(); |
| 89 | + test.setup(); |
| 90 | + test.emulateNetworkConditions(); |
| 91 | + test.tearDown(); |
| 92 | + } |
| 93 | +} |
0 commit comments