|
| 1 | +import memory from '@qavajs/memory'; |
| 2 | +import { When } from '@cucumber/cucumber'; |
| 3 | +import { getElement, getValue } from './transformers'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Save text of element to memory |
| 7 | + * @param {string} alias - element to get value |
| 8 | + * @param {string} key - key to store value |
| 9 | + * @example I save text of '#1 of Search Results' as 'firstSearchResult' |
| 10 | + */ |
| 11 | +When('I save text of {string} as {string}', async function (alias, key) { |
| 12 | + const element = await getElement(alias); |
| 13 | + const value = await element.innerText(); |
| 14 | + memory.setValue(key, value); |
| 15 | +}); |
| 16 | + |
| 17 | +/** |
| 18 | + * Save property of element to memory |
| 19 | + * @param {string} property - property to store |
| 20 | + * @param {string} alias - element to get value |
| 21 | + * @param {string} key - key to store value |
| 22 | + * @example I save 'checked' property of 'Checkbox' as 'checked' |
| 23 | + * @example I save '$prop' property of 'Checkbox' as 'checked' |
| 24 | + */ |
| 25 | +When('I save {string} property of {string} as {string}', async function (property, alias, key) { |
| 26 | + const element = await getElement(alias); |
| 27 | + const propertyName = await getValue(property); |
| 28 | + const value = await element.evaluate((node: any, propertyName: string) => node[propertyName], propertyName); |
| 29 | + memory.setValue(key, value); |
| 30 | +}); |
| 31 | + |
| 32 | +/** |
| 33 | + * Save attribute of element to memory |
| 34 | + * @param {string} attribute - attribute to store |
| 35 | + * @param {string} alias - element to get value |
| 36 | + * @param {string} key - key to store value |
| 37 | + * @example I save 'href' attribute of 'Link' as 'linkHref' |
| 38 | + * @example I save '$prop' attribute of 'Link' as 'linkHref' |
| 39 | + */ |
| 40 | +When('I save {string} attribute of {string} as {string}', async function (attribute, alias, key) { |
| 41 | + const element = await getElement(alias); |
| 42 | + const attributeName = await getValue(attribute); |
| 43 | + const value = await element.getAttribute(attributeName); |
| 44 | + memory.setValue(key, value); |
| 45 | +}); |
| 46 | + |
| 47 | +/** |
| 48 | + * Save number of elements in collection to memory |
| 49 | + * @param {string} alias - collection to get value |
| 50 | + * @param {string} key - key to store value |
| 51 | + * @example I save number of elements in 'Search Results' as 'numberOfSearchResults' |
| 52 | + */ |
| 53 | +When('I save number of elements in {string} collection as {string}', async function (alias, key) { |
| 54 | + const collection = await getElement(alias); |
| 55 | + const value = await collection.count(); |
| 56 | + memory.setValue(key, value); |
| 57 | +}); |
| 58 | + |
| 59 | +/** |
| 60 | + * Save array of texts of collection to memory |
| 61 | + * @param {string} alias - collection to get values |
| 62 | + * @param {string} key - key to store value |
| 63 | + * @example I save text of every element of 'Search Results' collection as 'searchResults' |
| 64 | + */ |
| 65 | +When( |
| 66 | + 'I save text of every element of {string} collection as {string}', |
| 67 | + async function (alias: string, key: string) { |
| 68 | + const collection = await getElement(alias); |
| 69 | + const values = await collection.evaluateAll( |
| 70 | + (collection: Array<any>) => collection.map(e => e.innerText) |
| 71 | + ); |
| 72 | + memory.setValue(key, values); |
| 73 | + } |
| 74 | +); |
| 75 | + |
| 76 | +/** |
| 77 | + * Save array of attributes of collection to memory |
| 78 | + * @param {string} alias - collection to get values |
| 79 | + * @param {string} key - key to store value |
| 80 | + * @example I save 'checked' attribute of every element of 'Search > Checkboxes' collection as 'checkboxes' |
| 81 | + */ |
| 82 | +When( |
| 83 | + 'I save {string} attribute of every element of {string} collection as {string}', |
| 84 | + async function (attribute: string, alias: string, key: string) { |
| 85 | + const collection = await getElement(alias); |
| 86 | + const values = await collection.evaluateAll( |
| 87 | + (collection: Array<any>, attr: string) => collection.map(e => e.attributes[attr].value), |
| 88 | + attribute |
| 89 | + ); |
| 90 | + memory.setValue(key, values); |
| 91 | + } |
| 92 | +); |
| 93 | + |
| 94 | +/** |
| 95 | + * Save array of property of collection to memory |
| 96 | + * @param {string} alias - collection to get values |
| 97 | + * @param {string} key - key to store value |
| 98 | + * @example I save 'href' property of every element of 'Search > Links' collection as 'hrefs' |
| 99 | + */ |
| 100 | +When( |
| 101 | + 'I save {string} property of every element of {string} collection as {string}', |
| 102 | + async function (property: string, alias: string, key: string) { |
| 103 | + const collection = await getElement(alias); |
| 104 | + const values = await collection.evaluateAll( |
| 105 | + (collection: Array<any>, prop: string) => collection.map(e => e[prop]), |
| 106 | + property |
| 107 | + ); |
| 108 | + memory.setValue(key, values); |
| 109 | + } |
| 110 | +); |
| 111 | + |
| 112 | +/** |
| 113 | + * Save current url to memory |
| 114 | + * @param {string} key - key to store value |
| 115 | + * @example I save current url as 'currentUrl' |
| 116 | + */ |
| 117 | +When('I save current url as {string}', async function (key: string) { |
| 118 | + memory.setValue(key, page.url()); |
| 119 | +}); |
| 120 | + |
| 121 | +/** |
| 122 | + * Save current page title to memory |
| 123 | + * @param {string} key - key to store value |
| 124 | + * @example I save page title as 'currentTitle' |
| 125 | + */ |
| 126 | +When('I save page title as {string}', async function (key: string) { |
| 127 | + const title = await page.title(); |
| 128 | + memory.setValue(key, title); |
| 129 | +}); |
| 130 | + |
| 131 | +/** |
| 132 | + * Save page screenshot into memory |
| 133 | + * @param {string} key - key to store value |
| 134 | + * @example I save screenshot as 'screenshot' |
| 135 | + */ |
| 136 | +When('I save screenshot as {string}', async function(key: string) { |
| 137 | + const screenshot = await page.screenshot(); |
| 138 | + memory.setValue(key, screenshot); |
| 139 | +}); |
0 commit comments