DEV Community

Terence Pan
Terence Pan

Posted on • Edited on

Playwright with Cucumber/JUnit 5 - Maven setup with pom.xml

Maven setup with pom.xml

In the pom.xml I am using BOMs(Bill of Materials) to manage versions of Junit 5 and Cucumber. This ensures that linked versions of those libraries are all compatible. For example, we are using Cucumber 7.3.4 here, so Gherkin 23.0.1 is the version that was developed with 7.3.4 and will be brought in automatically.

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit5.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-bom</artifactId>
                <version>${cucumber.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Enter fullscreen mode Exit fullscreen mode

Descriptions of dependencies:

  • Playwright: UI testing framework
  • junit-jupiter-api: JUnit 5 testing framework which provides testing methods through classes such as Assertions, Assumptions
  • junit-platform-suite: allows us to create JUnit 5 test suites to create test suite classes to organize features we can run
  • Cucumber: software tool that supports behavior-driven development (BDD)
  • cucumber-junit-platform-engine: used as engine for Junit5
  • cucumber-picocontainer: used for dependency injection to share state and singletons within a scenario through steps
  • gherkin: used to parse Cucumber features written in the Gherkin language to glue code

Description of plugins:

  • maven-compiler-plugin: used to compile sources for code
  • maven-surefire-plugin: used to run tests and test configurations such as reports

pom.xml contents

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.tpan</groupId>
    <artifactId>PlaywrightCucumberExample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <cucumber.version>7.3.4</cucumber.version>
        <maven.surefire.version>3.0.0-M7</maven.surefire.version>
        <playwright.version>1.25.0</playwright.version>
        <junit5.version>5.9.0</junit5.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>${junit5.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-bom</artifactId>
                <version>${cucumber.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.microsoft.playwright</groupId>
            <artifactId>playwright</artifactId>
            <version>${playwright.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.version}</version>
                <configuration>
                    <properties>
                        <configurationParameters>
                            cucumber.plugin=pretty,html:target/site/TestRun.html,json:target/site/TestRun.json
                            cucumber.publish.quiet=true
                            cucumber.publish.enabled=false
                        </configurationParameters>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Enter fullscreen mode Exit fullscreen mode

As always code is on available on Github

Top comments (0)