@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
245 lines • 12.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.O3rComponentFixture = void 0;
var tslib_1 = require("tslib");
var index_1 = require("../../errors/index");
var helpers_1 = require("../helpers");
var element_1 = require("./element");
/**
* Implementation of the fixture dedicated to Playwright, hence using the webdriver to interact with the dom.
*/
var O3rComponentFixture = /** @class */ (function () {
/**
* Root element of this fixture.
* 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 element ElementProfile to test
* @param timeout specific timeout that will throw when reach
*/
O3rComponentFixture.prototype.throwOnUndefinedElement = function (element, timeout) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!element) {
throw new Error('Element not found in ' + this.constructor.name);
}
return [4 /*yield*/, element.sourceElement.element.first().waitFor({ state: 'attached', timeout: timeout })];
case 1:
_a.sent();
return [2 /*return*/, element];
}
});
});
};
/**
* Throws an exception if the element is undefined.
* Otherwise returns the element.
* @param element ElementProfile to test
* @param timeout specific timeout that will throw when reach
*/
O3rComponentFixture.prototype.throwOnUndefined = function (element, timeout) {
var _this = this;
return (0, helpers_1.withTimeout)((function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var el;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, element];
case 1:
el = _a.sent();
return [4 /*yield*/, el.sourceElement.element.first().waitFor({ state: 'attached' })];
case 2:
_a.sent();
return [2 /*return*/, el];
}
});
}); })(), timeout);
};
/**
* 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 element;
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:
element = _a.sent();
if (options.shouldThrowIfNotPresent) {
return [2 /*return*/, this.throwOnUndefinedElement(element, options.timeout)];
}
return [2 /*return*/, element];
}
});
});
};
/**
* 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 element;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
element = _a.sent();
return [4 /*yield*/, element.isVisible()];
case 2:
if (!(_a.sent())) {
return [2 /*return*/];
}
return [4 /*yield*/, element.getText()];
case 3: return [2 /*return*/, _a.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 element;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
element = _a.sent();
return [4 /*yield*/, element.isVisible()];
case 2: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* 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 element;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
element = _a.sent();
return [4 /*yield*/, element.isVisible()];
case 2:
if (!_a.sent()) return [3 /*break*/, 4];
return [4 /*yield*/, element.click()];
case 3:
_a.sent();
_a.label = 4;
case 4: return [2 /*return*/];
}
});
});
};
O3rComponentFixture.prototype.query = function (selector, returnType) {
var elements = this.rootElement.sourceElement.element.locator(selector);
var element = elements.first();
var selectedElement = { element: element, page: this.rootElement.sourceElement.page };
return Promise.resolve(new (returnType || element_1.O3rElement)(selectedElement));
};
O3rComponentFixture.prototype.queryNth = function (selector, index, returnType) {
var elements = this.rootElement.sourceElement.element.locator(selector);
var element = elements.nth(index);
var selectedElement = { element: element, page: this.rootElement.sourceElement.page };
return Promise.resolve(new (returnType || element_1.O3rElement)(selectedElement));
};
O3rComponentFixture.prototype.queryAll = function (selector_1, returnType_1, groupType_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, returnType, groupType, timeout) {
var sourceElement, pElements, pElementsCount, elements, i, selectedElement, group, isValid, err_1;
if (timeout === void 0) { timeout = 5000; }
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 5, , 6]);
sourceElement = this.rootElement.sourceElement.element;
pElements = sourceElement.locator(selector);
// Mandatory because count is not reliable if we don't wait for the list to be attached
return [4 /*yield*/, pElements.first().waitFor({ state: 'attached', timeout: timeout })];
case 1:
// Mandatory because count is not reliable if we don't wait for the list to be attached
_a.sent();
return [4 /*yield*/, pElements.count()];
case 2:
pElementsCount = _a.sent();
elements = [];
for (i = 0; i < pElementsCount; i++) {
selectedElement = { element: pElements.nth(i), page: this.rootElement.sourceElement.page };
elements.push(returnType ? new returnType(selectedElement) : new element_1.O3rElement(selectedElement));
}
if (!groupType) return [3 /*break*/, 4];
group = new groupType(elements);
return [4 /*yield*/, group.isValidGroup()];
case 3:
isValid = _a.sent();
if (!isValid) {
throw new index_1.FixtureUsageError('invalid group of items');
}
return [2 /*return*/, group];
case 4: return [2 /*return*/, elements];
case 5:
err_1 = _a.sent();
// eslint-disable-next-line no-console -- no other logger available
console.warn("Failed to query all ".concat(selector), err_1);
return [2 /*return*/, Promise.resolve([])];
case 6: return [2 /*return*/];
}
});
});
};
/** @inheritdoc */
O3rComponentFixture.prototype.getElement = function () {
return this.rootElement;
};
/** @inheritdoc */
O3rComponentFixture.prototype.getSubComponents = function () {
return Promise.resolve({ block: [this] });
};
/** @inheritDoc */
O3rComponentFixture.prototype.queryNotPresent = function (selector) {
var element = this.rootElement.sourceElement.element.locator(selector).first();
return element.isHidden();
};
return O3rComponentFixture;
}());
exports.O3rComponentFixture = O3rComponentFixture;
//# sourceMappingURL=component-fixture.js.map