Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Each rule follows the [Selenium + TestNG migration guide](https://docs.seleniumb
| 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. |
| MIG-019 | Properties-based configuration (`.properties` files / `Properties` usage) | Manual review; map values to Selenium Boot configuration |

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.

Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/seleniumboot/migrator/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.stmt.ExpressionStmt;
Expand All @@ -26,6 +27,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -37,6 +39,10 @@
/** Read-only static analysis: parses Java sources and reports patterns that map onto Selenium Boot. */
public final class Analyzer {

private static final String PROPERTIES_CONFIG_ADVICE =
"Properties-based config: move these values to selenium-boot.yml"
+ " (see docs.seleniumboot.com/docs/configuration).";

private static final Set<String> DRIVER_LIFECYCLE_ANNOTATIONS = Set.of(
"BeforeMethod", "AfterMethod", "BeforeClass", "AfterClass", "BeforeTest", "AfterTest",
"BeforeSuite", "AfterSuite", "Before", "After", "BeforeEach", "AfterEach", "BeforeAll", "AfterAll");
Expand Down Expand Up @@ -159,6 +165,25 @@ private void scan(CompilationUnit cu, String file, List<Finding> out, LocatorSta
.filter(c -> c.getNameAsString().endsWith("DriverManager") || c.getNameAsString().endsWith("DriverFactory"))
.forEach(c -> out.add(new Finding("MIG-015", MANUAL, file, line(c), c.getNameAsString(),
"Custom driver lifecycle: review, then replace with BaseTest.")));
// MIG-019: config loaded from .properties files (flag only)
Set<String> propertiesVariables = propertiesVariableNames(cu);
cu.findAll(MethodCallExpr.class).forEach(m -> {
if (m.getNameAsString().equals("load")
&& m.getScope().map(s -> isPropertiesVariable(s, propertiesVariables)).orElse(false)) {
out.add(new Finding("MIG-019", MANUAL, file, line(m), "Properties.load(...)",
PROPERTIES_CONFIG_ADVICE));
}
if (m.getNameAsString().equals("getResourceAsStream")
&& m.getArguments().stream().anyMatch(Analyzer::isPropertiesLiteral)) {
out.add(new Finding("MIG-019", MANUAL, file, line(m), "getResourceAsStream(\"...properties\")",
PROPERTIES_CONFIG_ADVICE));
}
});
cu.findAll(ObjectCreationExpr.class).stream()
.filter(c -> c.getType().getNameAsString().equals("FileInputStream"))
.filter(c -> c.getArguments().stream().anyMatch(Analyzer::isPropertiesLiteral))
.forEach(c -> out.add(new Finding("MIG-019", MANUAL, file, line(c), "new FileInputStream(\"...properties\")",
PROPERTIES_CONFIG_ADVICE)));
// MIG-017: driver lifecycle managed in TestNG or JUnit setup/teardown methods
cu.findAll(MethodDeclaration.class).stream()
.filter(this::isDriverLifecycleMethod)
Expand Down Expand Up @@ -251,6 +276,34 @@ private static boolean containsLegacyWait(ExpressionStmt statement) {
return webDriverWait || expectedConditions;
}

private static Set<String> propertiesVariableNames(CompilationUnit cu) {
Set<String> names = new HashSet<>();
cu.findAll(VariableDeclarationExpr.class).stream()
.filter(v -> isPropertiesType(v.getElementType().asString()))
.forEach(v -> v.getVariables().forEach(variable -> names.add(variable.getNameAsString())));
cu.findAll(FieldDeclaration.class).stream()
.filter(f -> isPropertiesType(f.getElementType().asString()))
.forEach(f -> f.getVariables().forEach(variable -> names.add(variable.getNameAsString())));
cu.findAll(com.github.javaparser.ast.body.Parameter.class).stream()
.filter(p -> isPropertiesType(p.getType().asString()))
.forEach(p -> names.add(p.getNameAsString()));
return names;
}

private static boolean isPropertiesType(String type) {
return type.equals("Properties") || type.endsWith(".Properties");
}

private static boolean isPropertiesVariable(Expression scope, Set<String> propertiesVariables) {
if (scope instanceof NameExpr name) return propertiesVariables.contains(name.getNameAsString());
return scope instanceof FieldAccessExpr field && propertiesVariables.contains(field.getNameAsString());
}

private static boolean isPropertiesLiteral(Expression expression) {
return expression instanceof StringLiteralExpr string
&& string.getValue().endsWith(".properties");
}

private static int line(Node n) {
return n.getBegin().map(p -> p.line).orElse(0);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/seleniumboot/migrator/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private static String ruleLabel(String ruleId) {
case "MIG-011" -> "MIG-011 (@FindBy fields)";
case "MIG-012" -> "MIG-012 (PageFactory.initElements calls)";
case "MIG-018" -> "MIG-018 (ExtentReports usage)";
case "MIG-019" -> "MIG-019 (Properties-based config)";
default -> ruleId;
};
}
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/seleniumboot/migrator/AnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,57 @@ void locate() {
assertTrue(output.contains("By.xpath:"));
}

@Test
void detectsPropertiesBasedConfigInFixture() throws Exception {
var findings = new Analyzer().analyze(fixture("properties-config")).findings().stream()
.filter(finding -> finding.ruleId().equals("MIG-019")).toList();

assertEquals(4, findings.size());
assertTrue(findings.stream().allMatch(finding -> finding.status() == Finding.Status.MANUAL));
assertTrue(findings.stream().allMatch(finding -> finding.advice().contains("selenium-boot.yml")));
assertTrue(findings.stream().anyMatch(finding -> finding.detected().equals("Properties.load(...)")));
assertTrue(findings.stream().anyMatch(finding -> finding.detected().equals("new FileInputStream(\"...properties\")")));
assertTrue(findings.stream().anyMatch(finding -> finding.detected().equals("getResourceAsStream(\"...properties\")")));
}

@Test
void propertiesConfigRuleIgnoresPlainTestClass() {
var report = new Analyzer().analyzeSource("""
class PlainTest {
@Test void works() { assertTrue(true); }
}
""");
assertFalse(report.findings().stream().anyMatch(finding -> finding.ruleId().equals("MIG-019")));
}

@Test
void propertiesConfigRuleIgnoresNonPropertiesStreams() {
var report = new Analyzer().analyzeSource("""
import java.io.FileInputStream;
class Reader {
Object read() throws Exception {
return new FileInputStream("data.csv");
}
}
""");
assertFalse(report.findings().stream().anyMatch(finding -> finding.ruleId().equals("MIG-019")));
}

@Test
void propertiesConfigRuleHasLabelInRenderedReport() {
var report = new Analyzer().analyzeSource("""
import java.util.Properties;
import java.io.FileInputStream;
class Config {
void load() throws Exception {
Properties props = new Properties();
props.load(new FileInputStream("config.properties"));
}
}
""");
assertTrue(report.render().contains("MIG-019 (Properties-based config):"));
}

@Test
void ignoresNonSeleniumByLikeCalls() {
var report = new Analyzer().analyzeSource("""
Expand Down
17 changes: 17 additions & 0 deletions src/test/resources/properties-config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.io.FileInputStream;
import java.util.Properties;

public class Config {

public static Properties fromFile() throws Exception {
Properties props = new Properties();
props.load(new FileInputStream("config.properties"));
return props;
}

public static Properties fromClasspath() throws Exception {
Properties props = new Properties();
props.load(Config.class.getClassLoader().getResourceAsStream("app.properties"));
return props;
}
}