0% found this document useful (0 votes)
12 views4 pages

Core Analyzer

Uploaded by

Rahul Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Core Analyzer

Uploaded by

Rahul Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.test.

analyzer;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.InvalidSelectorException;
import java.util.*;

public class TestFailureAnalyzer {

public static class AnalysisResult {


private final String rootCause;
private final String recommendation;
private final Map<String, String> debugInfo;
private final double confidenceScore;

public AnalysisResult(String rootCause, String recommendation,


Map<String, String> debugInfo, double confidenceScore) {
this.rootCause = rootCause;
this.recommendation = recommendation;
this.debugInfo = debugInfo;
this.confidenceScore = confidenceScore;
}

// Getters
public String getRootCause() { return rootCause; }
public String getRecommendation() { return recommendation; }
public Map<String, String> getDebugInfo() { return debugInfo; }
public double getConfidenceScore() { return confidenceScore; }
}

public AnalysisResult analyzeFailure(Throwable exception, WebDriver driver) {


// Collect context information
Map<String, String> debugInfo = collectDebugInfo(exception, driver);

// Analyze the exception type and stack trace


String rootCause = determineRootCause(exception);
String recommendation = generateRecommendation(exception, debugInfo);
double confidenceScore = calculateConfidenceScore(exception, debugInfo);

return new AnalysisResult(rootCause, recommendation, debugInfo, confidenceScore);


}

private Map<String, String> collectDebugInfo(Throwable exception, WebDriver driver) {


Map<String, String> debugInfo = new HashMap<>();

// Add exception details


debugInfo.put("exceptionType", exception.getClass().getSimpleName());
debugInfo.put("exceptionMessage", exception.getMessage());

// Add stack trace


debugInfo.put("stackTrace", Arrays.toString(exception.getStackTrace()));

// Collect browser logs if available


if (driver != null) {
try {
LogEntries logs = driver.manage().logs().get("browser");
StringBuilder browserLogs = new StringBuilder();
for (LogEntry entry : logs) {
browserLogs.append(entry.getMessage()).append("\n");
}
debugInfo.put("browserLogs", browserLogs.toString());
} catch (Exception e) {
debugInfo.put("browserLogs", "Unable to retrieve browser logs");
}
}

return debugInfo;
}

private String determineRootCause(Throwable exception) {


if (exception instanceof NoSuchElementException) {
return "Element Not Found";
} else if (exception instanceof StaleElementReferenceException) {
return "Stale Element Reference";
} else if (exception instanceof TimeoutException) {
return "Timeout";
} else if (exception instanceof ElementNotInteractableException) {
return "Element Not Interactable";
} else if (exception instanceof InvalidSelectorException) {
return "Invalid Selector";
} else {
return analyzeCustomException(exception);
}
}

private String generateRecommendation(Throwable exception, Map<String, String> debugInfo)


{
if (exception instanceof NoSuchElementException) {
return "Element could not be found. Recommendations:\n" +
"1. Verify the element locator (ID, XPath, etc.)\n" +
"2. Add explicit wait conditions\n" +
"3. Check if element is inside an iframe\n" +
"4. Verify page load is complete before interaction";

} else if (exception instanceof StaleElementReferenceException) {


return "Element is no longer attached to the DOM. Recommendations:\n" +
"1. Re-locate the element before interaction\n" +
"2. Add wait conditions for page/element stability\n" +
"3. Verify no JavaScript events are modifying the element";
} else if (exception instanceof TimeoutException) {
return "Operation timed out. Recommendations:\n" +
"1. Increase wait timeout duration\n" +
"2. Check for performance issues\n" +
"3. Verify network connectivity\n" +
"4. Check for JavaScript errors blocking page load";

} else if (exception instanceof ElementNotInteractableException) {


return "Element is present but cannot be interacted with. Recommendations:\n" +
"1. Check if element is visible and enabled\n" +
"2. Verify element is not covered by other elements\n" +
"3. Scroll element into view before interaction\n" +
"4. Check CSS properties affecting interaction";

} else if (exception instanceof InvalidSelectorException) {


return "Invalid selector syntax. Recommendations:\n" +
"1. Review and correct the selector syntax\n" +
"2. Verify XPath or CSS selector is valid\n" +
"3. Check for dynamic element attributes\n" +
"4. Use more robust selector strategies";
}

return analyzeCustomExceptionForRecommendation(exception, debugInfo);


}

private String analyzeCustomException(Throwable exception) {


String message = exception.getMessage().toLowerCase();

if (message.contains("javascript")) {
return "JavaScript Error";
} else if (message.contains("network")) {
return "Network Issue";
} else if (message.contains("iframe")) {
return "IFrame Navigation Issue";
} else if (message.contains("alert")) {
return "Alert Handling Issue";
} else {
return "Unknown Issue";
}
}

private String analyzeCustomExceptionForRecommendation(Throwable exception, Map<String,


String> debugInfo) {
String message = exception.getMessage().toLowerCase();
String browserLogs = debugInfo.getOrDefault("browserLogs", "").toLowerCase();

if (message.contains("javascript") || browserLogs.contains("javascript error")) {


return "JavaScript error detected. Recommendations:\n" +
"1. Check browser console for JavaScript errors\n" +
"2. Verify JavaScript dependencies are loaded\n" +
"3. Check for script conflicts";
} else if (message.contains("network") || browserLogs.contains("failed to load")) {
return "Network issue detected. Recommendations:\n" +
"1. Verify network connectivity\n" +
"2. Check for CORS issues\n" +
"3. Verify API endpoints are accessible";
}

return "General Recommendations:\n" +


"1. Review full stack trace for detailed error information\n" +
"2. Check test environment stability\n" +
"3. Verify test prerequisites are met\n" +
"4. Add logging for better debugging";
}

private double calculateConfidenceScore(Throwable exception, Map<String, String> debugInfo)


{
double score = 0.5; // Base confidence score

// Adjust based on exception type


if (exception instanceof NoSuchElementException ||
exception instanceof StaleElementReferenceException ||
exception instanceof TimeoutException ||
exception instanceof ElementNotInteractableException ||
exception instanceof InvalidSelectorException) {
score += 0.3; // Well-known exceptions
}

// Adjust based on available debug info


if (debugInfo.containsKey("browserLogs") &&
!debugInfo.get("browserLogs").equals("Unable to retrieve browser logs")) {
score += 0.1;
}

// Adjust based on stack trace information


if (debugInfo.containsKey("stackTrace") &&
debugInfo.get("stackTrace").length() > 100) {
score += 0.1;
}

return Math.min(score, 1.0); // Cap at 1.0


}
}

You might also like