|
1 | 1 | const utils = require('@lambdatest/sdk-utils');
|
2 | 2 | const pkgName = require('../package.json').name;
|
| 3 | +const testType = 'js-playwright-driver'; |
3 | 4 |
|
4 |
| -// Take a DOM snapshot and post it to the snapshot endpoint |
5 |
| -async function smartuiSnapshot(page, name, options) { |
6 |
| -if (!page) throw new Error('A Playwright `page` object is required.'); |
7 |
| -if (!name || typeof name !== 'string') throw new Error('The `name` argument is required.'); |
8 |
| -if (!(await utils.isSmartUIRunning())) throw new Error('Cannot find SmartUI server.'); |
| 5 | +async function smartuiSnapshot(page, name, options = {}) { |
| 6 | +if (!page) throw new Error('A Playwright `page` object is required.'); |
| 7 | +if (!name || typeof name !== 'string') throw new Error('The `name` argument is required.'); |
| 8 | +if (!(await utils.isSmartUIRunning())) throw new Error('Cannot find SmartUI server.'); |
9 | 9 |
|
10 |
| -let log = utils.logger(pkgName); |
| 10 | +let log = utils.logger(pkgName); |
| 11 | +try { |
| 12 | +const resp = await utils.fetchDOMSerializer(); |
| 13 | +await page.evaluate(resp.body.data.dom); |
11 | 14 |
|
12 |
| -try { |
13 |
| -// Inject the DOM serialization script |
14 |
| -const resp = await utils.fetchDOMSerializer(); |
15 |
| -await page.evaluate(resp.body.data.dom); |
16 |
| - |
17 |
| -// Serialize and capture the DOM |
18 |
| -/* istanbul ignore next: no instrumenting injected code */ |
19 |
| -let { dom } = await page.evaluate((options) => ({ |
20 |
| -/* eslint-disable-next-line no-undef */ |
21 |
| -dom: SmartUIDOM.serialize(options) |
22 |
| -}), {}); |
| 15 | +let { dom } = await page.evaluate((options) => ({ |
| 16 | +dom: SmartUIDOM.serialize(options) |
| 17 | +}), {}); |
23 | 18 |
|
24 |
| -// Post the DOM to the snapshot endpoint with snapshot options and other info |
25 |
| -let { body } = await utils.postSnapshot({ |
26 |
| -dom, |
27 |
| -url: page.url(), |
28 |
| -name, |
29 |
| -options |
30 |
| -}, pkgName); |
| 19 | +let { body } = await utils.postSnapshot({ |
| 20 | +dom, |
| 21 | +url: page.url(), |
| 22 | +name, |
| 23 | +options |
| 24 | +}, testType); |
| 25 | +if (body && body.data && body.data.warnings?.length !== 0) body.data.warnings.map(e => log.warn(e)); |
31 | 26 |
|
32 |
| -log.info(`Snapshot captured: ${name}`); |
33 |
| - |
34 |
| -if (body && body.data && body.data.warnings?.length !== 0) body.data.warnings.map(e => log.warn(e)); |
35 |
| -} catch (err) { |
36 |
| -throw err; |
37 |
| -} |
| 27 | +log.info(`Snapshot captured: ${name}`); |
| 28 | +} catch (error) { |
| 29 | +log.error(`SmartUI snapshot failed "${name}"`); |
| 30 | +log.error(error); |
| 31 | +} |
38 | 32 | }
|
39 | 33 |
|
40 | 34 | module.exports = {
|
41 |
| -smartuiSnapshot |
42 |
| -} |
| 35 | +smartuiSnapshot |
| 36 | +} |
0 commit comments