Maven build lifecycle in Selenium Java - Build an Automation Script
import org.testng.Assert; import org.testng.annotations.Test; import java.io.File; import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class MavenBuildLifecycleTest { private String runCommand(String command, String workingDir) throws Exception { ProcessBuilder builder = new ProcessBuilder(); builder.command(command.split(" ")); builder.directory(new File(workingDir)); Process process = builder.start(); StringBuilder output = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { String line; while ((line = reader.readLine()) != null) { output.append(line).append(System.lineSeparator()); } } int exitCode = process.waitFor(); Assert.assertEquals(exitCode, 0, "Command failed: " + command); return output.toString(); } @Test public void testMavenBuildLifecycle() throws Exception { String projectDir = System.getProperty("user.dir"); Path targetDir = Paths.get(projectDir, "target"); // 1. mvn clean runCommand("mvn clean", projectDir); Assert.assertFalse(Files.exists(targetDir), "Target directory should be deleted after clean"); // 2. mvn compile String compileOutput = runCommand("mvn compile", projectDir); Assert.assertTrue(compileOutput.contains("BUILD SUCCESS"), "Compile phase should succeed"); // 3. mvn test String testOutput = runCommand("mvn test", projectDir); Assert.assertTrue(testOutput.contains("BUILD SUCCESS"), "Tests should run and pass"); Assert.assertTrue(testOutput.contains("Tests run:"), "Test results should be reported"); // 4. mvn package String packageOutput = runCommand("mvn package", projectDir); Assert.assertTrue(packageOutput.contains("BUILD SUCCESS"), "Package phase should succeed"); // Verify jar file exists File jarFile = targetDir.toFile().listFiles((dir, name) -> name.endsWith(".jar"))[0]; Assert.assertNotNull(jarFile, "Jar file should be created in target directory"); Assert.assertTrue(jarFile.exists(), "Jar file should exist"); } }
This test class automates the verification of Maven build lifecycle phases for a Selenium Java project.
The runCommand method runs Maven commands in the project directory and captures output. It asserts the command exits successfully.
The test method testMavenBuildLifecycle runs the lifecycle phases in order: clean, compile, test, and package. After each phase, it asserts expected results:
- After
clean, thetargetdirectory should be deleted. - After
compile, the build output should contain "BUILD SUCCESS". - After
test, tests should run and pass, verified by output containing "Tests run:" and "BUILD SUCCESS". - After
package, a jar file should be created in thetargetdirectory.
This approach uses ProcessBuilder to run Maven commands, captures output for assertions, and uses TestNG assertions to validate each step. It is simple, clear, and follows best practices for external process execution and test verification.
Now add data-driven testing to run the Maven lifecycle commands on three different Selenium Java projects located in different directories.