2017-07-26 21:45:26 +00:00
|
|
|
require("chromedriver");
|
|
|
|
import seleniumWebdriver = require("selenium-webdriver");
|
2018-03-28 22:04:59 +00:00
|
|
|
import {setWorldConstructor, After} from "cucumber";
|
2017-07-26 21:45:26 +00:00
|
|
|
import Fs = require("fs");
|
|
|
|
import Speakeasy = require("speakeasy");
|
2017-08-04 19:20:31 +00:00
|
|
|
import Assert = require("assert");
|
2017-09-24 12:49:03 +00:00
|
|
|
import Request = require("request-promise");
|
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-07-26 21:45:26 +00:00
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
|
|
|
|
|
2017-07-26 21:45:26 +00:00
|
|
|
function CustomWorld() {
|
|
|
|
const that = this;
|
|
|
|
this.driver = new seleniumWebdriver.Builder()
|
|
|
|
.forBrowser("chrome")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
this.totpSecrets = {};
|
2017-09-02 23:25:43 +00:00
|
|
|
this.configuration = {};
|
2017-07-26 21:45:26 +00:00
|
|
|
|
|
|
|
this.visit = function (link: string) {
|
|
|
|
return this.driver.get(link);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setFieldTo = function (fieldName: string, content: string) {
|
2017-10-14 13:23:00 +00:00
|
|
|
const that = this;
|
2017-07-26 21:45:26 +00:00
|
|
|
return this.driver.findElement(seleniumWebdriver.By.id(fieldName))
|
|
|
|
.sendKeys(content);
|
|
|
|
};
|
|
|
|
|
2017-08-02 22:30:41 +00:00
|
|
|
this.clearField = function (fieldName: string) {
|
|
|
|
return this.driver.findElement(seleniumWebdriver.By.id(fieldName)).clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getErrorPage = function (code: number) {
|
2017-08-04 19:20:31 +00:00
|
|
|
const that = this;
|
2018-07-08 15:02:28 +00:00
|
|
|
return this.driver.wait(seleniumWebdriver.until.elementLocated(seleniumWebdriver.By.tagName("h1")), 5000)
|
2017-08-04 19:20:31 +00:00
|
|
|
.then(function () {
|
|
|
|
return that.driver
|
|
|
|
.findElement(seleniumWebdriver.By.tagName("h1")).getText();
|
|
|
|
})
|
|
|
|
.then(function (txt: string) {
|
2017-10-22 15:42:05 +00:00
|
|
|
try {
|
|
|
|
Assert.equal(txt, "Error " + code);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(txt);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
})
|
2017-08-02 22:30:41 +00:00
|
|
|
};
|
|
|
|
|
2017-07-26 21:45:26 +00:00
|
|
|
this.clickOnButton = function (buttonText: string) {
|
2017-08-04 19:20:31 +00:00
|
|
|
const that = this;
|
2018-07-08 15:02:28 +00:00
|
|
|
return this.driver.wait(seleniumWebdriver.until.elementLocated(seleniumWebdriver.By.tagName("button")), 5000)
|
2017-08-04 19:20:31 +00:00
|
|
|
.then(function () {
|
|
|
|
return that.driver
|
|
|
|
.findElement(seleniumWebdriver.By.tagName("button"))
|
|
|
|
.findElement(seleniumWebdriver.By.xpath("//button[contains(.,'" + buttonText + "')]"))
|
|
|
|
.click();
|
|
|
|
});
|
2017-07-26 21:45:26 +00:00
|
|
|
};
|
|
|
|
|
2017-10-14 13:23:00 +00:00
|
|
|
this.waitUntilUrlContains = function (url: string) {
|
2017-10-14 13:16:14 +00:00
|
|
|
const that = this;
|
|
|
|
return this.driver.wait(seleniumWebdriver.until.urlIs(url), 15000)
|
2018-03-28 22:04:59 +00:00
|
|
|
.then(function () {return BluebirdPromise.resolve(); }, function (err: Error) {
|
2017-10-14 13:23:00 +00:00
|
|
|
that.driver.getCurrentUrl()
|
|
|
|
.then(function (current: string) {
|
|
|
|
console.error("====> Error due to: %s (current) != %s (expected)", current, url);
|
|
|
|
});
|
|
|
|
return BluebirdPromise.reject(err);
|
2017-10-14 13:16:14 +00:00
|
|
|
});
|
2017-09-24 21:19:03 +00:00
|
|
|
};
|
|
|
|
|
2017-07-26 21:45:26 +00:00
|
|
|
this.loginWithUserPassword = function (username: string, password: string) {
|
2018-07-08 15:02:28 +00:00
|
|
|
return that.driver.wait(seleniumWebdriver.until.elementLocated(seleniumWebdriver.By.id("username")), 5000)
|
2017-08-02 22:30:41 +00:00
|
|
|
.then(function () {
|
|
|
|
return that.driver.findElement(seleniumWebdriver.By.id("username"))
|
|
|
|
.sendKeys(username);
|
|
|
|
})
|
2018-07-26 13:31:19 +00:00
|
|
|
.then(function () {
|
|
|
|
return that.driver.findElement(seleniumWebdriver.By.id("password"))
|
|
|
|
.clear();
|
|
|
|
})
|
2017-07-26 21:45:26 +00:00
|
|
|
.then(function () {
|
|
|
|
return that.driver.findElement(seleniumWebdriver.By.id("password"))
|
|
|
|
.sendKeys(password);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return that.driver.findElement(seleniumWebdriver.By.tagName("button"))
|
|
|
|
.click();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-09-24 12:49:03 +00:00
|
|
|
this.retrieveLatestMail = function () {
|
|
|
|
return Request({
|
|
|
|
method: "GET",
|
|
|
|
uri: "http://localhost:8085/messages",
|
|
|
|
json: true
|
|
|
|
})
|
|
|
|
.then(function (data: any) {
|
|
|
|
const messageId = data[data.length - 1].id;
|
|
|
|
return Request({
|
|
|
|
method: "GET",
|
|
|
|
uri: `http://localhost:8085/messages/${messageId}.html`
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(function (data: any) {
|
|
|
|
const regexp = new RegExp(/<a href="(.+)" class="button">Continue<\/a>/);
|
|
|
|
const match = regexp.exec(data);
|
|
|
|
const link = match[1];
|
|
|
|
return BluebirdPromise.resolve(link);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-07-26 21:45:26 +00:00
|
|
|
this.registerTotpSecret = function (totpSecretHandle: string) {
|
2018-07-08 15:02:28 +00:00
|
|
|
return that.driver.wait(seleniumWebdriver.until.elementLocated(seleniumWebdriver.By.className("register-totp")), 5000)
|
2017-08-02 22:30:41 +00:00
|
|
|
.then(function () {
|
|
|
|
return that.driver.findElement(seleniumWebdriver.By.className("register-totp")).click();
|
|
|
|
})
|
2017-07-26 21:45:26 +00:00
|
|
|
.then(function () {
|
2017-09-24 12:49:03 +00:00
|
|
|
return that.retrieveLatestMail();
|
|
|
|
})
|
|
|
|
.then(function (url: string) {
|
|
|
|
return that.driver.get(url);
|
2017-07-26 21:45:26 +00:00
|
|
|
})
|
|
|
|
.then(function () {
|
2017-08-04 19:20:31 +00:00
|
|
|
return that.driver.wait(seleniumWebdriver.until.elementLocated(seleniumWebdriver.By.id("secret")), 5000);
|
2017-07-26 21:45:26 +00:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return that.driver.findElement(seleniumWebdriver.By.id("secret")).getText();
|
|
|
|
})
|
|
|
|
.then(function (secret: string) {
|
|
|
|
that.totpSecrets[totpSecretHandle] = secret;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.useTotpTokenHandle = function (totpSecretHandle: string) {
|
2017-09-21 20:07:34 +00:00
|
|
|
if (!this.totpSecrets[totpSecretHandle])
|
|
|
|
throw new Error("No available TOTP token handle " + totpSecretHandle);
|
|
|
|
|
2017-07-26 21:45:26 +00:00
|
|
|
const token = Speakeasy.totp({
|
|
|
|
secret: this.totpSecrets[totpSecretHandle],
|
|
|
|
encoding: "base32"
|
|
|
|
});
|
|
|
|
return this.useTotpToken(token);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.useTotpToken = function (totpSecret: string) {
|
2017-09-22 15:53:18 +00:00
|
|
|
return that.driver.wait(seleniumWebdriver.until.elementLocated(seleniumWebdriver.By.id("token")), 5000)
|
2017-08-02 22:30:41 +00:00
|
|
|
.then(function () {
|
|
|
|
return that.driver.findElement(seleniumWebdriver.By.id("token"))
|
|
|
|
.sendKeys(totpSecret);
|
|
|
|
});
|
2017-07-26 21:45:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.registerTotpAndSignin = function (username: string, password: string) {
|
|
|
|
const totpHandle = "HANDLE";
|
2017-11-02 20:34:07 +00:00
|
|
|
const authUrl = "https://login.example.com:8080/";
|
2017-07-26 21:45:26 +00:00
|
|
|
const that = this;
|
|
|
|
return this.visit(authUrl)
|
|
|
|
.then(function () {
|
|
|
|
return that.loginWithUserPassword(username, password);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return that.registerTotpSecret(totpHandle);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return that.visit(authUrl);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return that.loginWithUserPassword(username, password);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return that.useTotpTokenHandle(totpHandle);
|
|
|
|
})
|
|
|
|
.then(function () {
|
2017-10-21 23:23:26 +00:00
|
|
|
return that.clickOnButton("Sign in");
|
2017-07-26 21:45:26 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-28 22:04:59 +00:00
|
|
|
setWorldConstructor(CustomWorld);
|