diff --git a/README.md b/README.md index bc60127..c87d9f5 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,7 @@ Each rule follows the [Selenium + TestNG migration guide](https://docs.seleniumb | MIG-015 | Custom `*DriverManager` / `*DriverFactory` | Manual review | | MIG-016 | `implicitlyWait` | Remove; manual review | | MIG-017 | References to classes removed by migration | Update the caller before compiling | +| MIG-018 | ExtentReports usage (`ExtentReports`, `ExtentTest`, `ExtentSparkReporter`) | Manual review. Selenium Boot generates an HTML report. | The Selenium Boot [getting-started guide](https://docs.seleniumboot.com/docs/getting-started) documents page objects extending `BasePage`, with a `WebDriver` constructor and `By` locator fields. It does not document `@FindBy` or `PageFactory.initElements`; the analyzer therefore reports their counts for review rather than treating them as a direct `BasePage` mapping. diff --git a/src/main/java/com/seleniumboot/migrator/Analyzer.java b/src/main/java/com/seleniumboot/migrator/Analyzer.java index 1117aba..a98b8c6 100644 --- a/src/main/java/com/seleniumboot/migrator/Analyzer.java +++ b/src/main/java/com/seleniumboot/migrator/Analyzer.java @@ -7,6 +7,8 @@ import com.github.javaparser.ast.body.ConstructorDeclaration; import com.github.javaparser.ast.body.FieldDeclaration; import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.body.Parameter; +import com.github.javaparser.ast.body.VariableDeclarator; import com.github.javaparser.ast.expr.MethodCallExpr; import com.github.javaparser.ast.expr.NameExpr; import com.github.javaparser.ast.expr.ObjectCreationExpr; @@ -18,12 +20,14 @@ import com.github.javaparser.ast.stmt.ExpressionStmt; import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.expr.VariableDeclarationExpr; +import com.github.javaparser.ast.type.ClassOrInterfaceType; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.stream.Stream; @@ -36,6 +40,10 @@ public final class Analyzer { private static final Set DRIVER_LIFECYCLE_ANNOTATIONS = Set.of( "BeforeMethod", "AfterMethod", "BeforeClass", "AfterClass", "BeforeTest", "AfterTest", "BeforeSuite", "AfterSuite", "Before", "After", "BeforeEach", "AfterEach", "BeforeAll", "AfterAll"); + private static final Map EXTENT_REPORTS_TYPES = Map.of( + "ExtentReports", "com.aventstack.extentreports.ExtentReports", + "ExtentTest", "com.aventstack.extentreports.ExtentTest", + "ExtentSparkReporter", "com.aventstack.extentreports.reporter.ExtentSparkReporter"); private final JavaParser parser = new JavaParser( new ParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_25)); @@ -163,6 +171,29 @@ private void scan(CompilationUnit cu, String file, List out, LocatorSta ? "Delete the lifecycle glue; extend BaseTest. Driver creation, per-thread isolation, and teardown are handled for you." : "Remove the driver setup; keep the rest.")); }); + // MIG-018: ExtentReports declarations and object creation (flag only) + cu.findAll(ClassOrInterfaceType.class).stream() + .filter(type -> isExtentReportsType(cu, type)) + .filter(type -> type.getParentNode().map(parent -> + parent instanceof VariableDeclarator || parent instanceof Parameter + || parent instanceof MethodDeclaration || parent instanceof ObjectCreationExpr) + .orElse(false)) + .map(type -> new Finding("MIG-018", MANUAL, file, line(type), type.getNameAsString(), + "ExtentReports: Selenium Boot generates an HTML report out of the box. " + + "Remove after migrating custom logging to StepLogger.")) + .distinct() + .forEach(out::add); + } + + private boolean isExtentReportsType(CompilationUnit cu, ClassOrInterfaceType type) { + String simpleName = type.getNameAsString(); + String qualifiedName = EXTENT_REPORTS_TYPES.get(simpleName); + if (qualifiedName == null) return false; + if (type.getScope().isPresent()) return qualifiedName.equals(type.getNameWithScope()); + return cu.getImports().stream() + .filter(declaration -> !declaration.isAsterisk()) + .filter(declaration -> declaration.getName().getIdentifier().equals(simpleName)) + .noneMatch(declaration -> !declaration.getNameAsString().equals(qualifiedName)); } private boolean isDriverLifecycleMethod(MethodDeclaration method) { diff --git a/src/main/java/com/seleniumboot/migrator/Report.java b/src/main/java/com/seleniumboot/migrator/Report.java index 9a3cc25..61924e2 100644 --- a/src/main/java/com/seleniumboot/migrator/Report.java +++ b/src/main/java/com/seleniumboot/migrator/Report.java @@ -106,6 +106,7 @@ private static String ruleLabel(String ruleId) { case "MIG-010" -> "MIG-010 (Page objects)"; case "MIG-011" -> "MIG-011 (@FindBy fields)"; case "MIG-012" -> "MIG-012 (PageFactory.initElements calls)"; + case "MIG-018" -> "MIG-018 (ExtentReports usage)"; default -> ruleId; }; } diff --git a/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java b/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java index 8a3d36f..e32d2da 100644 --- a/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java +++ b/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java @@ -47,6 +47,61 @@ public class DriverFactory { assertTrue(r.contains("MIG-015")); } + @Test + void reportsExtentReportsDeclarationsAndCreations() { + var report = new Analyzer().analyzeSource(""" + import com.aventstack.extentreports.ExtentReports; + import com.aventstack.extentreports.ExtentTest; + import com.aventstack.extentreports.reporter.ExtentSparkReporter; + class LoginTest { + private ExtentTest test; + void setup() { + ExtentReports reports = new ExtentReports(); + reports.attachReporter(new ExtentSparkReporter("report.html")); + } + } + """); + assertTrue(report.unparsable().isEmpty()); + var findings = report.findings().stream() + .filter(finding -> finding.ruleId().equals("MIG-018")).toList(); + assertEquals(List.of("ExtentTest", "ExtentReports", "ExtentSparkReporter"), + findings.stream().map(Finding::detected).toList()); + assertEquals(List.of(5, 7, 8), findings.stream().map(Finding::line).toList()); + assertTrue(findings.stream().allMatch(finding -> finding.status() == Finding.Status.MANUAL)); + assertTrue(report.render().contains("MIG-018 (ExtentReports usage)")); + } + + @Test + void ignoresUnrelatedCodeWhenLookingForExtentReports() { + var report = new Analyzer().analyzeSource(""" + class LoginTest { + // ExtentReports is mentioned here but never used. + void run() { + String name = "ExtentTest"; + ExtentReportsHelper helper = new ExtentReportsHelper(); + } + } + """); + assertTrue(report.unparsable().isEmpty()); + assertFalse(report.findings().stream().anyMatch(finding -> finding.ruleId().equals("MIG-018"))); + } + + @Test + void checksPackageWhenExtentReportsTypeNamesCollide() { + var report = new Analyzer().analyzeSource(""" + import example.reporting.ExtentReports; + class LoginTest { + ExtentReports unrelated = new ExtentReports(); + example.reporting.ExtentTest other; + com.aventstack.extentreports.ExtentReports actual; + } + """); + assertTrue(report.unparsable().isEmpty()); + var findings = report.findings().stream() + .filter(finding -> finding.ruleId().equals("MIG-018")).toList(); + assertEquals(List.of(5), findings.stream().map(Finding::line).toList()); + } + @Test void detectsTestNgDriverLifecycleMethods() throws Exception { var findings = new Analyzer().analyze(fixture("lifecycle-testng")).findings().stream()