mailslurp-examples - java-serenity-jbehave

https://github.com/mailslurp/examples

Table of Contents

java-serenity-jbehave/serenity.properties

serenity.project.name = Example MailSlurp Project using Serenity and JBehave to test user sign up
webdriver.autodownload = true
webdriver.driver = firefox
webdriver.timeouts.implicitlywait = 60000
webdriver.timeouts.fluentwait = 60000
headless.mode = true


# Customise browser size
#serenity.browser.height = 1200
#serenity.browser.width = 1200

java-serenity-jbehave/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.mailslurp.examples</groupId>
    <artifactId>maislurp-serenity-jbehave-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Sample MailSlurp Serenity project using JBehave and Selenium</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <serenity.version>2.3.2</serenity.version>
        <serenity.jbehave.version>1.46.0</serenity.jbehave.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.mailslurp</groupId>
            <artifactId>mailslurp-client-java</artifactId>
            <version>11.5.11</version>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-jbehave</artifactId>
            <version>${serenity.jbehave.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*TestSuite.java</include>
                        <include>**/Test*.java</include>
                        <include>**/When*.java</include>
                    </includes>
                    <argLine>-Xmx512m</argLine>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </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>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

java-serenity-jbehave/README.md

# Email testing
See [examples repository](https://github.com/mailslurp/examples) for source.

java-serenity-jbehave/Makefile

-include ../.env

test:
	API_KEY=$(API_KEY) mvn install test

report:
	firefox ./target/site/serenity/index.html

java-serenity-jbehave/src/test/resources/stories/sign_up_user/narrative.txt

Sign up a user
To sign up for a web application
As a user
I want to be able to sign up for an application using my email address, verify the address, and log in

java-serenity-jbehave/src/test/resources/stories/sign_up_user/SignUpUser.story

Sign Up User
Narrative:
In order to use an authenticated application
As a user with an email address
I want to sign up to an application and verify my acount

Scenario: Sign Up User
Given the user has email address and is on the example application page
When the user signs up with an email address and password 'test-password'
Then they receive a confirmation code, confirm their account, login with 'test-password' and see 'Welcome'

java-serenity-jbehave/src/test/java/com/mailslurp/examples/MailSlurpClient.java

package com.mailslurp.examples;

import com.mailslurp.clients.ApiClient;
import com.mailslurp.clients.Configuration;
import okhttp3.OkHttpClient;

import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;

public class MailSlurpClient {

    // set your MailSlurp API KEY via environment variable
    private static final String apiKey = System.getenv("API_KEY");
    // set a timeout so we can wait for emails to arrive
    public static final Long TIMEOUT = 30000L;
    private final ApiClient apiClient;

    public MailSlurpClient() {
        assertThat(apiKey, notNullValue());

        // create a MailSlurp client and http client
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                .writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                .build();

        apiClient = Configuration.getDefaultApiClient();

        // IMPORTANT set api client timeouts
        apiClient.setConnectTimeout(TIMEOUT.intValue());
        apiClient.setWriteTimeout(TIMEOUT.intValue());
        apiClient.setReadTimeout(TIMEOUT.intValue());

        // IMPORTANT set API KEY and client
        apiClient.setHttpClient(httpClient);
        apiClient.setApiKey(apiKey);
    }

    public ApiClient getClient() {
        return apiClient;
    }
}

java-serenity-jbehave/src/test/java/com/mailslurp/examples/AcceptanceTestSuite.java

package com.mailslurp.examples;

import net.serenitybdd.jbehave.SerenityStories;

public class AcceptanceTestSuite extends SerenityStories {}

java-serenity-jbehave/src/test/java/com/mailslurp/examples/steps/DefinitionSteps.java

package com.mailslurp.examples.steps;

import com.mailslurp.clients.ApiException;
import com.mailslurp.models.Inbox;
import net.thucydides.core.annotations.Steps;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import com.mailslurp.examples.steps.serenity.EndUserSteps;

public class DefinitionSteps {

    private Inbox inbox;

    @Steps
    EndUserSteps endUser;

    @Given("the user has email address and is on the example application page")
    public void givenTheUserIsOnTheExampleApplicationPage() throws ApiException {
        inbox = endUser.has_email_address();
        endUser.is_on_the_application_page();
    }

    @When("the user signs up with an email address and password '$password'")
    public void thenTheUserSignsUpWithAnEmailAddressAndPassword(String password) {
        endUser.signs_up_with_email_address_and_password(inbox, password);
    }

    @Then("they receive a confirmation code, confirm their account, login with '$password' and see '$message'")
    public void thenTheyReceiveAConfirmationCodeConfirmTheirAccountLoginAndSee(String password, String message) throws ApiException {
        endUser.receive_confirmation_confirm_account_login_and_see_message(inbox, password, message);
    }

}

java-serenity-jbehave/src/test/java/com/mailslurp/examples/steps/serenity/EndUserSteps.java

package com.mailslurp.examples.steps.serenity;

import com.mailslurp.examples.MailSlurpClient;
import com.mailslurp.examples.pages.PlaygroundApplication;
import com.mailslurp.models.Email;
import com.mailslurp.models.Inbox;
import io.cucumber.java.eo.Se;
import net.serenitybdd.core.Serenity;
import net.thucydides.core.annotations.Step;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

import com.mailslurp.apis.*;
import com.mailslurp.clients.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EndUserSteps {

    PlaygroundApplication playgroundApplication;
    MailSlurpClient mailSlurpClient = new MailSlurpClient();

    @Step
    public Inbox has_email_address() throws ApiException {

        // create an inbox controller to create a real email address
        InboxControllerApi inboxControllerApi = new InboxControllerApi(mailSlurpClient.getClient());

        // create an email address for the test user
        Inbox inbox = inboxControllerApi.createInbox(null,null,null,null,null,null,null,null,null);
        assertThat(inbox.getEmailAddress(), containsString("@mailslurp."));

        return inbox;
    }

    @Step
    public void is_on_the_application_page() {
        playgroundApplication.open();
    }

    @Step
    public void receive_confirmation_confirm_account_login_and_see_message(Inbox inbox, String password, String message) throws ApiException {
        // fetch the latest email for the inbox
        WaitForControllerApi waitForControllerApi= new WaitForControllerApi(mailSlurpClient.getClient());
        Email email = waitForControllerApi.waitForLatestEmail(inbox.getId(), MailSlurpClient.TIMEOUT, true);

        // extract the code from the email
        Pattern p = Pattern.compile("Your Demo verification code is ([0-9]{6})");
        Matcher m = p.matcher(email.getBody());
        m.find();
        String code =  m.group(1);

        // submit the code
        playgroundApplication.submit_confirmation_code(code);

        // now login
        playgroundApplication.login_with_email_address_and_password(inbox.getEmailAddress(), password);
        playgroundApplication.wait_for_page_text(message);
    }

    @Step
    public void signs_up_with_email_address_and_password(Inbox inbox, String password) {
        playgroundApplication.sign_up_with_email_and_password(inbox.getEmailAddress(), password);
    }
}

java-serenity-jbehave/src/test/java/com/mailslurp/examples/pages/PlaygroundApplication.java

package com.mailslurp.examples.pages;

import net.serenitybdd.core.Serenity;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.pages.PageObject;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

@DefaultUrl("https://playground.mailslurp.com")
public class PlaygroundApplication extends PageObject {

    public void sign_up_with_email_and_password(String emailAddress, String password) {
        // click sign up button
        find("//a[@data-test='sign-in-create-account-link']").click();
        // expect sign up page
        waitFor("//*[@data-test='sign-up-header-section']//span").shouldContainText("Sign Up");
        // enter email and password
        find("//*[@name='email']").type(emailAddress);
        find("//*[@name='password']").type(password);
        // submit sign up
        find("//button[@data-test='sign-up-create-account-button']").click();
        // should show confirm page
        waitFor("//*[@data-test='confirm-sign-up-header-section']//span").shouldContainText("Confirm");
    }

    public void wait_for_page_text(String text) {
        waitForCondition().until(ExpectedConditions.textToBePresentInElement(find(By.tagName("BODY")), text));
    }

    public void login_with_email_address_and_password(String emailAddress, String password) {
        // enter email and password
        find("//*[@data-test='username-input']").type(emailAddress);
        find("//*[@data-test='sign-in-password-input']").type(password);
        find("//button[@data-test='sign-in-sign-in-button']").click();
    }

    public void submit_confirmation_code(String code) {
        find("//*[@name='code']").type(code);
        find("//button[@data-test='confirm-sign-up-confirm-button']").click();
    }
}