flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
312 lines • 12.4 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const response_1 = require("./response");
const assertion_1 = require("./assertion");
const domelement_1 = require("./domelement");
const _1 = require(".");
class AssertionContext {
get _isBrowserRequest() {
return this._response.isBrowser;
}
get _isHtmlRequest() {
return this._scenario.responseType == response_1.ResponseType.html;
}
get response() {
return this._response;
}
get scenario() {
return this._scenario;
}
get suite() {
return this._scenario.suite;
}
get browser() {
return this._isBrowserRequest ?
this._scenario.getBrowser() :
null;
}
get page() {
const browser = this.browser;
return browser !== null ?
browser.getPage() :
null;
}
constructor(scenario, response) {
this._scenario = scenario;
this._response = response;
}
comment(message) {
this._scenario.comment(message);
}
assert(a, b) {
const value = typeof b !== 'undefined' ? b : a;
const message = typeof b !== 'undefined' ? a : undefined;
return new assertion_1.Assertion(this, value, message);
}
pause(milliseconds) {
return new Promise((resolve) => {
setTimeout(() => {
this.comment('Paused ' + milliseconds + ' milliseconds');
resolve();
}, milliseconds);
});
}
findHavingText(selector, searchForText) {
return __awaiter(this, void 0, void 0, function* () {
if ((this._isBrowserRequest && this.page !== null) ||
this._isHtmlRequest) {
let matchingElement = null;
const elements = yield this.findAll(selector);
for (let i = 0; i < elements.length && matchingElement === null; i++) {
const element = elements[i];
const text = yield element.getText();
if (typeof searchForText == 'string') {
if (text.toString() == String(searchForText)) {
matchingElement = element;
}
}
else {
if (searchForText.test(text.toString())) {
matchingElement = element;
}
}
}
return matchingElement;
}
throw new Error('findHavingText is not available in this context');
});
}
;
findAllHavingText(selector, searchForText) {
return __awaiter(this, void 0, void 0, function* () {
if ((this._isBrowserRequest && this.page !== null) ||
this._isHtmlRequest) {
let matchingElements = [];
const elements = yield this.findAll(selector);
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
const text = yield element.getText();
if (typeof searchForText == 'string') {
if (text.toString() == String(searchForText)) {
matchingElements.push(element);
}
}
else {
if (searchForText.test(text.toString())) {
matchingElements.push(element);
}
}
}
return matchingElements;
}
throw new Error('findAllHavingText is not available in this context');
});
}
;
clearThenType(selector, textToType, opts = {}) {
return __awaiter(this, void 0, void 0, function* () {
yield this.clear(selector);
return this.type(selector, textToType, opts);
});
}
clear(selector) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
const input = yield this.page.$(selector);
if (input !== null) {
yield input.click({ clickCount: 3 });
return yield this.page.keyboard.press('Backspace');
}
}
else if (this._isHtmlRequest) {
const htmlResponse = this.response;
return yield htmlResponse.evaluate(this, function ($) {
$.find(selector).val('');
});
}
throw new Error(`Can not type into this element ${selector}`);
});
}
type(selector, textToType, opts = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
return yield this.page.type(selector, textToType, opts);
}
else if (this._isHtmlRequest) {
const htmlResponse = this.response;
return yield htmlResponse.evaluate(this, function ($) {
let currentValue = $(selector).val();
$(selector).val(currentValue + textToType);
});
}
throw new Error(`Can not type into element ${selector}`);
});
}
select(selector, value) {
if (this._isBrowserRequest && this.page !== null) {
const values = (typeof value == 'string') ? [value] : value;
return this.page.select.apply(null, [selector].concat(values));
}
throw new Error('Select not available in this context.');
}
evaluate(callback) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.response.evaluate(this, callback);
});
}
waitForReady(timeout = 15000) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
yield this.page.waitForNavigation({
timeout: timeout,
waitUntil: 'domcontentloaded'
});
return;
}
return this.pause(1);
});
}
waitForLoad(timeout = 30000) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
yield this.page.waitForNavigation({
timeout: timeout,
waitUntil: 'load'
});
return;
}
return this.pause(1);
});
}
waitForNetworkIdle(timeout = 10000) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
yield this.page.waitForNavigation({
timeout: timeout,
waitUntil: 'networkidle0'
});
return;
}
return this.pause(1);
});
}
waitForNavigation(timeout = 10000, waitFor) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
const allowedOptions = ["load", "domcontentloaded", "networkidle0", "networkidle2"];
const waitForEvent = (() => {
if (typeof waitFor == 'string' && allowedOptions.indexOf(waitFor) >= 0) {
return [waitFor];
}
else if (_1.Flagpole.toType(waitFor) == 'array' &&
waitFor.every((waitForItem) => {
return (allowedOptions.indexOf(waitForItem) >= 0);
})) {
return waitFor;
}
else {
return ["networkidle2"];
}
})();
yield this.page.waitForNavigation({
timeout: timeout,
waitUntil: waitForEvent
});
return;
}
return this.pause(1);
});
}
waitForHidden(selector, timeout = 100) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
const opts = { timeout: timeout || 100, hidden: true };
const element = yield this.page.waitForSelector(selector, opts);
return domelement_1.DOMElement.create(element, this, selector, selector);
}
else if (this._isHtmlRequest) {
return this.find(selector);
}
throw new Error('waitForHidden is not available in this context');
});
}
waitForVisible(selector, timeout = 100) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
const opts = { timeout: timeout || 100, visible: true };
const element = yield this.page.waitForSelector(selector, opts);
return domelement_1.DOMElement.create(element, this, selector, selector);
}
else if (this._isHtmlRequest) {
return this.find(selector);
}
throw new Error('waitForVisible is not available in this context');
});
}
waitForExists(selector, timeout) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest && this.page !== null) {
const opts = { timeout: timeout || 100 };
const element = yield this.page.waitForSelector(selector, opts);
return domelement_1.DOMElement.create(element, this, selector, selector);
}
else if (this._isHtmlRequest) {
return this.find(selector);
}
throw new Error('waitForExists is not available in this context');
});
}
find(selector) {
return this.response.find(selector);
}
findAll(selector) {
return this.response.findAll(selector);
}
submit(selector) {
return __awaiter(this, void 0, void 0, function* () {
const el = yield this.find(selector);
if (el === null) {
throw new Error(`Element with selector ${selector} not found.`);
}
return el.submit();
});
}
click(selector) {
return __awaiter(this, void 0, void 0, function* () {
const el = yield this.find(selector);
if (el === null) {
throw new Error(`Element with selector ${selector} not found.`);
}
return el.click();
});
}
openInBrowser() {
return __awaiter(this, void 0, void 0, function* () {
const output = this.response.body.toString();
const filePath = yield _1.Flagpole.openInBrowser(output);
this.scenario.comment(`Open response in browser temp file: ${filePath}`);
return filePath;
});
}
screenshot(opts) {
return __awaiter(this, void 0, void 0, function* () {
if (this._isBrowserRequest) {
if (this.page !== null) {
return yield this.page.screenshot(opts);
}
throw new Error(`No page found, so can't take a screenshot.`);
}
throw new Error(`This scenario type (${this.response.typeName}) does not support screenshots.`);
});
}
}
exports.AssertionContext = AssertionContext;
//# sourceMappingURL=assertioncontext.js.map