UNPKG

@o3r/testing

Version:

The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.

134 lines 6.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.O3rComponentFixture = void 0; const platform_browser_1 = require("@angular/platform-browser"); const index_1 = require("../../errors/index"); const helpers_1 = require("../helpers"); const element_1 = require("./element"); /** * Implementation of the fixture dedicated to angular, hence using angular testing framework. */ class O3rComponentFixture { /** * Root element of this fixture. It will be used as the context for further queries. * @param element */ constructor(element) { this.element = 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 */ throwOnUndefinedElement(element, _timeout) { if (!element) { throw new Error('Element not found in ' + this.constructor.name); } return Promise.resolve(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 */ async throwOnUndefined(element, timeout) { return (0, helpers_1.withTimeout)(element, timeout) .then((el) => { if (!el) { throw new Error('Element not found in ' + this.constructor.name); } return el; }); } async queryWithOptions(selector, elementConstructor, options = {}) { const element = await (options.index === undefined ? this.query(selector, elementConstructor) : this.queryNth(selector, options.index, elementConstructor)); if (options.shouldThrowIfNotPresent) { return this.throwOnUndefinedElement(element, options.timeout); } 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 */ async getText(selector, options = {}) { const element = await this.queryWithOptions(selector, options.elementConstructor, options); if (!element || !await element.isVisible()) { return; } return await element.getText(); } /** * 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 */ async isVisible(selector, options = {}) { const element = await this.queryWithOptions(selector, options.elementConstructor, options); return !!element && await element.isVisible(); } /** * 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 */ async click(selector, options = {}) { const element = await this.queryWithOptions(selector, options.elementConstructor, options); if (!!element && await element.isVisible()) { await element.click(); } } query(selector, returnType) { const queriedElement = this.element.sourceElement.query(platform_browser_1.By.css(selector)); return Promise.resolve(queriedElement ? (returnType ? new returnType(queriedElement) : new element_1.O3rElement(queriedElement)) : undefined); } queryNth(selector, index, returnType) { const queriedElement = this.element.sourceElement.queryAll(platform_browser_1.By.css(selector))[index]; return Promise.resolve(queriedElement ? (returnType ? new returnType(queriedElement) : new element_1.O3rElement(queriedElement)) : undefined); } async queryAll(selector, returnType, groupType, _timeout) { const queriedElement = this.element.sourceElement.queryAll(platform_browser_1.By.css(selector)); const elements = queriedElement.map((el) => (returnType ? new returnType(el) : new element_1.O3rElement(el))); if (groupType) { const group = new groupType(elements); const isValid = await group.isValidGroup(); if (!isValid) { throw new index_1.FixtureUsageError('invalid group of items'); } return Promise.resolve(group); } return Promise.resolve(elements); } /** @inheritdoc */ getElement() { return this.element; } /** @inheritdoc */ getSubComponents() { return Promise.resolve({ block: [this] }); } /** @inheritDoc */ queryNotPresent(selector, _timeout) { return Promise.resolve(!this.element.sourceElement.query(platform_browser_1.By.css(selector))); } } exports.O3rComponentFixture = O3rComponentFixture; //# sourceMappingURL=component-fixture.js.map