...4import com.codeborne.selenide.WebDriverRunner;5import org.junit.jupiter.api.*;6import org.openqa.selenium.Capabilities;7import org.openqa.selenium.chrome.ChromeDriver;8import org.openqa.selenium.devtools.CdpInfo;9import org.openqa.selenium.devtools.CdpVersionFinder;10import org.openqa.selenium.devtools.Connection;11import org.openqa.selenium.devtools.DevTools;12import org.openqa.selenium.devtools.noop.NoOpCdpInfo;13import org.openqa.selenium.devtools.v91.fetch.Fetch;14import org.openqa.selenium.devtools.v91.fetch.model.HeaderEntry;15import org.openqa.selenium.devtools.v91.network.Network;16import org.openqa.selenium.devtools.v91.network.model.AuthChallengeResponse;17import org.openqa.selenium.devtools.v91.network.model.Request;18import org.openqa.selenium.devtools.v91.network.model.RequestId;19import org.openqa.selenium.devtools.v91.network.model.RequestWillBeSent;20import org.openqa.selenium.devtools.v91.performance.Performance;21import org.openqa.selenium.devtools.v91.performance.model.Metric;22import org.openqa.selenium.remote.RemoteWebDriver;23import org.openqa.selenium.remote.http.ClientConfig;24import org.openqa.selenium.remote.http.HttpClient;25import java.net.MalformedURLException;26import java.net.URI;27import java.net.URISyntaxException;28import java.net.URL;29import java.util.ArrayList;30import java.util.Base64;31import java.util.List;32import java.util.Map;33import java.util.Optional;34import java.util.concurrent.ConcurrentHashMap;35import java.util.concurrent.TimeUnit;36import static java.util.Optional.empty;37import static org.awaitility.Awaitility.await;38import static org.junit.jupiter.api.Assertions.*;39import static com.codeborne.selenide.Condition.attribute;40import static com.codeborne.selenide.Selenide.*;41public class MainPageTest {42 @BeforeAll43 public static void setUpAll() {44 Configuration.browserSize = "1280x800";45 }46 @Test47 public void metricsTest() {48 DevTools devTools = getLocalDevTools();49 devTools.send(Performance.enable(empty()));50 open("https://www.jetbrains.com/");51 List<Metric> send = devTools.send(Performance.getMetrics());52 assertTrue(send.size() > 0);53 send.forEach(it -> System.out.printf("%s: %s%n", it.getName(), it.getValue()));54 }55 @Test56 public void getResponseTest() {57 DevTools devTools = getLocalDevTools();58 devTools.send(Network.enable(empty(), empty(), empty()));59 final List<RequestWillBeSent> requests = new ArrayList<>();60 final List<String> responses = new ArrayList<>();61 devTools.addListener(Network.requestWillBeSent(), req -> {62 if (req.getRequest().getUrl().contains("proxy/services/credit-application/async/api/external/v1/request/parameters")) {63 requests.add(req);64 }65 });66 devTools.addListener(Network.responseReceived(),67 entry -> {68 if (requests.size() == 0) {69 return;70 }71 RequestWillBeSent request = requests.get(0);72 if (request != null && entry.getRequestId().toString().equals(request.getRequestId().toString())) {73 Network.GetResponseBodyResponse send = devTools.send(Network.getResponseBody(request.getRequestId()));74 responses.add(send.getBody());75 }76 });77 open("https://www.sberbank.ru/ru/person/credits/money/consumer_unsecured/zayavka");78 await().pollThread(Thread::new)79 .atMost(10, TimeUnit.SECONDS)80 .until(() -> responses.size() == 1);81 System.out.println(responses.get(0));82 }83 @Test84 public void interceptRequestTest() {85 DevTools devTools = getLocalDevTools();86 devTools.send(Fetch.enable(empty(), empty()));87 String content = "[{\"title\":\"Todo 1\",\"order\":null,\"completed\":false},{\"title\":\"Todo 2\",\"order\":null,\"completed\":true}]";88 devTools.addListener(Fetch.requestPaused(), request -> {89 String url = request.getRequest().getUrl();90 String query = getUrl(url);91 if (url.contains("/todos/") && query == null) {92 List<HeaderEntry> corsHeaders = new ArrayList<>();93 corsHeaders.add(new HeaderEntry("Access-Control-Allow-Origin", "*"));94 corsHeaders.add(new HeaderEntry("Access-Control-Allow-Methods", "GET, POST, OPTIONS, DELETE"));95 devTools.send(Fetch.fulfillRequest(96 request.getRequestId(),97 200,98 Optional.of(corsHeaders),99 Optional.empty(),100 Optional.of(Base64.getEncoder().encodeToString(content.getBytes())),101 Optional.of("OK"))102 );103 } else {104 devTools.send(Fetch.continueRequest(105 request.getRequestId(),106 Optional.of(url),107 Optional.of(request.getRequest().getMethod()),108 request.getRequest().getPostData(),109 request.getResponseHeaders()));110 }111 });112 open("https://todobackend.com/client/index.html?https://todo-backend-spring4-java8.herokuapp.com/todos/");113 $$("#todo-list li").shouldHave(CollectionCondition.size(2));114 $$("#todo-list label").shouldHave(CollectionCondition.texts("Todo 1", "Todo 2"));115 }116 private String getUrl(String url) {117 try {118 return new URL(url).getQuery();119 } catch (MalformedURLException e) {120 throw new RuntimeException(e.getMessage(), e);121 }122 }123 private DevTools getLocalDevTools() {124 open();125 ChromeDriver driver = (ChromeDriver) WebDriverRunner.getWebDriver();126 DevTools devTools = driver.getDevTools();127 devTools.createSession();128 return devTools;129 }130 @Test131 public void devtoolsWithSelenoid() throws URISyntaxException {132 Configuration.remote = "http://localhost:4444/wd/hub";133 open();134 RemoteWebDriver webDriver = (RemoteWebDriver) WebDriverRunner.getWebDriver();135 Capabilities capabilities = webDriver.getCapabilities();136 CdpInfo cdpInfo = new CdpVersionFinder()137 .match(capabilities.getBrowserVersion())138 .orElseGet(NoOpCdpInfo::new);139 HttpClient.Factory factory = HttpClient.Factory.createDefault();140 URI uri = new URI(String.format("ws://localhost:4444/devtools/%s", webDriver.getSessionId()));141 Connection connection = new Connection(142 factory.createClient(ClientConfig.defaultConfig().baseUri(uri)),143 uri.toString());144 DevTools devTools = new DevTools(cdpInfo::getDomains, connection);145 devTools.createSession();146 devTools.send(Performance.enable(empty()));147 open("https://www.jetbrains.com/");148 List<Metric> send = devTools.send(Performance.getMetrics());149 assertTrue(send.size() > 0);150 send.forEach(it -> System.out.printf("%s: %s%n", it.getName(), it.getValue()));151 }152}...