flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
167 lines • 6.27 kB
JavaScript
"use strict";
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 url_1 = require("url");
const value_1 = require("./value");
const enums_1 = require("./enums");
const httpresponse_1 = require("./httpresponse");
const assertioncontext_1 = require("./assertioncontext");
function isPuppeteer(type) {
return [
enums_1.ResponseType.browser,
enums_1.ResponseType.extjs
].indexOf(type) >= 0;
}
exports.isPuppeteer = isPuppeteer;
class ProtoResponse {
constructor(scenario) {
this._httpResponse = httpresponse_1.HttpResponse.createEmpty();
this.scenario = scenario;
}
get isBrowser() {
return false;
}
get httpResponse() {
return this._httpResponse;
}
get statusCode() {
return this._wrapAsValue(this.httpResponse.statusCode, 'HTTP Status Code');
}
get statusMessage() {
return this._wrapAsValue(this.httpResponse.statusMessage, 'HTTP Status Message');
}
get body() {
return this._wrapAsValue(this.httpResponse.body, 'Raw Response Body');
}
get length() {
return this._wrapAsValue(this.httpResponse.body.length, 'Length of Response Body');
}
get headers() {
return this._wrapAsValue(this.httpResponse.headers, 'HTTP Headers');
}
get cookies() {
return this._wrapAsValue(this.httpResponse.cookies, 'HTTP Cookies');
}
get jsonBody() {
try {
const json = JSON.parse(this.httpResponse.body);
return this._wrapAsValue(json, 'JSON Response');
}
catch (ex) {
return this._wrapAsValue(null, 'JSON Response');
}
}
get url() {
return this._wrapAsValue(this.scenario.url, 'Request URL');
}
get finalUrl() {
return this._wrapAsValue(this.scenario.finalUrl, 'Response URL (after redirects)');
}
get loadTime() {
return this._wrapAsValue(this.scenario.requestDuration, 'Request to Response Load Time');
}
get context() {
return new assertioncontext_1.AssertionContext(this.scenario, this);
}
init(httpResponse) {
this._httpResponse = httpResponse;
}
absolutizeUri(uri) {
let baseUrl = new url_1.URL(this.scenario.suite.buildUrl(this.scenario.url || ''));
return (new url_1.URL(uri, baseUrl.href)).href;
}
getRoot() {
return this.httpResponse.body;
}
header(key) {
key = typeof this.httpResponse.headers[key] !== 'undefined' ? key : key.toLowerCase();
const headerValue = this.httpResponse.headers[key];
return this._wrapAsValue(typeof headerValue == 'undefined' ? null : headerValue, 'HTTP Headers[' + key + ']');
}
cookie(key) {
let cookie = null;
this.httpResponse.cookies.forEach((c) => {
if (c.key == key) {
cookie = c;
}
});
return this._wrapAsValue(cookie, 'HTTP Cookies[' + key + ']');
}
waitForNavigation(timeout = 10000, waitFor) {
return __awaiter(this, void 0, void 0, function* () {
return this.context.pause(1);
});
}
waitForLoad(timeout = 30000) {
return __awaiter(this, void 0, void 0, function* () {
return this.context.pause(1);
});
}
waitForReady(timeout = 30000) {
return __awaiter(this, void 0, void 0, function* () {
return this.context.pause(1);
});
}
waitForNetworkIdle(timeout = 30000) {
return __awaiter(this, void 0, void 0, function* () {
return this.context.pause(1);
});
}
waitForHidden(selector, timeout = 30000) {
return __awaiter(this, void 0, void 0, function* () {
return this.context.pause(1);
});
}
waitForVisible(selector, timeout = 30000) {
return __awaiter(this, void 0, void 0, function* () {
return this.context.pause(1);
});
}
waitForExists(selector, timeout = 30000) {
return __awaiter(this, void 0, void 0, function* () {
return this.context.pause(1);
});
}
screenshot(opts) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`This scenario type (${this.responseTypeName}) does not support screenshots.`);
});
}
type(selector, textToType, opts = {}) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`This scenario type (${this.responseTypeName}) does not support type.`);
});
}
clear(selector) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`This scenario type (${this.responseTypeName}) does not support clear.`);
});
}
findHavingText(selector, searchForText) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`This scenario type (${this.responseTypeName}) does not support findHavingText.`);
});
}
findAllHavingText(selector, searchForText) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`This scenario type (${this.responseTypeName}) does not support findAllHavingText.`);
});
}
selectOption(selector, value) {
return __awaiter(this, void 0, void 0, function* () {
throw new Error(`This scenario type (${this.responseTypeName}) does not support selectOption.`);
});
}
_wrapAsValue(data, name, source) {
return new value_1.Value(data, this.context, name, source);
}
}
exports.ProtoResponse = ProtoResponse;
//# sourceMappingURL=response.js.map