flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
422 lines • 17.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssertionContext = void 0;
const assertion_1 = require("./assertion");
const assertion_result_1 = require("../logging/assertion-result");
const util_1 = require("../util");
const flagpole_execution_1 = require("../flagpole-execution");
const helpers_1 = require("../helpers");
const value_promise_1 = require("../value-promise");
const getParamsFromExists = (a, b, c, d) => {
const selector = typeof b === "string" ? b : a;
const message = typeof b === "string" ? a : null;
const matches = c instanceof RegExp ? c : b instanceof RegExp ? b : null;
const contains = typeof c === "string" ? c : null;
const opts = ((contains ? d : message ? c : b) || {});
return {
selector: selector,
message: message,
matches: matches,
contains: contains,
opts: opts,
};
};
class AssertionContext {
constructor(scenario, response) {
this.scenario = scenario;
this.response = response;
this._assertions = [];
this._subScenarios = [];
this.map = util_1.asyncMap;
this.some = util_1.asyncSome;
this.every = util_1.asyncEvery;
this.filter = util_1.asyncFilter;
this.none = util_1.asyncNone;
this.each = util_1.asyncForEach;
}
get request() {
return this.scenario.request;
}
get suite() {
return this.scenario.suite;
}
get executionOptions() {
return flagpole_execution_1.FlagpoleExecution.global;
}
get incompleteAssertions() {
const incompleteAssertions = [];
this._assertions.forEach((assertion) => {
if (!assertion.assertionMade) {
incompleteAssertions.push(assertion);
}
});
return incompleteAssertions;
}
get assertionsResolved() {
const promises = [];
this._assertions.forEach((assertion) => {
if (assertion.assertionMade) {
promises.push(assertion.result);
}
});
return Promise.all(promises);
}
get subScenariosResolved() {
return Promise.all(this._subScenarios);
}
get currentUrl() {
return this.response.currentUrl;
}
comment(input) {
this.scenario.comment(input);
return this;
}
assert(a, b) {
const { value, message } = arguments.length === 2
? { value: b, message: a }
: { value: a, message: undefined };
const assertion = new assertion_1.Assertion(this, value, message);
this._assertions.push(assertion);
return assertion;
}
pause(milliseconds) {
return new Promise((resolve) => {
setTimeout(() => {
this._completedAction("PAUSE", `${milliseconds}ms`);
resolve();
}, milliseconds);
});
}
clearThenType(selector, textToType, opts = {}) {
return this.response.clearThenType(selector, textToType, opts);
}
clear(selector) {
return this.response.clear(selector);
}
type(selector, textToType, opts = {}) {
return this.response.type(selector, textToType, opts);
}
selectOption(selector, value) {
return __awaiter(this, void 0, void 0, function* () {
yield this.response.selectOption(selector, value);
this._completedAction("SELECT", typeof value == "string" ? value : value.join(", "));
});
}
eval(js, ...args) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.response.eval.apply(this, [js, ...args]);
});
}
waitForFunction(js, opts, ...args) {
return __awaiter(this, void 0, void 0, function* () {
yield this.response.waitForFunction.apply(this.response, [
js,
opts,
...args,
]);
this._completedAction("WAIT", "Function to evaluate as true");
});
}
waitForReady(timeout = 15000) {
return __awaiter(this, void 0, void 0, function* () {
yield this.response.waitForReady(timeout);
this._completedAction("WAIT", "Ready");
});
}
waitForLoad(timeout = 30000) {
return __awaiter(this, void 0, void 0, function* () {
yield this.response.waitForLoad(timeout);
this._completedAction("WAIT", "Load");
});
}
waitForNetworkIdle(timeout = 10000) {
return __awaiter(this, void 0, void 0, function* () {
yield this.response.waitForNetworkIdle(timeout);
this._completedAction("WAIT", "Network Idle");
});
}
waitForNavigation(timeout = 10000, waitFor) {
return __awaiter(this, void 0, void 0, function* () {
yield this.response.waitForNavigation(timeout, waitFor);
this._completedAction("WAIT", "Navigation");
});
}
waitForXPath(xPath, timeout) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const el = yield this.response.waitForXPath(xPath, timeout);
el.isNull()
? this._failedAction("XPATH", xPath)
: this._completedAction("XPATH", xPath);
return el;
}));
}
waitForHidden(selector, timeout) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const el = yield this.response.waitForHidden(selector, timeout);
el.isNull()
? this._failedAction("HIDDEN", selector)
: this._completedAction("HIDDEN", selector);
return el;
}));
}
waitForVisible(selector, timeout) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const el = yield this.response.waitForVisible(selector, timeout);
el.isNull()
? this._failedAction("VISIBLE", selector)
: this._completedAction("VISIBLE", selector);
return el;
}));
}
waitForHavingText(selector, text, timeout) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () { return this.waitForExists(selector, text, timeout); }));
}
waitForExists(selector, a, b) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const selectors = util_1.toArray(selector);
try {
const el = yield this.response.waitForExists(selector, a, b);
this._completedAction("EXISTS", `${selector}`);
return el;
}
catch (ex) {
this._failedAction("EXISTS", `${selector}`);
throw `${selector} did not exist before timeout`;
}
}));
}
waitForNotExists(selector, a, b) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
try {
const val = yield this.response.waitForNotExists(selector, a, b);
this._completedAction("NOT EXISTS", `${selector}`);
return val;
}
catch (ex) {
this._failedAction("NOT EXISTS", `${selector}`);
throw `${selector} still exists after timeout`;
}
}));
}
exists(selector, a, b) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const selectors = util_1.toArray(selector);
const params = helpers_1.getFindParams(a, b);
const opts = params.opts || {};
const element = yield util_1.asyncUntil(selectors, (selector) => __awaiter(this, void 0, void 0, function* () {
return params.contains
? yield this.response.find(selector, params.contains, opts)
: params.matches
? yield this.response.find(selector, params.matches, opts)
: yield this.response.find(selector, opts);
}));
const name = helpers_1.getFindName(params, selectors, 0);
const value = element === null ? helpers_1.wrapAsValue(this, null, name) : element;
this._assertExists(null, name, value);
return value;
}));
}
existsAll(selector, a, b) {
return __awaiter(this, void 0, void 0, function* () {
const selectors = util_1.toArray(selector);
const elements = yield this._findAllForSelectors(selectors, a, b);
this.assert(selectors.length > 1
? `${selectors.join(", ")} all exist`
: `${selectors[0]} exists`, Object.values(elements)).every((element) => !element[0].isNullOrUndefined());
return util_1.flatten(elements);
});
}
existsAny(selectors, a, b) {
return __awaiter(this, void 0, void 0, function* () {
const elements = yield this._findAllForSelectors(selectors, a, b);
this.assert(selectors.length > 1
? `${selectors.join(", ")} some exist`
: `${selectors[0]} exists`, Object.values(elements)).some((element) => !element[0].isNullOrUndefined());
return util_1.flatten(elements);
});
}
find(selector, a, b) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const selectors = util_1.toArray(selector);
const params = helpers_1.getFindParams(a, b);
const element = yield util_1.asyncUntil(selectors, (selector) => __awaiter(this, void 0, void 0, function* () {
const value = typeof a == "string"
? yield this.response.find(selector, a, b)
: a instanceof RegExp
? yield this.response.find(selector, a, b)
: yield this.response.find(selector, b);
return value.isNullOrUndefined() ? false : value;
}));
return (element === null
? helpers_1.wrapAsValue(this, null, helpers_1.getFindName(params, selectors, null))
: element);
}));
}
findAll(selector, a, b) {
return __awaiter(this, void 0, void 0, function* () {
const selectors = util_1.toArray(selector);
const elements = yield this._findAllForSelectors(selectors, a, b);
return util_1.flatten(elements);
});
}
findXPath(xPath) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
return this.response.findXPath(xPath);
}));
}
findAllXPath(xPath) {
return this.response.findAllXPath(xPath);
}
submit(selector) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
const el = yield this.exists(selector);
if (el.isTag()) {
el.submit();
}
return el;
}));
}
logFailure(message, errorDetails, sourceCode, highlightText) {
const result = new assertion_result_1.AssertionFail(message, errorDetails, sourceCode, highlightText);
this.scenario.result(result);
return result;
}
logOptionalFailure(message, errorDetails) {
const result = new assertion_result_1.AssertionFailOptional(message, errorDetails);
this.scenario.result(result);
return result;
}
logPassing(message) {
const result = new assertion_result_1.AssertionPass(message);
this.scenario.result(result);
return result;
}
click(selector, a, b) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
return typeof a == "string"
? this.response.click(selector, a, b)
: a instanceof RegExp
? this.response.click(selector, a, b)
: this.response.click(selector, b);
}));
}
openInBrowser() {
return __awaiter(this, void 0, void 0, function* () {
const output = this.response.body.toString();
const filePath = yield util_1.openInBrowser(output);
this.scenario.comment(`Open response in browser temp file: ${filePath}`);
return filePath;
});
}
screenshot(a, b) {
const output = (() => {
if (typeof a === "string") {
return b ? this.response.screenshot(a, b) : this.response.screenshot(a);
}
return a ? this.response.screenshot(a) : this.response.screenshot();
})();
this._completedAction("SCREENSHOT");
return output;
}
movePointer(...pointers) {
return this.response.movePointer(...pointers);
}
gesture(type, opts) {
return this.response.gesture(type, opts);
}
push(key, value) {
this.scenario.push(key, value);
return this;
}
set(aliasName, value) {
this.scenario.set(aliasName, value);
return this;
}
get(aliasName) {
return this.scenario.get(aliasName);
}
scrollTo(point) {
return __awaiter(this, void 0, void 0, function* () {
yield this.response.scrollTo(point);
return this;
});
}
count(arr, callback) {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
if (callback) {
const n = yield util_1.asyncCount(arr, callback);
return helpers_1.wrapAsValue(this, n, "Count");
}
return helpers_1.wrapAsValue(this, arr.length, "Count");
}));
}
abort(message) {
return __awaiter(this, void 0, void 0, function* () {
yield this.scenario.abort(message);
});
}
rotateScreen(rotation) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.response.rotateScreen(rotation);
});
}
getScreenProperties() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.response.getScreenProperties();
});
}
hideKeyboard() {
return __awaiter(this, void 0, void 0, function* () {
yield this.response.hideKeyboard();
});
}
getSource() {
return value_promise_1.ValuePromise.execute(() => __awaiter(this, void 0, void 0, function* () {
return yield this.response.getSource();
}));
}
_findAllForSelectors(selectors, a, b) {
return __awaiter(this, void 0, void 0, function* () {
return util_1.asyncMapToObject(selectors, (selector) => __awaiter(this, void 0, void 0, function* () {
return typeof a == "string"
? this.response.findAll(selector, a, b)
: a instanceof RegExp
? this.response.findAll(selector, a, b)
: this.response.findAll(selector, a);
}));
});
}
_completedAction(verb, noun) {
return __awaiter(this, void 0, void 0, function* () {
this.scenario.result(new assertion_result_1.AssertionActionCompleted(verb, noun || ""));
});
}
_failedAction(verb, noun) {
return __awaiter(this, void 0, void 0, function* () {
this.scenario.result(new assertion_result_1.AssertionActionFailed(verb, noun || ""));
});
}
_assertExists(message, name, el) {
if (message) {
el.isNullOrUndefined()
? this.scenario.result(new assertion_result_1.AssertionFail(message, name))
: this.scenario.result(new assertion_result_1.AssertionPass(message));
}
else {
el.isNullOrUndefined()
? this._failedAction("EXISTS", `${name}`)
: this._completedAction("EXISTS", `${name}`);
}
}
}
exports.AssertionContext = AssertionContext;
//# sourceMappingURL=assertion-context.js.map