-
Notifications
You must be signed in to change notification settings - Fork 4
/
CDPNetworkConditions.java
72 lines (60 loc) · 2.33 KB
/
CDPNetworkConditions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package example.test;
import java.time.Duration;
import java.util.Optional;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v91.network.model.ConnectionType;
import org.openqa.selenium.devtools.v91.network.Network;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
import static org.openqa.selenium.devtools.v91.network.Network.loadingFailed;
public class CDPNetworkConditions {
public ChromeDriver driver;
DevTools devTool;
@BeforeMethod
public void setUp() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
devTool = driver.getDevTools();
}
@AfterMethod
public void afterMethod() {
driver.quit();
}
@Test
public void emulateSlowNetwork() {
devTool.createSession();
devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTool.send(Network.emulateNetworkConditions(false, 100, 200000, 100000, Optional.of(ConnectionType.CELLULAR3G)));
long startTime = System.currentTimeMillis();
driver.get("https://www.qed42.com");
long endTime = System.currentTimeMillis();
System.out.println("Slow Network: Page loaded in " + (endTime - startTime) + " milliseconds");
}
@Test
public void emulateNetworkOffline() {
devTool.createSession();
devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTool.send(Network.emulateNetworkConditions(true, 100, 200000, 100000, Optional.of(ConnectionType.CELLULAR3G)));
devTool.addListener(loadingFailed(), loadingFailed -> {
Assert.assertEquals(loadingFailed.getErrorText(), "net::ERR_INTERNET_DISCONNECTED");
});
try {
driver.get("https://www.qed42.com");
} catch (WebDriverException exc) {
System.out.println("Network Offline: " + driver.getCurrentUrl() + " did not load");
}
}
@Test
public void accessURLNormal() {
long startTime = System.currentTimeMillis();
driver.get("https://www.qed42.com");
long endTime = System.currentTimeMillis();
System.out.println("Normal Way: Page loaded in " + (endTime - startTime) + " milliseconds");
}
}