|
| 1 | +/** |
| 2 | + * @fileoverview Assert on the page state before taking a screenshot, so the screenshot is consistent |
| 3 | + * @author Luke Page |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict' |
| 7 | + |
| 8 | +const assertionCommands = [ |
| 9 | + // assertions |
| 10 | + 'should', |
| 11 | + 'and', |
| 12 | + 'contains', |
| 13 | + |
| 14 | + // retries until it gets something |
| 15 | + 'get', |
| 16 | + |
| 17 | + // not an assertion, but unlikely to require waiting for render |
| 18 | + 'scrollIntoView', |
| 19 | + 'scrollTo', |
| 20 | +]; |
| 21 | + |
| 22 | +module.exports = { |
| 23 | + meta: { |
| 24 | + docs: { |
| 25 | + description: 'Assert on the page state before taking a screenshot, so the screenshot is consistent', |
| 26 | + category: 'Possible Errors', |
| 27 | + recommended: false, |
| 28 | + }, |
| 29 | + schema: [], |
| 30 | + messages: { |
| 31 | + unexpected: 'Make an assertion on the page state before taking a screenshot', |
| 32 | + }, |
| 33 | + }, |
| 34 | + create (context) { |
| 35 | + return { |
| 36 | + CallExpression (node) { |
| 37 | + if (isCallingCyScreenshot(node) && !isPreviousAnAssertion(node)) { |
| 38 | + context.report({ node, messageId: 'unexpected' }) |
| 39 | + } |
| 40 | + }, |
| 41 | + } |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +function isRootCypress(node) { |
| 46 | + while(node.type === 'CallExpression') { |
| 47 | + if (node.callee.type !== 'MemberExpression') return false |
| 48 | + if (node.callee.object.type === 'Identifier' && |
| 49 | + node.callee.object.name === 'cy') { |
| 50 | + return true |
| 51 | + } |
| 52 | + node = node.callee.object |
| 53 | + } |
| 54 | + return false |
| 55 | +} |
| 56 | + |
| 57 | +function getPreviousInChain(node) { |
| 58 | + return node.type === 'CallExpression' && |
| 59 | + node.callee.type === 'MemberExpression' && |
| 60 | + node.callee.object.type === 'CallExpression' && |
| 61 | + node.callee.object.callee.type === 'MemberExpression' && |
| 62 | + node.callee.object.callee.property.type === 'Identifier' && |
| 63 | + node.callee.object.callee.property.name |
| 64 | +} |
| 65 | + |
| 66 | +function getCallExpressionCypressCommand(node) { |
| 67 | + return isRootCypress(node) && |
| 68 | + node.callee.property.type === 'Identifier' && |
| 69 | + node.callee.property.name |
| 70 | +} |
| 71 | + |
| 72 | +function isCallingCyScreenshot (node) { |
| 73 | + return getCallExpressionCypressCommand(node) === 'screenshot' |
| 74 | +} |
| 75 | + |
| 76 | +function getPreviousCypressCommand(node) { |
| 77 | + const previousInChain = getPreviousInChain(node) |
| 78 | + |
| 79 | + if (previousInChain) { |
| 80 | + return previousInChain |
| 81 | + } |
| 82 | + |
| 83 | + while(node.parent && !node.parent.body) { |
| 84 | + node = node.parent |
| 85 | + } |
| 86 | + |
| 87 | + if (!node.parent || !node.parent.body) return null |
| 88 | + |
| 89 | + const body = node.parent.body.type === 'BlockStatement' ? node.parent.body.body : node.parent.body |
| 90 | + |
| 91 | + const index = body.indexOf(node) |
| 92 | + |
| 93 | + // in the case of a function declaration it won't be found |
| 94 | + if (index < 0) return null |
| 95 | + |
| 96 | + if (index === 0) return getPreviousCypressCommand(node.parent); |
| 97 | + |
| 98 | + const previousStatement = body[index - 1] |
| 99 | + |
| 100 | + if (previousStatement.type !== 'ExpressionStatement' || |
| 101 | + previousStatement.expression.type !== 'CallExpression') |
| 102 | + return null |
| 103 | + |
| 104 | + return getCallExpressionCypressCommand(previousStatement.expression) |
| 105 | +} |
| 106 | + |
| 107 | +function isPreviousAnAssertion (node) { |
| 108 | + const previousCypressCommand = getPreviousCypressCommand(node) |
| 109 | + return assertionCommands.indexOf(previousCypressCommand) >= 0 |
| 110 | +} |
0 commit comments