1package com.devtools;
2
3import org.openqa.selenium.chrome.ChromeDriver;
4import org.openqa.selenium.devtools.DevTools;
5import org.openqa.selenium.devtools.network.Network;
6import org.openqa.selenium.devtools.network.model.ConnectionType;
7
8import java.util.HashMap;
9import java.util.Map;
10import java.util.Optional;
11
12public class SetNetwork {
13
14 final static String PROJECT_PATH = System.getProperty("user.dir");
15
16 public static void main(String[] args){
17 System.setProperty("webdriver.chrome.driver", PROJECT_PATH + "/src/main/resources/chromedriver");
18 ChromeDriver driver;
19 driver = new ChromeDriver();
20
21 DevTools devTools = driver.getDevTools();
22 devTools.createSession();
23 devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
24 devTools.send(Network.emulateNetworkConditions(
25 false,
26 20,
27 20,
28 50,
29 Optional.of(ConnectionType.CELLULAR2G)
30 ));
31 driver.get("https://www.google.com");
32 }
33}
1package com.devtools;
2
3import org.openqa.selenium.chrome.ChromeDriver;
4import org.openqa.selenium.devtools.DevTools;
5import org.openqa.selenium.devtools.emulation.Emulation;
6import org.openqa.selenium.devtools.network.model.ConnectionType;
7
8import java.util.HashMap;
9import java.util.Map;
10import java.util.Optional;
11
12public class GeoMocking {
13
14 final static String PROJECT_PATH = System.getProperty("user.dir");
15
16 public static void main(String[] args){
17 System.setProperty("webdriver.chrome.driver", PROJECT_PATH + "/src/main/resources/chromedriver");
18 ChromeDriver driver = new ChromeDriver();
19
20 DevTools devTools = driver.getDevTools();
21 devTools.createSession();
22 devTools.send(Emulation.setGeolocationOverride(
23 Optional.of(35.8235),
24 Optional.of(-78.8256),
25 Optional.of(100)));
26 driver.get("https://mycurrentlocation.net/");
27 }
28}
29view raw
1package com.devtools;
2
3import org.openqa.selenium.chrome.ChromeDriver;
4import org.openqa.selenium.devtools.DevTools;
5import java.util.HashMap;
6import java.util.Map;
7
8public class SetDeviceMode {
9
10 final static String PROJECT_PATH = System.getProperty("user.dir");
11
12 public static void main(String[] args){
13 System.setProperty("webdriver.chrome.driver", PROJECT_PATH + "/src/main/resources/chromedriver");
14 ChromeDriver driver;
15 driver = new ChromeDriver();
16
17 DevTools devTools = driver.getDevTools();
18 devTools.createSession();
19 Map deviceMetrics = new HashMap()
20 {{
21 put("width", 600);
22 put("height", 1000);
23 put("mobile", true);
24 put("deviceScaleFactor", 50);
25 }};
26 driver.executeCdpCommand("Emulation.setDeviceMetricsOverride", deviceMetrics);
27 driver.get("https://www.google.com");
28 }
29}
1package com.devtools;
2
3import org.apache.commons.codec.binary.Base64;
4import org.openqa.selenium.By;
5import org.openqa.selenium.chrome.ChromeDriver;
6import org.openqa.selenium.devtools.DevTools;
7import org.openqa.selenium.devtools.network.Network;
8import org.openqa.selenium.devtools.network.model.Headers;
9import java.util.HashMap;
10import java.util.Map;
11import java.util.Optional;
12
13public class SetAuthHeader {
14
15 private static final String USERNAME = "guest";
16 private static final String PASSWORD = "guest";
17 final static String PROJECT_PATH = System.getProperty("user.dir");
18
19 public static void main(String[] args){
20 System.setProperty("webdriver.chrome.driver", PROJECT_PATH + "/src/main/resources/chromedriver");
21 ChromeDriver driver = new ChromeDriver();
22
23 //Create DevTools session and enable Network
24 DevTools chromeDevTools = driver.getDevTools();
25 chromeDevTools.createSession();
26 chromeDevTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
27
28 //Open website
29 driver.get("https://jigsaw.w3.org/HTTP/");
30
31 //Send authorization header
32 Map<String, Object> headers = new HashMap<>();
33 String basicAuth ="Basic " + new String(new Base64().encode(String.format("%s:%s", USERNAME, PASSWORD).getBytes()));
34 headers.put("Authorization", basicAuth);
35 chromeDevTools.send(Network.setExtraHTTPHeaders(new Headers(headers)));
36
37 //Click authentication test - this normally invokes a browser popup if unauthenticated
38 driver.findElement(By.linkText("Basic Authentication test")).click();
39
40 String loginSuccessMsg = driver.findElement(By.tagName("html")).getText();
41 if(loginSuccessMsg.contains("Your browser made it!")){
42 System.out.println("Login successful");
43 }else{
44 System.out.println("Login failed");
45 }
46
47 driver.quit();
48 }
49}