As preparation please secure your application from the tutorial End-to-End Tests for SAP Cloud SDK Projects as explained in Secure your application.
The first page you see accessing the application is the app-router showing a login form. The basic idea is that the first part of the test is to log in into the application as a user. You adapt the tests in a way that they first visit the login page, enter the credentials and then press the login button.
First, you 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, you have to trigger the login before the test execution. This can be configured in the file e2e-tests/cucumber.conf.js
which you created in the tutorial End-to-End Tests for SAP Cloud SDK Projects. The updated content of the BeforeAll
method looks as follows:
BeforeAll(async () => {
const options = {
configFile: __dirname + "/nightwatch.conf.js",
env: argv.NIGHTWATCH_ENV || "firefox",
};
await startWebDriver(options);
await createSession(options);
const loginPage = client.page.login();
await loginPage.navigate().loginWithForm(false);
});
There are two new lines in the BeforeAll
block to log into the application.
Furthermore, you have to specify the username
and password
. In our case they come from environment variables.
The updated file e2e-tests/nightwatch.conf.js
from e2e-test looks as follows:
const chromedriver = require('chromedriver');
const geckodriver = require('geckodriver');
const argv = require("yargs").argv;
module.exports = {
//...
test_settings: {
default: {
//...
globals: {
//...
user: "${e2e_username}",
pass: "${e2e_password}"
},
//...
},
},
};
In global, you define user and pass. ${e2e_username}
means that the value is taken from the environment variable e2e_username
.