|
| 1 | +import { When } from '@cucumber/cucumber'; |
| 2 | +import { getValue, getElement } from './transformers'; |
| 3 | +import memory from '@qavajs/memory'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Execute client function |
| 7 | + * @param {string} functionKey - memory key of function |
| 8 | + * @example I execute '$fn' function // fn is function reference |
| 9 | + * @example I execute 'window.scrollBy(0, 100)' function |
| 10 | + */ |
| 11 | +When('I execute {string} function', async function (functionKey) { |
| 12 | + const fn = await getValue(functionKey); |
| 13 | + await page.evaluate(fn); |
| 14 | +}); |
| 15 | + |
| 16 | +/** |
| 17 | + * Execute client function and save result into memory |
| 18 | + * @param {string} functionKey - memory key of function |
| 19 | + * @param {string} memoryKey - memory key to store result |
| 20 | + * @example I execute '$fn' function and save result as 'result' // fn is function reference |
| 21 | + * @example I execute 'window.scrollY' function and save result as 'scroll' |
| 22 | + */ |
| 23 | +When('I execute {string} function and save result as {string}', async function (functionKey, memoryKey) { |
| 24 | + const fn = await getValue(functionKey); |
| 25 | + memory.setValue(memoryKey, await page.evaluate(fn)); |
| 26 | +}); |
| 27 | + |
| 28 | +/** |
| 29 | + * Execute client function on certain element |
| 30 | + * @param {string} functionKey - memory key of function |
| 31 | + * @param {string} alias - alias of target element |
| 32 | + * @example I execute '$fn' function on 'Component > Element' // fn is function reference |
| 33 | + * @example I execute 'arguments[0].scrollIntoView()' function on 'Component > Element' |
| 34 | + */ |
| 35 | +When('I execute {string} function on {string}', async function (functionKey, alias) { |
| 36 | + let fn = await getValue(functionKey); |
| 37 | + const element = await getElement(alias); |
| 38 | + if (typeof fn === 'string') { |
| 39 | + fn = new Function('return ' + fn) |
| 40 | + } |
| 41 | + await element.evaluate(fn); |
| 42 | +}); |
| 43 | + |
| 44 | +/** |
| 45 | + * Execute client function on certain element |
| 46 | + * @param {string} functionKey - memory key of function |
| 47 | + * @param {string} alias - alias of target element |
| 48 | + * @example I execute '$fn' function on 'Component > Element' and save result as 'innerText' // fn is function reference |
| 49 | + * @example I execute 'arguments[0].innerText' function on 'Component > Element' and save result as 'innerText' |
| 50 | + */ |
| 51 | +When( |
| 52 | + 'I execute {string} function on {string} and save result as {string}', |
| 53 | + async function (functionKey, alias, memoryKey) { |
| 54 | + let fn = await getValue(functionKey); |
| 55 | + if (typeof fn === 'string') { |
| 56 | + fn = new Function('return ' + fn) |
| 57 | + } |
| 58 | + const element = await getElement(alias); |
| 59 | + memory.setValue(memoryKey, await element.evaluate(fn)); |
| 60 | + } |
| 61 | +); |
0 commit comments