UNPKG

detox-allure2-adapter

Version:
144 lines 5.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatWebExpectation = exports.formatWebAction = exports.formatWebPredicate = void 0; const utils_1 = require("../../utils"); const predicate_formatters_1 = require("./predicate-formatters"); function formatWebViewContext(predicate) { const webView = (0, utils_1.msg)('WebView'); if (!predicate) { return (0, utils_1.glue)(webView, ':'); } return (0, utils_1.glue)(webView, ' ', (0, predicate_formatters_1.formatPredicate)(predicate), ':'); } function formatWebPredicate(predicate, atIndex, prefix = '') { if (!predicate) { return (0, utils_1.msg)('?'); } const { type, value } = predicate; const _ = (name) => prefix && name ? `${prefix}_${name}` : prefix || name || ''; const index$ = atIndex == null ? '' : `[${atIndex}]`; const args = { [_(type)]: value }; if (index$) { args[_(type) + '_index'] = `${atIndex}`; } switch (type) { case 'id': { return (0, utils_1.msg)(`#${value}${index$}`, args); } case 'class': { return (0, utils_1.msg)(`.${value}${index$}`, args); } case 'tag': { return (0, utils_1.msg)(`${value}${index$}`, args); } case 'css': { return (0, utils_1.msg)(`${value}${index$}`, args); } case 'xpath': { return (0, utils_1.msg)(`xpath:${value}${index$}`, args); } case 'name': case 'label': case 'href': { return (0, utils_1.msg)(`[${type}=${value}]${index$}`, args); } case 'hrefContains': { return (0, utils_1.msg)(`[href*=${value}]${index$}`, args); } default: { return (0, utils_1.msg)(`[${type}=${value}]${index$}`, args); } } } exports.formatWebPredicate = formatWebPredicate; const formatWebExpectationVerb = (invocation) => { const hasNot = invocation.webModifiers?.includes('not'); const expectation = invocation.webExpectation || ''; const verb = expectation .replace(/([A-Z])/g, ' $1') .toLowerCase() .trim(); const toOrNotTo = (0, utils_1.msg)(hasNot ? 'not to' : 'to'); const formatExpectationClause = () => { const [expected] = invocation.params || []; switch (expectation) { case 'toHaveText': { return (0, utils_1.msg)(`have text "${expected}"`, { expected_text: expected }); } default: { return (0, utils_1.msg)(verb.replace(/^to /, '')); } } }; return (0, utils_1.concat)(toOrNotTo, formatExpectationClause()); }; const formatWebAction = (invocation) => { const { webAction, webPredicate, webAtIndex, params, predicate } = invocation; if (!webAction) { return null; } const context = formatWebViewContext(predicate); const target = formatWebPredicate(webPredicate, webAtIndex, 'web'); switch (webAction) { case 'scrollToView': { return (0, utils_1.concat)(context, 'Scroll to', target); } case 'tap': { return (0, utils_1.concat)(context, 'Tap', target); } case 'clearText': { return (0, utils_1.concat)(context, 'Clear text in', target); } case 'focus': { return (0, utils_1.concat)(context, 'Focus on', target); } case 'moveCursorToEnd': { return (0, utils_1.concat)(context, 'Move cursor to end in', target); } case 'selectAllText': { return (0, utils_1.concat)(context, 'Select all text in', target); } case 'getText': { return (0, utils_1.concat)(context, 'Get text from', target); } case 'getTitle': { return (0, utils_1.concat)(context, 'Get title of', target); } case 'getCurrentUrl': { return (0, utils_1.concat)(context, 'Get current URL'); } case 'typeText': { const [text] = params; return (0, utils_1.concat)(context, 'Type', (0, utils_1.msg)(`"${text}"`, { text }), 'in', target); } case 'replaceText': { const [text] = params; return (0, utils_1.concat)(context, 'Replace text in', target, 'with', (0, utils_1.msg)(`"${text}"`, { text })); } case 'runScript': { const [script] = params; return (0, utils_1.concat)(context, (0, utils_1.msg)('Run script on', { script: (0, utils_1.truncate)(script, 1000) }), target); } case 'runScriptWithArgs': { const [script, args] = params; const spreadArgs = (args || []).reduce((acc, arg, index) => { acc[`arg${index}`] = arg; return acc; }, {}); return (0, utils_1.concat)(context, (0, utils_1.msg)('Run script with args on', { script: (0, utils_1.truncate)(script, 1000), ...spreadArgs }), target); } default: { return (0, utils_1.msg)(`Web action: ${webAction}`); } } }; exports.formatWebAction = formatWebAction; const formatWebExpectation = (invocation) => { if (!invocation.webExpectation) { return null; } const context = formatWebViewContext(invocation.predicate); return (0, utils_1.concat)(context, 'Expect', formatWebPredicate(invocation.webPredicate, invocation.webAtIndex, 'web'), formatWebExpectationVerb(invocation)); }; exports.formatWebExpectation = formatWebExpectation; //# sourceMappingURL=web-formatters.js.map