Skip to content

Commit 52ca11a

Browse files
committed
Added logic to support not clearing allure results if not needed. Added Retry logger to track flaky tests in csv file
1 parent 1220319 commit 52ca11a

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
lines changed

src/main/java/custom_framework/utils/ExtentReporting.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import com.aventstack.extentreports.ExtentReports;
44
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
55

6+
import java.util.Properties;
7+
68
public class ExtentReporting {
79
static ExtentReports extent;
810

911
public static ExtentReports getReportObject() {
1012
String path = System.getProperty("user.dir") + "\\reports\\index.html";
13+
Properties settings = Settings.load("settings.properties");
14+
String browser = settings.getProperty("browser");
15+
String environment = settings.getProperty("environment");
1116
ExtentSparkReporter reporter = new ExtentSparkReporter(path);
1217

1318
reporter.config().setReportName("Custom Framework Report Name");
@@ -18,6 +23,10 @@ public static ExtentReports getReportObject() {
1823
extent.attachReporter(reporter);
1924
extent.setSystemInfo("Framework creator", "Nikolina Djekic");
2025
extent.setSystemInfo("Linkedin", "https://www.linkedin.com/in/nikolina-djekic/");
26+
extent.setSystemInfo("Browser", System.getProperty("browser", browser));
27+
extent.setSystemInfo("Environment", System.getProperty("environment", environment));
28+
extent.setSystemInfo("Java Version", System.getProperty("java.version"));
29+
extent.setSystemInfo("OS", System.getProperty("os.name"));
2130
return extent;
2231
}
2332

src/main/java/custom_framework/utils/Listeners.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
import java.io.FileInputStream;
1212
import java.io.IOException;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
import java.util.Comparator;
17+
import java.util.Properties;
1318

1419
public class Listeners extends SharedMethods implements ITestListener {
1520

@@ -19,7 +24,8 @@ public class Listeners extends SharedMethods implements ITestListener {
1924

2025
@Override
2126
public void onTestStart(ITestResult result) {
22-
test = extent.createTest(result.getMethod().getMethodName());
27+
String testName = result.getTestClass().getName() + " - " + result.getMethod().getMethodName();
28+
test = extent.createTest(testName);
2329
extentTest.set(test); // to support parallel execution
2430
}
2531

@@ -54,10 +60,47 @@ public void onTestSkipped(ITestResult result) {
5460
}
5561
}
5662

63+
@Override
64+
public void onStart(ITestContext context) {
65+
if (shouldClearAllureResults()) {
66+
clearAllureResultsDirectory();
67+
}
68+
}
69+
5770
@Override
5871
public void onFinish(ITestContext context) {
5972
extent.flush();
6073
extentTest.remove();
6174
}
6275

76+
private boolean shouldClearAllureResults() {
77+
Properties properties = new Properties();
78+
try {
79+
FileInputStream fileInputStream = new FileInputStream("src/main/resources/settings.properties");
80+
properties.load(fileInputStream);
81+
return Boolean.parseBoolean(properties.getProperty("clear.allure.results"));
82+
} catch (IOException e) {
83+
e.printStackTrace();
84+
}
85+
return false; // Default to not clearing if the property cannot be read
86+
}
87+
88+
private void clearAllureResultsDirectory() {
89+
Path allureResultsPath = Paths.get("target/allure-results");
90+
if (Files.exists(allureResultsPath)) {
91+
try (var paths = Files.walk(allureResultsPath)) {
92+
paths.sorted(Comparator.reverseOrder())
93+
.forEach(path -> {
94+
try {
95+
Files.delete(path);
96+
} catch (IOException e) {
97+
log.warn("Failed to delete file: {}", path.toAbsolutePath(), e);
98+
}
99+
});
100+
} catch (IOException e) {
101+
log.error("Failed to clear allure results directory", e);
102+
}
103+
}
104+
}
105+
63106
}

src/main/java/custom_framework/utils/Retry.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
import org.testng.IRetryAnalyzer;
44
import org.testng.ITestResult;
55

6+
import java.util.HashMap;
7+
import java.util.Map;
8+
69
public class Retry implements IRetryAnalyzer {
710

811
private int count = 0;
912
private static final int NUMBER_OF_TRIES = 1;
13+
private static Map<String, Integer> retryCountMap = new HashMap<>(); // To store retry counts
1014

1115
@Override
1216
public boolean retry(ITestResult iTestResult) {
1317
if (!iTestResult.isSuccess()) { //Check if test not succeed
1418
if (count < NUMBER_OF_TRIES) { //Check if NUMBER_OF_TRIES count is reached
1519
count++; //Increase the maxTry count by 1
1620
iTestResult.setStatus(ITestResult.FAILURE); //Mark test as failed
21+
trackRetry(iTestResult);
1722
return true; //Tells TestNG to re-run the test
1823
} else {
1924
iTestResult.setStatus(ITestResult.FAILURE); //If maxCount reached,test marked as failed
@@ -23,4 +28,15 @@ public boolean retry(ITestResult iTestResult) {
2328
}
2429
return false;
2530
}
31+
32+
private void trackRetry(ITestResult result) {
33+
String testName = result.getMethod().getMethodName();
34+
int retries = retryCountMap.getOrDefault(testName, 0) + 1;
35+
retryCountMap.put(testName, retries);
36+
}
37+
38+
public static Map<String, Integer> getRetryCountMap() {
39+
return retryCountMap;
40+
}
41+
2642
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package custom_framework.utils;
2+
3+
import java.io.*;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
public class RetryLogger {
10+
11+
private static final String LOG_FILE_PATH = "target/retry-log.csv";
12+
13+
// Read the existing retry data from the CSV file
14+
public static Map<String, Integer> readExistingRetryData() {
15+
Map<String, Integer> existingData = new HashMap<>();
16+
if (Files.exists(Paths.get(LOG_FILE_PATH))) {
17+
try (BufferedReader reader = new BufferedReader(new FileReader(LOG_FILE_PATH))) {
18+
String line;
19+
while ((line = reader.readLine()) != null) {
20+
String[] split = line.split(",");
21+
if (split.length == 2) {
22+
String testName = split[0].trim().replace("Test: ", "");
23+
int retryCount = Integer.parseInt(split[1].trim().replace("Retries: ", ""));
24+
existingData.put(testName, retryCount);
25+
}
26+
}
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
return existingData;
32+
}
33+
34+
// Log retry data, updating existing entries if needed
35+
public static void logRetryData(Map<String, Integer> retryCountMap) {
36+
Map<String, Integer> existingRetryData = readExistingRetryData();
37+
38+
// Update retry data
39+
for (Map.Entry<String, Integer> entry : retryCountMap.entrySet()) {
40+
String testName = entry.getKey();
41+
int newRetryCount = entry.getValue();
42+
43+
// If the test already exists in the CSV, update the retry count
44+
existingRetryData.put(testName, existingRetryData.getOrDefault(testName, 0) + newRetryCount);
45+
}
46+
47+
// Write updated data back to the CSV
48+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(LOG_FILE_PATH))) {
49+
for (Map.Entry<String, Integer> entry : existingRetryData.entrySet()) {
50+
writer.write("Test: " + entry.getKey() + ", Retries: " + entry.getValue());
51+
writer.newLine();
52+
}
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
}
56+
}
57+
58+
}

src/main/resources/settings.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ environment=TEST
3030
timeout=10
3131
system=theinternet
3232
#system=webdriveruniversity
33+
clear.allure.results=false
3334
remoteUrl=localhost
3435
#
3536
# ==================== WHAT ELSE COULD BE ADDED ====================

0 commit comments

Comments
 (0)