UNPKG

qape

Version:

Monkey testing library

96 lines (77 loc) 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _AbstractAction = _interopRequireDefault(require("./AbstractAction")); var _faker = _interopRequireDefault(require("faker")); var _helpers = require("../../shared/helpers"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Type action, which will type a random text into a random * input element. Or a specific text into a specific element, * when actionsConfig is specified. * @extends AbstractAction */ class TypeAction extends _AbstractAction.default { /** * @returns {string} 'type' */ static get id() { return 'type'; } /** * Checks if an elements is a typable input or textarea * @param {puppeteer.ElementHandle} element * @returns {boolean} */ static isActionAvailable(element) { return element.executionContext().evaluate(element => { return element.matches( // Is typable input box 'input' + ':not([type="radio"])' + ':not([type="checkbox"])' + ':not([type="date"])' + ':not(:disabled)' + ':not([readonly])' + // Is typable textarea ', textarea' + ':not(:disabled)' + ':not([readonly])'); }, element); } /** * Performs the type action with following wrappers: * - Hover over the element * (So that in preview mode, you will see the element before typing) * - Highlight the element (Only in headfull mode) * - Type the configured or random text into the element * @param {puppeteer.ElementHandle} element * @param {puppeteer.Page} page * @returns {Promise} Resolves when typing is done */ async action(element, page) { this._text = this._getText(); await element.hover(); if (this._config.headlessModeDisabled) { await this._actionsHelper.highlightElement(element); await page.waitForTimeout(this._config.previewModePauseTime); } await element.type(this._text, { delay: this._config.typeActionDelay }); } /** * @returns {string} Text from action config, * or random string the list of available texts */ _getText() { if (this._actionConfig && this._actionConfig.text) { return this._actionConfig.text; } return _faker.default.fake(`{{${(0, _helpers.getRandomElementFromArray)(this._config.typeActionTextTypes)}}}`); } /** * Adds info to the action results * @param {Object} results * @returns {Object} */ async updateResults(results) { results.config.text = this._text; results.message = `Type '${this._text}' into ${results.html} [selector:"${results.config.selector}"]`; return results; } } exports.default = TypeAction;