Serenity Java

Applies to CrossBrowserTesting SaaS, last modified on January 10, 2023

Want a powerful and easy to use command line tool for running Selenium tests? Serenity might be the option for you. Serenity provides language-bindings for the powerful browser-driving tool Selenium. Its use of Cucumber’s Gherkin language allows you to write your tests in a way that can be easily read by anyone on your team. Serenity Java integrates easily with the CrossBrowserTesting platform, so you can perform tests on a wide variety of OS/Device/Browser combinations, all from one test.

Get set up

  • Start by installing Maven, a software project management and comprehension tool.

  • Create a simple project with Cucumber installed by running command:

    mvn archetype:generate \
    -DarchetypeGroupId=io.cucumber \
    -DarchetypeArtifactId=cucumber-archetype \
    -DarchetypeVersion=2.3.1.2 \
    -DgroupId=com.cbt \
    -DartifactId=seleniumserenity \
    -Dpackage=com.cbt \
    -Dversion=1.0.0-SNAPSHOT \
    -DinteractiveMode=false

  • After changing into the seleniumserenity/ directory that was just created, add the necessary serenity dependencies, properties, and plugins to the pom.xml file.

    seleniumserenity/pom.xml

    <?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>com.cbt</groupId>
        <artifactId>serenity-cbt</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>

        <name>Serenity CBT Integration</name>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <serenity.version>1.1.10</serenity.version>
            <serenity.maven.version>1.1.7</serenity.maven.version>
            <serenity.cucumber.version>1.1.1</serenity.cucumber.version>
        </properties>

        <repositories>
            <repository>
                <id>serenity</id>
                <name>bintray</name>
                <url>http://dl.bintray.com/serenity/maven</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>serenity</id>
                <name>bintray-plugins</name>
                <url>http://dl.bintray.com/serenity/maven</url>
            </pluginRepository>
        </pluginRepositories>

        <dependencies>
            <dependency>
                <groupId>net.serenity-bdd</groupId>
                <artifactId>serenity-core</artifactId>
                <version>${serenity.version}</version>
            </dependency>
           
            <dependency>
                <groupId>net.serenity-bdd</groupId>
                <artifactId>serenity-junit</artifactId>
                <version>${serenity.version}</version>
            </dependency>
            <dependency>
                <groupId>net.serenity-bdd</groupId>
                <artifactId>serenity-rest-assured</artifactId>
                <version>${serenity.version}</version>
            </dependency>
            <dependency>
                <groupId>net.serenity-bdd</groupId>
                <artifactId>serenity-cucumber</artifactId>
                <version>${serenity.cucumber.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.6.1</version>
            </dependency>
            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>1.8.6</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.googlecode.lambdaj</groupId>
                <artifactId>lambdaj</artifactId>
                <version>2.3.3</version>
            </dependency>
            <dependency>
                <groupId>org.assertj</groupId>
                <artifactId>assertj-core</artifactId>
                <version>1.7.0</version>
                <scope>test</scope>
            </dependency>
         
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <version>1.4</version>
                    <executions>
                        <execution>
                            <id>enforce</id>
                            <configuration>
                                <rules>
                                    <requireUpperBoundDeps/>
                                </rules>
                            </configuration>
                            <goals>
                                <goal>enforce</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.18</version>
                    <configuration>
                        <includes>
                            <include>com/cbt/cucumber/SingleTest.java</include>
                        </includes>
                        <reuseForks>true</reuseForks>
                        <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>net.serenity-bdd.maven.plugins</groupId>
                    <artifactId>serenity-maven-plugin</artifactId>
                    <version>${serenity.maven.version}</version>
                    <dependencies>
                      <dependency>
                        <groupId>net.serenity-bdd</groupId>
                        <artifactId>serenity-core</artifactId>
                        <version>${serenity.version}</version>
                      </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>serenity-reports</id>
                            <phase>post-integration-test</phase>
                            <goals>
                                <goal>aggregate</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

        <profiles>
            <profile>
                <id>single</id>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-failsafe-plugin</artifactId>
                            <version>2.18</version>
                            <configuration>
                                <includes>
                                    <include>com/cbt/cucumber/SingleTest.java</include>
                                </includes>
                                <reuseForks>true</reuseForks>
                                <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
                                <systemPropertyVariables>
                                    <environment>single</environment>
                                    <taskID>MY_TEST_SCHEMA_${surefire.forkNumber}</taskID>
                                </systemPropertyVariables>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>

        </profiles>
    </project>
  • In the same folder, create the serenity.properties file, which will be used to set capabilities, containing the following content:

    serenity.dry.run=false
    serenity.take.screenshots=AFTER_EACH_STEP

    environment.single.name=Todo_Test_Serenity
    environment.single.browserName=Chrome
    environment.single.version=72
    environment.single.platform=Windows 10
    environment.single.record_video=true
    environment.single.screenResolution=1366x768

Write tests

Create an empty file called src/test/resources/features/single.feature with the following content:

Feature: ToDo App
  Scenario: Archiving ToDos
    Given I go to my ToDo App
    When I archive all todos
    Then I should have no todos

Edit the file called src/test/java/com/cbt/CbtSerenityCapabilities.java with the following content:

src/test/java/com/cbt/CbtSerenityCapabilities.java

package com.cbt;

import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import java.util.Iterator;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import net.thucydides.core.util.EnvironmentVariables;
import net.thucydides.core.util.SystemEnvironmentVariables;

publicclass CbtSerenityCapabilities{
    DesiredCapabilities capabilities = new DesiredCapabilities();

    public DesiredCapabilities newCaps() {
        EnvironmentVariables environmentVariables = SystemEnvironmentVariables.createEnvironmentVariables();
        String environment = System.getProperty("environment");
        
        Iterator it = environmentVariables.getKeys().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            if (environment != null && key.startsWith("environment." + environment)) {
                capabilities.setCapability(key.replace("environment." + environment + ".", ""), environmentVariables.getProperty(key));
            }
        }
            System.out.println(capabilities);
            return capabilities;
    }

    public boolean takesScreenshots() {
        return true;
    }
}

Edit the file called src/test/java/com/cbt/cucumber/SingleTest.java with the following content:

src/test/java/com/cbt/cucumber/SingleTest.java

package com.cbt.cucumber;

import cucumber.api.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features="src/test/resources/features/single.feature")
publicclass SingleTest { }

Edit the file called src/test/java/com/cbt/cucumber/steps/TodoSteps.java with the following content:

src/test/java/com/cbt/cucumber/steps/TodoSteps.java

package com.cbt.cucumber.steps;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import org.junit.Assert;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.*;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import net.thucydides.core.annotations.Steps;
import java.text.ParseException;
import com.cbt.CbtSerenityCapabilities;

publicclass TodoSteps {
    private String username, authkey;
    private RemoteWebDriver driver;

    @Before
    publicvoid setUp() throws Throwable {

        username = "YOUR_USERNAME"; //replace with your username be sure to encode @ with %40
        authkey = "YOUR_AUTHKEY"; //replace with your authkey

        CbtSerenityCapabilities caps = new CbtSerenityCapabilities();

        driver = new RemoteWebDriver(new URL("http://" + username + ":" + authkey +"@hub.crossbrowsertesting.com:80/wd/hub"), caps.newCaps());
    }
    @Given("^I go to my ToDo App$")
    publicvoid I_go_to_my_todo_app() throws Throwable {
         driver.get("http://crossbrowsertesting.github.io/todo-app.html");
    }

    @When("^I click on all todos$")
    publicvoid I_click_on_my_todos() throws Throwable {
        driver.findElement(By.name("todo-1")).click();
        driver.findElement(By.name("todo-2")).click();
        driver.findElement(By.name("todo-3")).click();
        driver.findElement(By.name("todo-4")).click();
        driver.findElement(By.name("todo-5")).click();
    }

    @When("^I click archive$")
    publicvoid I_click_archive() throws Throwable {
        driver.findElement(By.linkText("archive")).click();
    }

    @Then("^I should have no todos$")
    publicvoid I_should_have_no_todos() throws Throwable {
        List elems = driver.findElements(By.className("done-true"));
        Assert.assertEquals(0, elems.size());
    }

    @After
    publicvoid teardown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

Note: You will need to use your Username and Authkey to run your tests on CrossBrowserTesting. To get yours, sign up for a free trial or purchase a plan.

As you can probably make out from our test, we visit a small ToDo App example, interact with our page, and use assertions to verify that the changes we’ve made are actually reflected in our app.

Now you are ready to run your test using the command:

mvn verify -P single

Congratulations! You have successfully integrated CBT and Serenity for Java. We kept it short and sweet for our purposes, but there is so much more you can do with Cucumber! Being built on top of Selenium means the sky is the limit as far as what you can do.

For examples and source code to this tutorial, check out our  Serenity Java GitHub Repository

If you have any trouble, feel free to get in touch. We are always happy to help!

See Also

About Selenium Testing
Selenium and Java
TestNG
Cucumber Java

Highlight search results