Skip to content

Commit cd82619

Browse files
Merge pull request #465 from telekom/feature/cdp-demos
Feature/cdp demos
2 parents c3e24e1 + af3de2b commit cd82619

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

docs/src/docs/selenium4/selenium4-cdp.adoc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,67 @@ devTools.send(Emulation.setDeviceMetricsOverride(...);
297297
----
298298

299299
See here for more details: https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#method-setDeviceMetricsOverride
300+
301+
== Manipulate browser requests
302+
303+
Change the web requests of your browser:
304+
305+
[source, java]
306+
----
307+
308+
public class ChromeDevToolsTests extends TesterraTest implements
309+
ChromeDevToolsProvider,
310+
UiElementFinderFactoryProvider,
311+
WebDriverManagerProvider {
312+
313+
// https://weatherstack.com/ uses your client IP address to find out your location.
314+
// There is a REST api call to https://weatherstack.com/ws_api.php?ip=<ip> to get
315+
// the local weather information.
316+
// This test updates the REST api call with a static public IP address
317+
@Test
318+
public void testCDP_Network_changeRequest() {
319+
WebDriver webDriver = WEB_DRIVER_MANAGER.getWebDriver();
320+
DevTools rawDevTools = CHROME_DEV_TOOLS.getRawDevTools(webDriver);
321+
final String location1 = "213.136.89.121"; // free German proxy server in Munich
322+
323+
rawDevTools.send(Fetch.enable(Optional.empty(), Optional.empty()));
324+
rawDevTools.addListener(Fetch.requestPaused(), requestConsumer -> {
325+
Request request = requestConsumer.getRequest();
326+
String currentUrl = request.getUrl();
327+
if (currentUrl.contains("ws_api.php?ip=")) {
328+
String updatedUrl = currentUrl.substring(0, currentUrl.indexOf("?"))
329+
+ "?ip=" + location1;
330+
rawDevTools.send(
331+
Fetch.continueRequest(
332+
requestConsumer.getRequestId(),
333+
Optional.of(updatedUrl),
334+
Optional.empty(),
335+
Optional.empty(),
336+
Optional.empty(),
337+
Optional.empty()));
338+
} else {
339+
// All other requests will be sent without any change
340+
rawDevTools.send(
341+
Fetch.continueRequest(
342+
requestConsumer.getRequestId(),
343+
Optional.of(currentUrl),
344+
Optional.empty(),
345+
Optional.empty(),
346+
Optional.empty(),
347+
Optional.empty()));
348+
349+
}
350+
351+
});
352+
353+
354+
webDriver.get("https://weatherstack.com/");
355+
356+
UiElementFinder uiElementFinder = UI_ELEMENT_FINDER_FACTORY.create(webDriver);
357+
uiElementFinder.find(By.xpath("//div[@id = 'cookiescript_accept']")).click();
358+
UiElement weatherLocation = uiElementFinder
359+
.find(By.xpath("//span[@data-api = 'location']"));
360+
weatherLocation.assertThat().text().isContaining("Munich");
361+
}
362+
}
363+
----

integration-tests/src/test/java/eu/tsystems/mms/tic/testframework/playground/ChromeDevToolsTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import eu.tsystems.mms.tic.testframework.AbstractWebDriverTest;
2424
import eu.tsystems.mms.tic.testframework.constants.Browsers;
25+
import eu.tsystems.mms.tic.testframework.pageobjects.UiElement;
2526
import eu.tsystems.mms.tic.testframework.pageobjects.UiElementFinder;
2627
import eu.tsystems.mms.tic.testframework.testing.ChromeDevToolsProvider;
2728
import eu.tsystems.mms.tic.testframework.utils.TimerUtils;
@@ -37,9 +38,11 @@
3738
import org.openqa.selenium.devtools.HasDevTools;
3839
import org.openqa.selenium.devtools.events.ConsoleEvent;
3940
import org.openqa.selenium.devtools.v130.emulation.Emulation;
41+
import org.openqa.selenium.devtools.v130.fetch.Fetch;
4042
import org.openqa.selenium.devtools.v130.log.Log;
4143
import org.openqa.selenium.devtools.v130.log.model.LogEntry;
4244
import org.openqa.selenium.devtools.v130.network.Network;
45+
import org.openqa.selenium.devtools.v130.network.model.Request;
4346
import org.openqa.selenium.devtools.v130.network.model.RequestWillBeSent;
4447
import org.openqa.selenium.devtools.v130.network.model.ResponseReceived;
4548
import org.openqa.selenium.logging.HasLogEvents;
@@ -348,4 +351,52 @@ public void testT14_MobileDeviceEmulation() {
348351

349352
}
350353

354+
/**
355+
* This test calls the page https://weatherstack.com/ which uses your local IP address to show your local weather.
356+
* With the help of the Request fetcher you can modify the request to change to IP address.
357+
*
358+
* See details at https://chromedevtools.github.io/devtools-protocol/tot/Fetch/
359+
*/
360+
@Test
361+
public void testT15_Network_changeRequest() {
362+
WebDriver webDriver = WEB_DRIVER_MANAGER.getWebDriver();
363+
DevTools rawDevTools = CHROME_DEV_TOOLS.getRawDevTools(webDriver);
364+
final String location1 = "213.136.89.121"; // free German proxy server in Munich
365+
366+
rawDevTools.send(Fetch.enable(Optional.empty(), Optional.empty()));
367+
rawDevTools.addListener(Fetch.requestPaused(), requestConsumer -> {
368+
Request request = requestConsumer.getRequest();
369+
String currentUrl = request.getUrl();
370+
if (currentUrl.contains("ws_api.php?ip=")) {
371+
String updatedUrl = currentUrl.substring(0, currentUrl.indexOf("?")) + "?ip=" + location1;
372+
rawDevTools.send(
373+
Fetch.continueRequest(
374+
requestConsumer.getRequestId(),
375+
Optional.of(updatedUrl),
376+
Optional.empty(),
377+
Optional.empty(),
378+
Optional.empty(),
379+
Optional.empty()));
380+
} else {
381+
rawDevTools.send(
382+
Fetch.continueRequest(
383+
requestConsumer.getRequestId(),
384+
Optional.of(currentUrl),
385+
Optional.empty(),
386+
Optional.empty(),
387+
Optional.empty(),
388+
Optional.empty()));
389+
390+
}
391+
392+
});
393+
394+
webDriver.get("https://weatherstack.com/");
395+
396+
UiElementFinder uiElementFinder = UI_ELEMENT_FINDER_FACTORY.create(webDriver);
397+
uiElementFinder.find(By.xpath("//div[@id = 'cookiescript_accept']")).click();
398+
UiElement weatherLocation = uiElementFinder.find(By.xpath("//span[@data-api = 'location']"));
399+
weatherLocation.assertThat().text().isContaining("Munich");
400+
}
401+
351402
}

0 commit comments

Comments
 (0)