I tried the below Groovy code JSR233 Post Procosser , It's reaching to the file path that I ensured by entering the manual text and it's clearing it, but after that it's not writing the extracted values into the file and doesn't throws any error
// Define the path to the CSV file
def csvFilePath = 'C:/xxxxxxxxxxx/testdata/login_landing_access-token_data.csv'
// Clear the contents of the CSV file without deleting the header
def csvFile = new File(csvFilePath)
if (csvFile.exists()) {
def csvWriter = new FileWriter(csvFilePath)
def header = 'captured_access_token,captured_userid,captured_user_emailaddress,captured_login_responsebody\n'
csvWriter.append(header)
csvWriter.flush()
csvWriter.close()
}
// Write extracted values for the current thread only on the first iteration
if (ctx.getThreadNum() == 0 && ctx.getVariables().getIteration() == 1) {
def capturedToken = vars.get('captured_access_token')
def capturedUserid = vars.get('captured_userid')
def capturedEmailaddress = vars.get('captured_user_emailaddress')
def capturedResponseBody = vars.get('captured_login_responsebody')
// Write to CSV file
def csvWriter = new FileWriter(csvFilePath, true)
csvWriter.append("${capturedToken},${capturedUserid},${capturedEmailaddress},${capturedResponseBody}\n")
csvWriter.flush()
csvWriter.close()
}
Also tired the Below code Delete the file & Recreate before Test starts
try {
// Define the path to the CSV file
def csvFilePath = "C:/xxxxxxxxx/testdata/login_landing_access-token_data.csv"
// Delete the file if it exists
def csvFile = new File(csvFilePath)
if (csvFile.exists()) {
csvFile.delete()
}
// Recreate the file if it doesn't exist
if (!csvFile.exists()) {
csvFile.createNewFile()
def csvWriter = new FileWriter(csvFilePath, true)
csvWriter.append("captured_access_token,captured_userid,captured_user_emailaddress,captured_login_responsebody\n")
csvWriter.close()
}
// Write extracted values including headers for the current thread only on the first iteration
if (ctx.getThreadNum() == 0 && ctx.getVariables().getIteration() == 1) {
def capturedToken = vars.get("captured_access_token")
def capturedUserid = vars.get("captured_userid")
def capturedEmailaddress = vars.get("captured_user_emailaddress")
def capturedResponseBody = vars.get("captured_login_responsebody")
// Write to CSV file
def csvWriter = new FileWriter(csvFilePath, true)
csvWriter.append("${capturedToken},${capturedUserid},${capturedEmailaddress},${capturedResponseBody}\n")
csvWriter.flush() // Flush the writer to ensure the data is written immediately
csvWriter.close()
}
} catch (Exception e) {
log.error("Error occurred while writing to CSV file: ${e.message}")
}
I am able to Write the Extracted values into the CSV successfully using Bean Shell Post Processor