Skip to Content

In this tutorial, we give an overview of how to run End-To-End (E2E) tests against applications which are secured. We show how to configure the pipeline provided together with the SAP S/4HANA Cloud SDK in order to run these tests.

Note: This post is part of a series. For a complete overview visit the SAP S/4HANA Cloud SDK Overview.

Goal of this Blog Post

In the previous blog posts, we learned how to write E2E tests (step 13), how to secure an application (step 7) and how to setup a continuous delivery server (step 14).

In this tutorial, we will adapt the E2E tests from step 13 so that they can also test a secured application. We also show how to configure the continuous delivery pipeline (step 14) afterwards.

Add Authentication to E2E Tests

The basic idea is that the first part of the test is to log in into the application as a user would do it. We created an application which is secured as explained in step 7. The first page you see accessing the application is the app-router showing a login form. We adapt the tests in a way that they first visit the login page, enter the credentials and then press the login button.

First, we create the following page object in e2e-tests/page_objects/login.js. This page object reads the credentials from the configuration and enters them into the input fields. The selectors of these fields are specified in the elements section. These selectors are working for the standard app-router. For other forms, e.g. on Cloud Platform Neo, or customized forms these selectors need to be updated.

"use strict";

const loginCommands = {
  loginWithForm: function () {
    const user = this.api.globals.user;
    const pass = this.api.globals.pass;
    delete this.api.globals.user;
    delete this.api.globals.pass;
    this.waitForElementVisible("@usernameField")
      .setValue("@usernameField", user);
    this.waitForElementVisible("@passwordField")
      .setValue("@passwordField", pass);
    this.waitForElementVisible("@submitButton").click("@submitButton");

    this.expect.element("@usernameField").to.not.be.present;
    return this;
  },
};

module.exports = {
  url: function () {
    return this.api.launchUrl;
  },
  elements: {
    usernameField: "input[name=username]",
    passwordField: "input[name=password]",
    submitButton: "input[type=submit]"
  },
  commands: [loginCommands]
};

As a next step, we have to trigger the login before the test execution. For this purpose, nightwatchjs offers the possibility to define hooks. Therefore, we create the file e2e-tests/hooks.js. This file defines a BeforeAll method, which is executed before the test execution. It navigates to the login page and calls the login method we defined before.

"use strict";

const { defineSupportCode } = require("cucumber");
const browser = require("nightwatch-cucumber").client;

defineSupportCode(({ Before, BeforeAll, After }) => {

  BeforeAll({ timeout: browser.globals.stepTimeout * 4 }, () => {
    const loginPage = browser.page.login();
    loginPage.navigate().loginWithForm();
  });
});

Last, we have to register the hook in the nightwatch configuration file. Furthermore, we have to specify the username and password. In our case they come from environment variables.
The updated file e2e-tests/nightwatch.conf.js from step 13 looks as follows:

const argv = require("yargs").argv;

require('nightwatch-cucumber')({
    cucumberArgs: [
       '--require', 'timeout.js', 
       '--require', 'hooks.js', 
       '--require', 'features/step_definitions', 
       '--format', 'json:../s4hana_pipeline/reports/e2e/cucumber.json',
       'features'
  ]
})

var chromeOptionsArgs = ["--no-sandbox" , "window-size=1400,1000"]
if(argv.headless){
    chromeOptionsArgs.push("--headless")
    chromeOptionsArgs.push("--disable-gpu")
}

const options = {
    output_folder: '../s4hana_pipeline/reports/e2e',
    custom_assertions_path: '',
    page_objects_path: 'page_objects',
    live_output: false,
    disable_colors: false,
    globals_path: "external.globals.js",
    selenium: {
        start_process: false,
    },
    test_settings: {
        default: {
            launch_url : argv.launchUrl,
            selenium_port: 9515,
            selenium_host: '127.0.0.1',
            default_path_prefix: "",
            desiredCapabilities: {
                browserName: "chrome",
                javascriptEnabled: true,
                acceptSslCerts: true,
                chromeOptions: {
                    args: chromeOptionsArgs
                }
            },
            globals: {
                abortOnAssertionFailure : true,
                retryAssertionTimeout: 10000,
                waitForConditionTimeout: 10000,
                asyncHookTimeout : 10000,
                user: "${e2e_username}",
                pass: "${e2e_password}"
            },
            screenshots : {
                enabled : true,
                on_failure : true,
                on_error : true,
                path: '../s4hana_pipeline/reports/e2e/screenshots'
            }
        }
    }
}

module.exports = options;

Note that we added the file hooks.js as requirement in the cucumberArgs. Furthermore, in globals, we define user and pass${e2e_username} means that the value is taken from the environment variable e2e_username.

Run E2E Test locally

To run the tests locally we can use the same command as used in step 13. However, in SAP Cloud Platform Cloud Foundry the application url (launchUrl) should point to the app-router. In the Neo environment, it should point to the application because the user is redirected to the login form automatically.

Furthermore, the username and password environment variables have to be set. On Windows you can use the command set to do that. The final command looks as follows:

set e2e_username=myUser
set e2e_password=myPassword
npm install
npm run ci-e2e -- --launchUrl=https://path/to/your/running/app-router

Note that your password is stored in the history of your terminal. We recommend using a technical user for the E2E tests.

Run E2E Tests in the Pipeline

To run the tests in our pipeline we have to adapt the pipeline_config.yml. In step 14 we learned that this file configures the behavior of the pipeline. To execute the E2E tests we have to add a section called endToEndTests representing a stage in the section stages. The final configuration is shown below.

#Project Setup
general:
  productiveBranch: 'master'
  projectName: 'business-partner-manager'

#Stage Specific Configurations
stages:  
  endToEndTests:
    appUrls:
      - url: 'https://approuter-USERNAME.cfapps.eu10.hana.ondemand.com'
        credentialId: e2e-test-user-cf

    cfTargets:
      - space: 'MySpaceName'
        manifest: 'manifest-test.yml'
        org: 'MyOrg'
        appName: 'firstapp'
        credentialsId: 'deployment-cf'

The section endToEndTests consists of two sections. Before we can run the E2E tests, we first have to deploy our application to the SAP Cloud Platform. As for the productive deployment, we define a section called cfTargets or neoTargets to configure the deployment. Afterwards in appUrls we define a list of urls and credential ids specifying the launch url for the tests and the credentials used as username and password environment variables.

The tests are executed once per entry in the list. For each entry the url is passed as launchUrl to the test. The credentialsId is used to read the corresponding credentials from the credentials store in Jenkins. Thus, we have to create these credentials, as explained in step 14. The username and password is read from the credentials store and passed as environment variable to the test.

We can also run the E2E tests after the productive deployment. These tests can be called smoke tests to check that your application is running after the deployment. It is usually a subset of the E2E tests, but can also be the full test suite. Please note that for the smoke test the command npm ci-smoke is used instead of npm ci-e2e. Thus, we have to define this command in the file package.json. To run the same tests you can just copy the configuration from the command ci-e2e in the section scripts as shown below. To run a subset of the tests you can use tags.

 "scripts": {
    "ci-e2e": "cd e2e-tests && nightwatch --skiptags=ignore",
    "ci-smoke": "cd e2e-tests && nightwatch --skiptags=ignore"
  },

The configuration for the stage productiveDeployment in the file pipeline_config.yml has the same structure as the stage endToEndTests:

#Project Setup
general:
  productiveBranch: 'master'
  projectName: 'business-partner-manager'

#Stage Specific Configurations
stages:  
  productionDeployment:
    appUrls:
      - url: 'https://approuter-USERNAME.cfapps.eu10.hana.ondemand.com'
        credentialId: e2e-test-user-cf

    cfTargets:
      - space: 'MySpaceNameProductive'
        manifest: 'manifest.yml'
        org: 'MyOrg'
        appName: 'firstapp'
        credentialsId: 'deployment-cf'

In this tutorial we learned how to run E2E tests against secured applications. We also learned how to configure the pipeline so that the pipeline can run these tests.

Questions and Troubleshooting

Are you facing a development question? Then check out Stack Overflow for SAP S/4HANA Cloud SDK related questions. If you do not find an answer, feel free to post your question and make sure to attach the tag ‘s4sdk’. Our team, as well as the whole Stack Overflow community, are at your service and will quickly react to your question.

For an overview of SAP S/4HANA Cloud SDK related questions, go to https://stackoverflow.com/questions/tagged/s4sdk.

You think that you found a bug in one of our Continuous Delivery artifacts? Feel free to open an issue in our Pipeline GitHub repository on https://github.com/SAP/cloud-s4-sdk-pipeline/issues.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply