0
0
Selenium Javatesting~15 mins

Maven project creation in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Create a Maven project and verify Selenium dependency setup
Preconditions (3)
Step 1: Open terminal or command prompt
Step 2: Run command: mvn archetype:generate -DgroupId=com.example -DartifactId=selenium-test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Step 3: Navigate to the created project folder selenium-test
Step 4: Open the pom.xml file in the project
Step 5: Add Selenium Java dependency inside <dependencies> tag:
Step 6: <dependency>
Step 7: <groupId>org.seleniumhq.selenium</groupId>
Step 8: <artifactId>selenium-java</artifactId>
Step 9: <version>4.10.0</version>
Step 10: </dependency>
Step 11: Save the pom.xml file
Step 12: Run command: mvn clean compile to download dependencies and compile the project
Step 13: Verify that the build is successful and no errors appear
✅ Expected Result: Maven project is created with Selenium Java dependency added. The project compiles successfully with Selenium libraries downloaded.
Automation Requirements - JUnit 5 with Selenium WebDriver
Assertions Needed:
Verify project directory exists
Verify pom.xml contains Selenium dependency
Verify mvn clean compile command runs successfully
Best Practices:
Use explicit assertions to check file contents
Use ProcessBuilder or similar to run Maven commands and capture output
Structure test with setup and teardown if needed
Automated Solution
Selenium Java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.*;
import java.nio.file.*;

public class MavenProjectCreationTest {

    private final Path projectPath = Path.of("selenium-test");
    private final Path pomFile = projectPath.resolve("pom.xml");

    @Test
    public void testMavenProjectCreationAndDependency() throws IOException, InterruptedException {
        // Step 1: Run mvn archetype:generate command
        ProcessBuilder generateBuilder = new ProcessBuilder(
            "mvn", "archetype:generate",
            "-DgroupId=com.example",
            "-DartifactId=selenium-test",
            "-DarchetypeArtifactId=maven-archetype-quickstart",
            "-DinteractiveMode=false"
        );
        generateBuilder.redirectErrorStream(true);
        Process generateProcess = generateBuilder.start();
        String generateOutput = new String(generateProcess.getInputStream().readAllBytes());
        int generateExitCode = generateProcess.waitFor();
        assertEquals(0, generateExitCode, "Maven archetype generation failed");

        // Step 2: Verify project directory exists
        assertTrue(Files.exists(projectPath) && Files.isDirectory(projectPath), "Project directory does not exist");

        // Step 3: Add Selenium dependency to pom.xml
        String pomContent = Files.readString(pomFile);
        String seleniumDependency = "<dependency>\n" +
            "    <groupId>org.seleniumhq.selenium</groupId>\n" +
            "    <artifactId>selenium-java</artifactId>\n" +
            "    <version>4.10.0</version>\n" +
            "  </dependency>";

        if (!pomContent.contains("selenium-java")) {
            int insertPos = pomContent.indexOf("</dependencies>");
            if (insertPos == -1) {
                // If no dependencies tag, add one before </project>
                insertPos = pomContent.indexOf("</project>");
                String dependenciesBlock = "  <dependencies>\n" + seleniumDependency + "\n  </dependencies>\n";
                pomContent = pomContent.substring(0, insertPos) + dependenciesBlock + pomContent.substring(insertPos);
            } else {
                pomContent = pomContent.substring(0, insertPos) + seleniumDependency + "\n" + pomContent.substring(insertPos);
            }
            Files.writeString(pomFile, pomContent);
        }

        // Step 4: Verify pom.xml contains Selenium dependency
        String updatedPom = Files.readString(pomFile);
        assertTrue(updatedPom.contains("selenium-java"), "Selenium dependency not found in pom.xml");

        // Step 5: Run mvn clean compile
        ProcessBuilder compileBuilder = new ProcessBuilder("mvn", "clean", "compile");
        compileBuilder.directory(projectPath.toFile());
        compileBuilder.redirectErrorStream(true);
        Process compileProcess = compileBuilder.start();
        String compileOutput = new String(compileProcess.getInputStream().readAllBytes());
        int compileExitCode = compileProcess.waitFor();

        assertEquals(0, compileExitCode, "Maven compile failed. Output:\n" + compileOutput);
    }
}

This test automates the manual steps to create a Maven project with Selenium dependency.

First, it runs the Maven archetype generate command using ProcessBuilder and checks the exit code to ensure success.

Then it verifies the project folder exists.

Next, it reads the pom.xml file and adds the Selenium dependency if missing, then verifies the dependency is present.

Finally, it runs mvn clean compile inside the project folder and asserts the build succeeds.

This approach uses explicit assertions, runs commands programmatically, and manipulates files safely, following best practices.

Common Mistakes - 4 Pitfalls
Hardcoding absolute file paths
Not checking process exit codes
Editing pom.xml as plain text without checking structure
Ignoring output streams of processes
Bonus Challenge

Now add data-driven testing to create Maven projects with 3 different groupId and artifactId combinations

Show Hint