-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from LambdaTest/stage
Release @lambdatest/cypress-driver v1.0.4
- Loading branch information
Showing
7 changed files
with
1,143 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const { smartuiSnapshot } = require('./src/smartui'); | ||
|
||
Cypress.Commands.add('smartuiSnapshot', smartuiSnapshot); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} | ||
}) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
Oops, something went wrong.