/serenity-bdd-maven-junit-example

serenity-bdd-maven-junit-example

Primary LanguageJavaGNU General Public License v3.0GPL-3.0

Getting Started guide to run serenity bdd project with maven and junit

This guide walks you through to run a simple serenity bdd test project using maven and junit

Getting Started

To run serenity bdd sample project with maven and junit

What you’ll build

To run serenity bdd sample project with maven and junit against an application under test http://serenitybddpractice.com

What you’ll need

  1. About 15 minutes

  2. Java IDE like

    1. Intellij or

    2. Eclipse

  3. JDk 8 or higher

  4. Git (optional)

How to complete this guide

The information provided here is in detail step by step procedure like any other serenity bdd guides.
If you are already aware of any of these steps feel free to skip them.

To Start from beginning move on to Build with Maven.

To skip the basics, do the following :

  1. Download and unzip the source repository for this guide, or clone it using git:

Build with Maven

Step 1:

Creating a project from basic maven archetype
Open commandline and run

mvn archetype:generate -DgroupId=serenity-bdd-guides -DartifactId=serenity-bdd-maven-junit-example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -Dpackage=net.serenitybdd.examples // Linux & Mac machines
Step 1 Explanation:
  1. A basic maven project requires a group id & artifact id ( generally a project name).

  2. An archetypeArtifactId defines the type of maven project that we would like to create

    1. We are choosing a quickstart archetype

  3. We are providing a package name under which the classes will be created.

Note
A new basic maven project created with a folder name serenity-bdd-maven-junit-example by end of this step.
Step 2:
  1. Open the new maven project using an IDE like Intellij or Eclipse

  2. Open the file named pom.xml and make sure the following tags are available.

    1. Provide Group Id (Should be there)

    2. Provide Artifact id (Should be there)

    3. Provide project name (Add the project name)

    4. Provide properties (Add these property values)

        <modelVersion>4.0.0</modelVersion>
2.b.1   <groupId>serenity-bdd-guides</groupId>
2.b.2   <artifactId>serenity-bdd-maven-junit-test-example</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>

2.b.3   <name>Sample Serenity BDD project using maven and junit</name>

2.b.4   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <serenity.version>1.9.45</serenity.version>
        <serenity.maven.version>1.9.43</serenity.maven.version>
        <encoding>UTF-8</encoding>
       </properties>
Step 2 Explanation :
  1. Group Id and artifact Id should be available after step 1

  2. Add project name

  3. Add project properties include serenity version and serenity maven version.

Step 3:
  1. Add the below dependencies

    1. serenity-core

    2. serenity-junit

    3. junit

  2. Add the below plugins

    1. maven-failsafe-plugin

    2. serenity-maven-plugin

3.a <dependencies>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
3.a.1         <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
3.a.2         <artifactId>serenity-junit</artifactId>
            <version>${serenity.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
3.a.3         <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
   </dependencies>

 <build>
3.b      <plugins>
            <plugin>
3.b.1             <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
3.b.2             <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.maven.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
   </build>
Step 3 Explanation:
  1. Dependencies will get the required jar files for the project to run successfully

  2. Plugins will be used as a tool to execute a specific action defined by maven

Step 4:
  1. Add Page Objects

    1. Create ApplicationHomePage Class

    2. Create LoginScreen Class

    3. Create UserAccountPage Class

4.a public class ApplicationHomePage extends PageObject {

    @FindBy(css="#_desktop_user_info > div > a")
    private static WebElementFacade GET_LOGIN_SCREEN;

    @FindBy(css="#_mobile_user_info > div > a")
    private static WebElementFacade GET_MOBILE_LOGIN_SCREEN;

    public void openAt(){
        this.openAt("http://www.serenitybddpractice.com");
    }

    public void goToLoginScreen(){
        if(CurrentOS.getType() == CurrentOS.OSType.other)
            GET_MOBILE_LOGIN_SCREEN.click();
        else GET_LOGIN_SCREEN.click();
    }
4.b public class LoginScreen extends PageObject {

        @FindBy(css="#login-form > section > div:nth-child(2) > div.col-md-6 > input")
        private static WebElementFacade FILL_USERNAME;

        @FindBy(css="#login-form > section > div:nth-child(3) > div.col-md-6 > div > input")
        private static WebElementFacade FILL_PASSWORD;

        @FindBy(css="#submit-login")
        private static WebElementFacade SIGN_IN;

        public LoginScreen(WebDriver driver) {
            super(driver);
        }

        private void fillUserName(String userName){
            FILL_USERNAME.clear();
            FILL_USERNAME.sendKeys(userName);
        }

        private void fillPassword(String password){
            FILL_PASSWORD.clear();
            FILL_PASSWORD.sendKeys(password);
        }


        private void clickSignIn() {
            SIGN_IN.click();
        }

        public void login(String userName, String password){
            fillUserName(userName);
            fillPassword(password);
            clickSignIn();
        }
    }
4.c public class UserAccountPage extends PageObject {

        public void checkTitle(){
            Assert.assertEquals( getTitle(),"My account");
        }
    }
Step 5:
  1. Add Step Objects

    1. Create LoginSteps class

5.a public class LoginSteps {

        private String actor;

        private ApplicationHomePage applicationHomePage;

        private LoginScreen loginScreen;

        private UserAccountPage userAccountPage;


        @Step("#actor is a registered member")
        public void IsARegisteredMember(){
            applicationHomePage.openAt();
            // we can check credentials using api or db
        }

        @Step("#actor should be able to sign in with their account")
        public void signInWithTheirAccount(String userName, String password){
            applicationHomePage.goToLoginScreen();
            loginScreen.login(userName, password);
        }

        @Step("#actor should be able to view their profile")
        public void checkProfile(){
            userAccountPage.checkTitle();
        }
    }
Step 6:
  1. Add Test

    1. Create Login Test class

6.a @RunWith(SerenityRunner.class)
    public class LoginIT {

        @Managed
        WebDriverFacade driver; // defaults to firefox geckodriver

        @Steps
        LoginSteps carla;

        @Test
        public void checkUserSuccessfullyLogin(){
            // Given
            carla.IsARegisteredMember();

            // When
            carla.signInWithTheirAccount("carla_the_online_customer@getnada.com","password");

            // Then
            carla.checkProfile();
        }
    }

Run tests

Step 1:
  1. Open a command line and run

  2. mvn verify

  3. mvn verify -Dwebdriver.driver=driverName

Tip
Possible driver values
a. chrome
b. firefox
c. iexplorer
d. edge
e. safari
f. appium
g. android
h. ios
i. provided driver
1.b mvn verify
1.b ./mvnw verify // Linux & Mac machines using maven wrapper
1.b mvnw.cmd verify // Windows machines using maven wrapper
1.c mvn verify -Dwebdriver.driver=chrome // for chromedriver
Caution
Make sure the required driver setup is done and it’s accessible using path variable
Tip
You can also provide the driver path by providing the following parameter in command line webdriver.${driverName}.driver.
For chrome driver path parameter name is webdriver.chrome.driver

Summary

Congratulations! You’ve successfully built a basic serenity bdd project using junit and maven.