You can retrieve data from DevTools during test execution by using static methods that organize the data in a structured way, making it easier to fetch and continue with your tests. Below are the available methods for fetching different types of data.
You have to enable it by adding the following to your application.properties:
enableDevTools=true
Methods Overview:
1. Request Data:
-
Get the last request:
Request req = DevtoolsCDP.getLastRequest(driver);Retrieves the most recent HTTP request made during the test.
-
Get all requests:
List<Request> listReq = DevtoolsCDP.getAllResponses(driver);Retrieves a list of all HTTP requests made during the test.
-
Get the last N requests:
List<Request> listReq = DevtoolsCDP.getNumOfLastRequests(driver, 5);Retrieves the last N requests (e.g., the 5 most recent).
2. Response Data:
-
Get the last response:
ResponseReceived res = DevtoolsCDP.getLastResponse(driver);Retrieves the most recent HTTP response received during the test.
-
Get all responses:
List<ResponseReceived> listRes = Devtools.getAllResponses(driver);Retrieves a list of all HTTP responses received during the test.
-
Get the last N responses:
List<ResponseReceived> listRes = DevtoolsCDP.getNumOfLastResponses(driver, 5);Retrieves the last N responses (e.g., the 5 most recent).
3. Console Logs:
-
Get the last console log:
String str = DevtoolsCDP.getLastConsoleLog(driver);Retrieves the most recent log entry from the browser's console.
-
Get all console logs:
List<String> consoleLogList = DevtoolsCDP.getAllConsoleLogs(driver);Retrieves a list of all console log entries recorded during the test.
-
Get the last N console logs:
List<String> consoleLogList = DevtoolsCDP.getNumOfLastConsoleLogs(driver, 5);Retrieves the last N console log entries (e.g., the 5 most recent).
Example Usage
Here’s an example of how you might use these methods to fetch request, response, and console log data during a test:
// Get all responses
List<ResponseReceived> allResponses = DevtoolsCDP.getAllResponses(driver);
// Filter responses with a 200 OK status code
for (ResponseReceived response : allResponses) {
if (response.getStatus() == 200) {
System.out.println("Successful Response URL: " + response.getUrl());
System.out.println("Response Status: " + response.getStatus());
// Manipulate the response data, e.g., check response body content
String responseBody = response.getBody();
if (responseBody.contains("success")) {
System.out.println("Response body contains 'success'.");
}
}
}
Conclusion
These methods provide an efficient way to retrieve and organize data from DevTools, allowing you to easily interact with requests, responses, and console logs during test execution.