0

コンソールでステータス コードを表示でき、GET リクエストのレスポンス ボディを取得できます(以下は Selenium4 を使用したコードです)。

JSを使用してPostmanで行うのと同じように、bodyで変数を定義してSelenium 4でPOSTリクエストを送信する方法は?

目標は、Selenium 4 を使用して API チェーンを実現することです。

package selenium4Practice;
import java.util.Optional;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v96.network.model.Request;
import org.openqa.selenium.devtools.v96.network.model.RequestId;
import org.openqa.selenium.devtools.v96.network.Network;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ChromeDevLogResponse {

    public static void main(String[] args) {
        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        DevTools devTools = driver.getDevTools();
        devTools.createSession();
        // enable network tracking
        RequestId[] requestIds = new RequestId[1];
        
        devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
        // adding listener to listen the response when Event fired when request is sent
        devTools.addListener(Network.requestWillBeSent(), request -> {
            Request req = request.getRequest();
            System.out.println("Request URL " + req.getUrl());
            requestIds[0] = request.getRequestId();
        });
        // adding listener to listen the response when Event fired when response is
        // received
        // response is - response object received
        devTools.addListener(Network.responseReceived(), response -> {
//          Response res = response.getResponse();
            System.out.println("Response URL " + response.getResponse().getUrl());
            int status = response.getResponse().getStatus();
            System.out.println("Status code is " + status);
            String headers = response.getResponse().getHeaders().toString();
            System.out.println("Response Headers " + headers);
            System.out.println("Response Time is " + response.getResponse().getResponseTime());
            String type = response.getType().toJson();
            String responseBody = devTools.send(Network.getResponseBody(requestIds[0])).getBody();
            System.out.println("Response Body: " + responseBody);
            if (response.getResponse().getStatus().toString().startsWith("4")) {
                System.out.println(response.getResponse().getUrl() + " is failing with status code "
                        + response.getResponse().getStatus());
            }
        });
        driver.get("https://reqres.in/api/users");
    }
}
4

0 に答える 0