Skip to content

Commit 1c10739

Browse files
authoredMar 13, 2024
Merge pull request #30 from LambdaTest/stage
Release @lambdatest/cypress-driver v1.0.4
2 parents 08cc82c + beae987 commit 1c10739

File tree

7 files changed

+1143
-26
lines changed

7 files changed

+1143
-26
lines changed
 

‎packages/cypress/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { smartuiSnapshot } = require('./src/smartui');
2+
3+
Cypress.Commands.add('smartuiSnapshot', smartuiSnapshot);

‎packages/cypress/package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@lambdatest/cypress-driver",
3+
"version": "1.0.4",
4+
"description": "Cypress driver for all Lambdatest functionalities",
5+
"main": "index.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/LambdaTest/lambdatest-js-sdk.git",
9+
"directory": "packages/cypress"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/LambdaTest/lambdatest-js-sdk/issues"
13+
},
14+
"homepage": "https://github.com/LambdaTest/lambdatest-js-sdk/packages/cypress#readme",
15+
"scripts": {
16+
"test": "echo \"Error: no test specified\" && exit 1"
17+
},
18+
"keywords": [
19+
"lambdatest",
20+
"cypress"
21+
],
22+
"author": "LambdaTest <keys@lambdatest.com>",
23+
"license": "MIT",
24+
"devDependencies": {
25+
"cypress": "4.x"
26+
},
27+
"peerDependencies": {
28+
"cypress": "4.x"
29+
}
30+
}

‎packages/cypress/src/smartui.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { cylog } = require('./logger');
2+
const utils = require('./utils');
3+
4+
module.exports = new class httpClient {
5+
async request(options) {
6+
return Cypress.backend('http:request', {
7+
retryOnNetworkFailure: false, ...options
8+
});
9+
}
10+
11+
isSmartUIRunning() {
12+
return this.request({
13+
url: `${utils.getSmartUIServerAddress()}/healthcheck`,
14+
method: 'GET',
15+
})
16+
}
17+
18+
fetchDOMSerializer() {
19+
return this.request({
20+
url: `${utils.getSmartUIServerAddress()}/domserializer`,
21+
method: 'GET'
22+
})
23+
}
24+
25+
postSnapshot(snapshot, testType) {
26+
return this.request({
27+
url: `${utils.getSmartUIServerAddress()}/snapshot`,
28+
method: 'POST',
29+
body: JSON.stringify({
30+
snapshot,
31+
testType
32+
}),
33+
headers: {
34+
'Content-Type': 'application/json',
35+
}
36+
})
37+
}
38+
};

‎packages/cypress/src/utils/logger.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const chalk = require('chalk');
2+
const pkgName = require('../../package.json').name;
3+
4+
function cylog(name, message, meta = {}) {
5+
return Cypress.log({
6+
name,
7+
message,
8+
displayName: pkgName,
9+
consoleProps: () => meta
10+
});
11+
}
12+
13+
function log(level, message) {
14+
if (typeof message === 'object') {
15+
message = JSON.stringify(message);
16+
}
17+
switch (level) {
18+
case 'debug':
19+
message = chalk.blue(message);
20+
break;
21+
case 'warn':
22+
message = chalk.yellow(`Warning: ${message}`);
23+
break;
24+
case 'error':
25+
message = chalk.red(`Error: ${message}`);
26+
break;
27+
}
28+
return `[${pkgName}] ${message}`;
29+
}
30+
31+
module.exports = {
32+
cylog,
33+
log
34+
}

‎packages/cypress/src/utils/utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function getSmartUIServerAddress() {
2+
if (!Cypress.env('SMARTUI_SERVER_ADDRESS')) throw new Error('SmartUI server address not found');
3+
return Cypress.env('SMARTUI_SERVER_ADDRESS');
4+
}
5+
6+
module.exports = {
7+
getSmartUIServerAddress
8+
};

0 commit comments

Comments
 (0)
Failed to load comments.