|
| 1 | +const { cylog, log } = require('./utils/logger'); |
| 2 | +const client = require('./utils/httpClient'); |
| 3 | +const testType = 'cypress-driver'; |
| 4 | +const CY_TIMEOUT = 30 * 1000 * 1.5; |
| 5 | + |
| 6 | +function smartuiSnapshot(name, options = {}) { |
| 7 | +// Default name to test title |
| 8 | +name = name || cy.state('runnable').fullTitle(); |
| 9 | + |
| 10 | +return cy.then({ timeout: CY_TIMEOUT }, async () => { |
| 11 | +if (Cypress.config('isInteractive') && !Cypress.config('enableSmartUIInteractiveMode')) { |
| 12 | +return cylog('smartuiSnapshot', 'Disabled in interactive mode', { |
| 13 | +details: 'use "cypress run" instead of "cypress open"', |
| 14 | +snapshot: name, |
| 15 | +}); |
| 16 | +} |
| 17 | + |
| 18 | +if (!(await client.isSmartUIRunning())) { |
| 19 | +throw new Error('Cannot find SmartUI server.'); |
| 20 | +} |
| 21 | + |
| 22 | +let resp = await client.fetchDOMSerializer(); |
| 23 | +eval(resp.body.data.dom); |
| 24 | + |
| 25 | +return cy.document({ log: false }).then({ timeout: CY_TIMEOUT }, dom => { |
| 26 | +let domSnapshot = window.SmartUIDOM.serialize({ ...options, dom }); |
| 27 | + |
| 28 | +return client.postSnapshot({ |
| 29 | +dom: domSnapshot, |
| 30 | +url: dom.URL, |
| 31 | +name, |
| 32 | +options |
| 33 | +}, testType).then(resp => { |
| 34 | +if (resp.status >= 200 && resp.status < 300) { |
| 35 | +if (resp.body.data?.warnings?.length) { |
| 36 | +resp.body.data.warnings.map(e => cy.task('log', log('warn', e))); |
| 37 | +} |
| 38 | +cylog('smartuiSnapshot', `Snapshot captured: ${name}`); |
| 39 | +cy.task('log', log('info', `Snapshot captured: ${name}`)); |
| 40 | +} else { |
| 41 | +throw new Error(resp.body.error.message); |
| 42 | +} |
| 43 | +}).catch(error => { |
| 44 | +cy.task('log', log('error', `SmartUI snapshot failed "${name}"`)); |
| 45 | +cy.task('log', log('error', error.message)); |
| 46 | +}); |
| 47 | +}); |
| 48 | +}); |
| 49 | +} |
| 50 | + |
| 51 | +module.exports = { |
| 52 | +smartuiSnapshot |
| 53 | +} |
0 commit comments