Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion store_network_logs_data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.testsigma.addons</groupId>
<artifactId>store_network_logs_data</artifactId>
<version>1.0.2</version>
<version>1.0.4</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.testsigma.addons.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.testsigma.sdk.ApplicationType;
import com.testsigma.sdk.Result;
import com.testsigma.sdk.WebAction;
Expand All @@ -12,14 +10,10 @@
import lombok.Data;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.testsigma.addons.web.utilities.ResponseDataUtilities.getSpecificHeaderValue;


import static com.testsigma.addons.web.utilities.ResponseDataUtilities.getRequestBody;

@Data
@Action(actionText = "get value of the request header from the attribute header_key " +
"and store the value in runtime variable variable_name",
Expand Down Expand Up @@ -51,90 +45,34 @@ public Result execute() {
logger.info("Execution started for action: GetDataFromrequestBody");

try {
// Step 1: Robustly fetch the request body
logger.info("Fetching request body for test case result ID: " + testCaseResult.getId());
String requestBody;
// Step 1: Get the specific header value
logger.info("Getting header value for key: " + headerKey.getValue().toString() + " from test case result ID: " + testCaseResult.getId());
String headerValue;
try {
requestBody = getRequestBody(testCaseResult.getId(), logger);
headerValue = getSpecificHeaderValue(testCaseResult.getId(), headerKey.getValue().toString(), logger);
} catch (IllegalStateException e) {
throw new Exception("Failed to retrieve request body. The API response from" +
" the preceding step might be missing or empty.", e);
throw new Exception("Failed to retrieve header value. The network request from the preceding step might be missing or empty.", e);
}

if (requestBody == null || requestBody.trim().isEmpty()) {
throw new Exception("Retrieved request body is empty. Cannot search for attributes.");
if (headerValue == null || headerValue.trim().isEmpty()) {
throw new Exception("Retrieved header value is empty. Cannot proceed.");
}
logger.info("request body successfully retrieved.");

// Step 2: Parse the entire JSON into a generic object to handle any structure
logger.info("Parsing request body JSON...");
ObjectMapper mapper = new ObjectMapper();
Object parsedJson;
try {
parsedJson = mapper.readValue(requestBody, Object.class);
} catch (JsonProcessingException e) {
logger.info("request body is not a valid JSON. Error: " + e.getMessage());
throw new Exception("request body is not a valid JSON. Error: " + e.getMessage());
}

// Step 3: Get parameters
String attributeKey = headerKey.getValue().toString();

// Step 4: Find all occurrences of the attribute using recursion
List<Object> foundValues = new ArrayList<>();
findAttributeRecursively(parsedJson, attributeKey, foundValues);

// Step 5: Validate the result and get the desired occurrence
if (foundValues.isEmpty()) {
throw new Exception("Attribute '" + attributeKey + "' was not found anywhere in the request body.");
}

// Step 6: Convert the found value to a String and store it
Object rawValue = foundValues.get(0);
String valueToStore = (rawValue == null) ? "null" : rawValue.toString();
logger.info("Header value successfully retrieved: " + headerValue);

// Step 2: Store the header value in runtime data
runTimeData.setKey(variableName.getValue().toString());
runTimeData.setValue(valueToStore);
runTimeData.setValue(headerValue);

logger.info("Successfully extracted header value and stored it in runtime variable: " + runTimeData.getValue());
setSuccessMessage("Header value fetched and stored in runtime variable: " + runTimeData.getValue());
logger.info("Successfully extracted header value and stored it in runtime variable: " + runTimeData.getValue().toString());
setSuccessMessage("Header value fetched and stored in runtime variable: " + runTimeData.getValue().toString());

} catch (Exception e) {
logger.warn("Exception occurred during execution: " + ExceptionUtils.getStackTrace(e));
setErrorMessage("Exception occurred while fetching data from request body: " + e.getMessage());
setErrorMessage("Exception occurred while fetching data from request headers: " + e.getMessage());
return Result.FAILED;
}
return Result.SUCCESS;

}

/**
* Recursively traverses any JSON structure (Map or List) and collects all values for a given key.
*/
private void findAttributeRecursively(Object obj, String key, List<Object> found) {
if (obj == null) {
return;
}

if (obj instanceof Map) {
// It's a JSON object, check its keys
Map<?, ?> map = (Map<?, ?>) obj;
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (key.equals(entry.getKey())) {
found.add(entry.getValue());
}
// Continue searching in the value, which could be another Map or List
findAttributeRecursively(entry.getValue(), key, found);
}
} else if (obj instanceof List) {
// It's a JSON array, iterate through its items
List<?> list = (List<?>) obj;
for (Object item : list) {
findAttributeRecursively(item, key, found);
}
}
}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.testsigma.addons.web;


import com.testsigma.sdk.Result;
import com.testsigma.sdk.WebAction;
import com.testsigma.sdk.annotation.Action;
import com.testsigma.sdk.annotation.RunTimeData;
import com.testsigma.sdk.annotation.TestCaseResult;
import com.testsigma.sdk.annotation.TestData;
import lombok.Data;

import static com.testsigma.addons.web.utilities.ResponseDataUtilities.getStatusCodeData;

@Data
@Action(actionText = "get the status code of the stored URL and store it in runtime variable status_code",
description = "This action retrieves the status code of the stored URL.",
applicationType = com.testsigma.sdk.ApplicationType.WEB)
public class GetStatusCode extends WebAction {

@TestCaseResult
private com.testsigma.sdk.TestCaseResult testCaseResult;
@TestData(reference = "status_code", isRuntimeVariable = true)
private com.testsigma.sdk.TestData statusCodeVariable;

@RunTimeData
private com.testsigma.sdk.RunTimeData runTimeData;

@Override
public Result execute() {
try {
Result result = Result.SUCCESS;
int statusCodeFromFile = getStatusCodeData(testCaseResult.getId(), logger);
if (statusCodeFromFile == -1) {
setErrorMessage("Status code not found in the file.");
return Result.FAILED;
}
logger.info("Status code retrieved: " + statusCodeFromFile);
// Store the status code in the runtime variable
runTimeData.setKey(statusCodeVariable.getValue().toString());
runTimeData.setValue(String.valueOf(statusCodeFromFile));
setSuccessMessage("Status code retrieved successfully: " + statusCodeFromFile);
return Result.SUCCESS;
} catch (Exception e) {
logger.debug("Error retrieving status code: " + e.getMessage());
setErrorMessage("Failed to retrieve status code: " + e.getMessage());
return Result.FAILED;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.testsigma.addons.web;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.testsigma.sdk.ApplicationType;
import com.testsigma.sdk.Result;
import com.testsigma.sdk.WebAction;
Expand All @@ -16,11 +18,11 @@

import java.util.Optional;

import static com.testsigma.addons.web.utilities.ResponseDataUtilities.addRequestBodyData;
import static com.testsigma.addons.web.utilities.ResponseDataUtilities.addResponseBodyData;
import static com.testsigma.addons.web.utilities.ResponseDataUtilities.saveAllNetworkData;

@Data
@Action(actionText = "Add Network Listener to find the response for the request url url_value and the request method method_value",
@Action(actionText = "Add Network Listener to find the response for the request url url_value" +
" and the request method method_value",
description = "Add Network Listener to find the response for the given request url and given request method",
applicationType = ApplicationType.WEB)
public class StartTracking extends WebAction {
Expand Down Expand Up @@ -51,21 +53,51 @@ public Result execute() {

// Enable network interception with high buffer size
logger.info("Enabling network interception...");
devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.of(100000000)));
devTool.send(Network.enable(Optional.empty(),
Optional.empty(), Optional.of(100000000)));

final RequestId[] requestIds = new RequestId[1];
final String[] capturedRequestHeaders = new String[1];

// Listener to intercept network requests
logger.info("Adding listener for network requests...");
devTool.addListener(Network.requestWillBeSent(), request -> {
String requestUrl = request.getRequest().getUrl();

request.getRequest().getHeaders();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove or complete the incomplete statement.

Line 67 contains an incomplete statement request.getRequest().getHeaders(); that doesn't assign the result to any variable or perform any action. This appears to be leftover code from refactoring.

-                request.getRequest().getHeaders();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
request.getRequest().getHeaders();
// (The redundant call to request.getRequest().getHeaders() has been removed)
🤖 Prompt for AI Agents
In
store_network_logs_data/src/main/java/com/testsigma/addons/web/StartTracking.java
at line 67, the statement `request.getRequest().getHeaders();` is incomplete and
unused. Remove this line entirely or assign its result to a variable if headers
are needed later; otherwise, deleting it will clean up leftover code from
refactoring.

logger.info("Intercepted request URL: " + requestUrl);

// Check if the URL and method match the specified criteria
if (requestUrl.contains(urlValue.getValue().toString()) &&
request.getRequest().getMethod().equalsIgnoreCase(methodValue.getValue().toString())) {
logger.info("Matching request found with URL: " + requestUrl + " and method: " + methodValue.getValue());
logger.info("Matching request found with URL: " + requestUrl +
" and method: " + methodValue.getValue());
requestIds[0] = request.getRequestId();
// Capture request headers from the request event and format them properly
StringBuilder headersBuilder = new StringBuilder();
logger.info("Capturing headers for request: " + request.getRequestId());
logger.info("Headers map: " + request.getRequest().getHeaders());

if (request.getRequest().getHeaders() != null && !request.getRequest().getHeaders().isEmpty()) {
request.getRequest().getHeaders().forEach((key, value) -> {
logger.info("Header - " + key + ": " + value);
if (headersBuilder.length() > 0) {
headersBuilder.append("\n");
}
headersBuilder.append(key).append(": ").append(value != null ? value.toString() : "");
});
} else {
logger.warn("No headers found in request. Trying alternative approach...");
// Fallback: try to get headers from the request object directly
if (request.getRequest().getUrl() != null) {
headersBuilder.append(":method: ").append(request.getRequest().getMethod()).append("\n");
headersBuilder.append(":path: ").append(request.getRequest().getUrl()).append("\n");
headersBuilder.append(":scheme: https\n");
}
}

capturedRequestHeaders[0] = headersBuilder.toString();
logger.info("Captured headers string: " + capturedRequestHeaders[0]);
}
});

Expand All @@ -78,13 +110,31 @@ public Result execute() {
try {
// Retrieve the response body using the captured RequestId
String responseBody = devTool.send(Network.getResponseBody(requestIds[0])).getBody();
logger.info("Storing response body in test case result...");
addResponseBodyData(testCaseResult.getId(), responseBody, logger);
logger.info("Response body successfully stored.");
String requestBody = devTool.send(Network.getRequestPostData(requestIds[0]));
logger.info("Request body successfully retrieved and will be stored.");
addRequestBodyData(testCaseResult.getId(), requestBody, logger);
logger.info("Request body successfully stored.");
int status = response.getResponse().getStatus();
response.getResponse().getHeaders().forEach((key, value) -> {
logger.info("Response Header - " + key + ": " + value);
});
logger.info("Response status: " + status);
logger.info("Storing all network data together...");
String requestHeaders = capturedRequestHeaders[0] != null ? capturedRequestHeaders[0] : "";
logger.info("Request headers to store: " + requestHeaders);
logger.info("Response body to store: " + responseBody);

// Store all data together in one operation
JsonObject allData = new JsonObject();
allData.addProperty("statusCode", status);

JsonArray headersArray = new JsonArray();
headersArray.add(requestHeaders);
allData.add("requestHeaders", headersArray);

JsonArray responseArray = new JsonArray();
responseArray.add(responseBody);
allData.add("responseBody", responseArray);

// Save all data at once
saveAllNetworkData(testCaseResult.getId(), allData, logger);
logger.info("All network data stored together successfully.");
} catch (Exception e) {
logger.warn("Error while storing response body: " + ExceptionUtils.getStackTrace(e));
}
Expand All @@ -94,7 +144,8 @@ public Result execute() {
} catch (Exception e) {
// Log the exception details and set the error message
logger.warn("Exception occurred during execution: " + ExceptionUtils.getStackTrace(e));
setErrorMessage("Exception occurred while adding Network Response Listener to the driver: " + e.getMessage());
setErrorMessage("Exception occurred while adding Network Response Listener" +
" to the driver: " + e.getMessage());
return Result.FAILED;
}

Expand Down
Loading