From 2db8f76a6dd697cc167f9ea7023580532b1c9443 Mon Sep 17 00:00:00 2001 From: Jennydou517 <3527851563@qq.com> Date: Sun, 22 Nov 2026 07:03:43 -0600 Subject: [PATCH 1/3] Detect ExtentReports usage in Java source --- .../com/seleniumboot/migrator/Analyzer.java | 17 ++++++++ .../com/seleniumboot/migrator/Report.java | 1 + .../seleniumboot/migrator/AnalyzerTest.java | 39 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/main/java/com/seleniumboot/migrator/Analyzer.java b/src/main/java/com/seleniumboot/migrator/Analyzer.java index 1117aba..2aea9f1 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,6 +20,7 @@ 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; @@ -36,6 +39,8 @@ 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 Set EXTENT_REPORTS_TYPES = + Set.of("ExtentReports", "ExtentTest", "ExtentSparkReporter"); private final JavaParser parser = new JavaParser( new ParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_25)); @@ -163,6 +168,18 @@ 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 -> EXTENT_REPORTS_TYPES.contains(type.getNameAsString())) + .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 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..0f024a0 100644 --- a/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java +++ b/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java @@ -47,6 +47,45 @@ 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 detectsTestNgDriverLifecycleMethods() throws Exception { var findings = new Analyzer().analyze(fixture("lifecycle-testng")).findings().stream() From bcb701d356309c345aeb5861cec6ee7087a8b4d1 Mon Sep 17 00:00:00 2001 From: Jennydou517 <3527851563@qq.com> Date: Sun, 22 Nov 2026 07:14:22 -0600 Subject: [PATCH 2/3] Avoid unrelated ExtentReports type matches --- .../com/seleniumboot/migrator/Analyzer.java | 20 ++++++++++++++++--- .../seleniumboot/migrator/AnalyzerTest.java | 16 +++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/seleniumboot/migrator/Analyzer.java b/src/main/java/com/seleniumboot/migrator/Analyzer.java index 2aea9f1..a98b8c6 100644 --- a/src/main/java/com/seleniumboot/migrator/Analyzer.java +++ b/src/main/java/com/seleniumboot/migrator/Analyzer.java @@ -27,6 +27,7 @@ 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; @@ -39,8 +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 Set EXTENT_REPORTS_TYPES = - Set.of("ExtentReports", "ExtentTest", "ExtentSparkReporter"); + 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)); @@ -170,7 +173,7 @@ private void scan(CompilationUnit cu, String file, List out, LocatorSta }); // MIG-018: ExtentReports declarations and object creation (flag only) cu.findAll(ClassOrInterfaceType.class).stream() - .filter(type -> EXTENT_REPORTS_TYPES.contains(type.getNameAsString())) + .filter(type -> isExtentReportsType(cu, type)) .filter(type -> type.getParentNode().map(parent -> parent instanceof VariableDeclarator || parent instanceof Parameter || parent instanceof MethodDeclaration || parent instanceof ObjectCreationExpr) @@ -182,6 +185,17 @@ private void scan(CompilationUnit cu, String file, List out, LocatorSta .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) { if (lifecycleAnnotation(method) == null || method.getBody().isEmpty()) return false; var body = method.getBody().get(); diff --git a/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java b/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java index 0f024a0..e32d2da 100644 --- a/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java +++ b/src/test/java/com/seleniumboot/migrator/AnalyzerTest.java @@ -86,6 +86,22 @@ void run() { 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() From 1ac806731fffba595863cf29c9c5dc76dc205e79 Mon Sep 17 00:00:00 2001 From: Jennydou517 <3527851563@qq.com> Date: Mon, 23 Nov 2026 01:34:13 -0600 Subject: [PATCH 3/3] Document the MIG-018 rule in the README --- README.md | 1 + 1 file changed, 1 insertion(+) 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.