Skip to content

Commit

Permalink
Merge pull request #30 from LambdaTest/stage
Browse files Browse the repository at this point in the history
Release @lambdatest/cypress-driver v1.0.4
  • Loading branch information
arushsaxena1998 committed Mar 13, 2024
2 parents 08cc82c + beae987 commit 1c10739
Show file tree
Hide file tree
Showing 7 changed files with 1,143 additions and 26 deletions.
3 changes: 3 additions & 0 deletions packages/cypress/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { smartuiSnapshot } = require('./src/smartui');

Cypress.Commands.add('smartuiSnapshot', smartuiSnapshot);
30 changes: 30 additions & 0 deletions packages/cypress/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@lambdatest/cypress-driver",
"version": "1.0.4",
"description": "Cypress driver for all Lambdatest functionalities",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/LambdaTest/lambdatest-js-sdk.git",
"directory": "packages/cypress"
},
"bugs": {
"url": "https://github.com/LambdaTest/lambdatest-js-sdk/issues"
},
"homepage": "https://github.com/LambdaTest/lambdatest-js-sdk/packages/cypress#readme",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"lambdatest",
"cypress"
],
"author": "LambdaTest <keys@lambdatest.com>",
"license": "MIT",
"devDependencies": {
"cypress": "4.x"
},
"peerDependencies": {
"cypress": "4.x"
}
}
53 changes: 53 additions & 0 deletions packages/cypress/src/smartui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { cylog, log } = require('./utils/logger');
const client = require('./utils/httpClient');
const testType = 'cypress-driver';
const CY_TIMEOUT = 30 * 1000 * 1.5;

function smartuiSnapshot(name, options = {}) {
// Default name to test title
name = name || cy.state('runnable').fullTitle();

return cy.then({ timeout: CY_TIMEOUT }, async () => {
if (Cypress.config('isInteractive') && !Cypress.config('enableSmartUIInteractiveMode')) {
return cylog('smartuiSnapshot', 'Disabled in interactive mode', {
details: 'use "cypress run" instead of "cypress open"',
snapshot: name,
});
}

if (!(await client.isSmartUIRunning())) {
throw new Error('Cannot find SmartUI server.');
}

let resp = await client.fetchDOMSerializer();
eval(resp.body.data.dom);

return cy.document({ log: false }).then({ timeout: CY_TIMEOUT }, dom => {
let domSnapshot = window.SmartUIDOM.serialize({ ...options, dom });

return client.postSnapshot({
dom: domSnapshot,
url: dom.URL,
name,
options
}, testType).then(resp => {
if (resp.status >= 200 && resp.status < 300) {
if (resp.body.data?.warnings?.length) {
resp.body.data.warnings.map(e => cy.task('log', log('warn', e)));
}
cylog('smartuiSnapshot', `Snapshot captured: ${name}`);
cy.task('log', log('info', `Snapshot captured: ${name}`));
} else {
throw new Error(resp.body.error.message);
}
}).catch(error => {
cy.task('log', log('error', `SmartUI snapshot failed "${name}"`));
cy.task('log', log('error', error.message));
});
});
});
}

module.exports = {
smartuiSnapshot
}
38 changes: 38 additions & 0 deletions packages/cypress/src/utils/httpClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const { cylog } = require('./logger');
const utils = require('./utils');

module.exports = new class httpClient {
async request(options) {
return Cypress.backend('http:request', {
retryOnNetworkFailure: false, ...options
});
}

isSmartUIRunning() {
return this.request({
url: `${utils.getSmartUIServerAddress()}/healthcheck`,
method: 'GET',
})
}

fetchDOMSerializer() {
return this.request({
url: `${utils.getSmartUIServerAddress()}/domserializer`,
method: 'GET'
})
}

postSnapshot(snapshot, testType) {
return this.request({
url: `${utils.getSmartUIServerAddress()}/snapshot`,
method: 'POST',
body: JSON.stringify({
snapshot,
testType
}),
headers: {
'Content-Type': 'application/json',
}
})
}
};
34 changes: 34 additions & 0 deletions packages/cypress/src/utils/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const chalk = require('chalk');
const pkgName = require('../../package.json').name;

function cylog(name, message, meta = {}) {
return Cypress.log({
name,
message,
displayName: pkgName,
consoleProps: () => meta
});
}

function log(level, message) {
if (typeof message === 'object') {
message = JSON.stringify(message);
}
switch (level) {
case 'debug':
message = chalk.blue(message);
break;
case 'warn':
message = chalk.yellow(`Warning: ${message}`);
break;
case 'error':
message = chalk.red(`Error: ${message}`);
break;
}
return `[${pkgName}] ${message}`;
}

module.exports = {
cylog,
log
}
8 changes: 8 additions & 0 deletions packages/cypress/src/utils/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function getSmartUIServerAddress() {
if (!Cypress.env('SMARTUI_SERVER_ADDRESS')) throw new Error('SmartUI server address not found');
return Cypress.env('SMARTUI_SERVER_ADDRESS');
}

module.exports = {
getSmartUIServerAddress
};
Loading

0 comments on commit 1c10739

Please sign in to comment.