@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
274 lines • 14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.O3rComponentFixture = void 0;
var tslib_1 = require("tslib");
var protractor_1 = require("protractor");
var index_1 = require("../../errors/index");
var helpers_1 = require("../helpers");
var element_1 = require("./element");
var utils_1 = require("./utils");
/**
* Implementation of the fixture dedicated to protractor, hence using the webdriver to interact with the dom.
* @deprecated Will be removed in v13, please use Playwright instead
*/
var O3rComponentFixture = /** @class */ (function () {
/**
* Root element of this fixture. Optional in a Protractor.
* All further queries will be applied to the element tree if any, otherwise they will be applied to the whole DOM.
* @param rootElement
*/
function O3rComponentFixture(rootElement) {
this.rootElement = rootElement;
}
/**
* Throws an exception if the element is undefined.
* Otherwise returns the element.
* @param elemnt ElementProfile to test
* @param _timeout specific timeout that will throw when reach
*/
O3rComponentFixture.prototype.throwOnUndefinedElement = function (elemnt, _timeout) {
if (!elemnt) {
throw new Error('Element not found in ' + this.constructor.name);
}
return Promise.resolve(elemnt);
};
/**
* Throws an exception if the element is undefined.
* Otherwise returns the element.
* @param elemnt ElementProfile to test
* @param timeout specific timeout that will throw when reach
*/
O3rComponentFixture.prototype.throwOnUndefined = function (elemnt, timeout) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _this = this;
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, (0, helpers_1.withTimeout)(elemnt, timeout)
.then(function (el) {
if (!el) {
throw new Error('Element not found in ' + _this.constructor.name);
}
return el;
})];
});
});
};
/**
* Get the element associated to the selector if present
* @param selector Selector to access the element
* @param elementConstructor Constructor that will be used to create the Element, defaults to O3rElement
* @param options Options supported
* @param options.index index Select the element associated to the index
* @param options.shouldThrowIfNotPresent If set to true the function will throw if the element is not present
* @param options.timeout Duration to wait for the element to be present before it throws
*/
O3rComponentFixture.prototype.queryWithOptions = function (selector_1, elementConstructor_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, elementConstructor, options) {
var queryElement;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (options.index === undefined
? this.query(selector, elementConstructor)
: this.queryNth(selector, options.index, elementConstructor))];
case 1:
queryElement = _a.sent();
if (options.shouldThrowIfNotPresent) {
return [2 /*return*/, this.throwOnUndefinedElement(queryElement, options.timeout)];
}
return [2 /*return*/, queryElement];
}
});
});
};
/**
* Get text from the element associated to the given selector, or undefined if the element is not found or not visible
* @param selector Selector to access the element
* @param options Options supported
* @param options.elementConstructor Constructor that will be used to create the Element, defaults to O3rElement
* @param options.index index Select the element associated to the index
* @param options.shouldThrowIfNotPresent If set to true the function will throw if the element is not present
* @param options.timeout Duration to wait for the element to be present before it throws
*/
O3rComponentFixture.prototype.getText = function (selector_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, options) {
var getTextElement, _a;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
getTextElement = _b.sent();
_a = !getTextElement;
if (_a) return [3 /*break*/, 3];
return [4 /*yield*/, getTextElement.isVisible()];
case 2:
_a = !(_b.sent());
_b.label = 3;
case 3:
if (_a) {
return [2 /*return*/];
}
return [4 /*yield*/, getTextElement.getText()];
case 4: return [2 /*return*/, _b.sent()];
}
});
});
};
/**
* Check if the element associated to the given selector is visible
* @param selector Selector to access the element
* @param options Options supported
* @param options.elementConstructor Constructor that will be used to create the Element, defaults to O3rElement
* @param options.index index Select the element associated to the index
* @param options.shouldThrowIfNotPresent If set to true the function will throw if the element is not present
* @param options.timeout Duration to wait for the element to be present before it throws
*/
O3rComponentFixture.prototype.isVisible = function (selector_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, options) {
var isVisibleElement, _a;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
isVisibleElement = _b.sent();
_a = !!isVisibleElement;
if (!_a) return [3 /*break*/, 3];
return [4 /*yield*/, isVisibleElement.isVisible()];
case 2:
_a = (_b.sent());
_b.label = 3;
case 3: return [2 /*return*/, _a];
}
});
});
};
/**
* Click on the element associated to the given selector if it exists and is visible
* @param selector Selector to access the element
* @param options Options supported
* @param options.elementConstructor Constructor that will be used to create the Element, defaults to O3rElement
* @param options.index index Select the element associated to the index
* @param options.shouldThrowIfNotPresent If set to true the function will throw if the element is not present
* @param options.timeout Duration to wait for the element to be present before it throws
*/
O3rComponentFixture.prototype.click = function (selector_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, options) {
var clickElement, _a;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
clickElement = _b.sent();
_a = !!clickElement;
if (!_a) return [3 /*break*/, 3];
return [4 /*yield*/, clickElement.isVisible()];
case 2:
_a = (_b.sent());
_b.label = 3;
case 3:
if (!_a) return [3 /*break*/, 5];
return [4 /*yield*/, clickElement.click()];
case 4:
_b.sent();
_b.label = 5;
case 5: return [2 /*return*/];
}
});
});
};
O3rComponentFixture.prototype.query = function (selector, returnType) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var sourceElement, cssSelector, isElPresent, queriedElement;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
sourceElement = this.rootElement && this.rootElement.sourceElement;
cssSelector = protractor_1.By.css(selector);
return [4 /*yield*/, (sourceElement ? sourceElement.isElementPresent(cssSelector) : protractor_1.browser.isElementPresent(cssSelector))];
case 1:
isElPresent = _a.sent();
if (!isElPresent) {
// eslint-disable-next-line no-console -- no other logger available
console.warn("No component matching ".concat(selector, " found"));
return [2 /*return*/];
}
queriedElement = sourceElement ? sourceElement.element(cssSelector) : (0, protractor_1.element)(cssSelector);
return [2 /*return*/, returnType ? new returnType(queriedElement) : new element_1.O3rElement(queriedElement)];
}
});
});
};
O3rComponentFixture.prototype.queryNth = function (selector, index, returnType) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var sourceElement, item;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
sourceElement = this.rootElement && this.rootElement.sourceElement;
item = (sourceElement || protractor_1.element).all(protractor_1.By.css(selector)).get(index);
return [4 /*yield*/, item.isPresent()];
case 1:
if (!(_a.sent())) {
// eslint-disable-next-line no-console -- no other logger available
console.warn("No component matching ".concat(selector, ":nth(").concat(index, ") found"));
return [2 /*return*/];
}
return [2 /*return*/, new (returnType || element_1.O3rElement)(item)];
}
});
});
};
O3rComponentFixture.prototype.queryAll = function (selector, returnType, groupType, _timeout) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var pElements, elements, group, isValid;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
pElements = (this.rootElement ? this.rootElement.sourceElement.all(protractor_1.By.css(selector)) : protractor_1.element.all(protractor_1.By.css(selector)))
.filter(function (item) { return typeof item !== 'undefined'; })
.then(function (elf) { return elf.map(function (item) { return (returnType ? new returnType(item) : new element_1.O3rElement(item)); }); });
return [4 /*yield*/, (0, utils_1.convertPromise)(pElements)];
case 1:
elements = _a.sent();
if (!groupType) return [3 /*break*/, 3];
group = new groupType(elements);
return [4 /*yield*/, group.isValidGroup()];
case 2:
isValid = _a.sent();
if (!isValid) {
throw new index_1.FixtureUsageError('invalid group of items');
}
return [2 /*return*/, Promise.resolve(group)];
case 3: return [2 /*return*/, elements];
}
});
});
};
/** @inheritdoc */
O3rComponentFixture.prototype.getElement = function () {
return this.rootElement;
};
/** @inheritdoc */
O3rComponentFixture.prototype.getSubComponents = function () {
return Promise.resolve({ block: [this] });
};
/** @inheritDoc */
O3rComponentFixture.prototype.queryNotPresent = function (selector, _timeout) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var sourceElement;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
sourceElement = this.rootElement && this.rootElement.sourceElement;
return [4 /*yield*/, (sourceElement || protractor_1.browser).isElementPresent(protractor_1.By.css(selector))];
case 1: return [2 /*return*/, !(_a.sent())];
}
});
});
};
return O3rComponentFixture;
}());
exports.O3rComponentFixture = O3rComponentFixture;
//# sourceMappingURL=component-fixture.js.map