Created
October 3, 2022 10:38
-
-
Save pmicko/0a7968344f23a94231bb0ab4765a4ff0 to your computer and use it in GitHub Desktop.
Cypress - Send test results to elastic server
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
export const sendDataToElastic = currentTest => { | |
if (Cypress.env('ELASTIC_URL')) { | |
const timestampElasticData = new Date().toISOString() | |
const timestampIndex = format(new Date(),'yyyy.MM.dd') | |
const index = `qa-test-report-${timestampIndex}` | |
const elasticData = { | |
'@timestamp': timestampElasticData, | |
project: 'project-name-here', | |
environment: Cypress.env('GITHUB_RUN_ID') ? 'github' : 'local', | |
testTitle: currentTest.title, | |
testResult: currentTest.state, | |
testDuration: currentTest.duration, | |
url: Cypress.config('baseUrl'), | |
runId: Cypress.env('GITHUB_RUN_ID'), | |
type: 'E2E', | |
branch: Cypress.env('PR_NAME') || 'master', | |
sutEnvironment: Cypress.env('ENVIRONMENT') | |
}; | |
cy.request({ | |
method: 'POST', | |
url: `${Cypress.env('ELASTIC_URL')}${index}/_doc`, | |
auth: { | |
username: Cypress.env('ELASTIC_USERNAME'), | |
password: Cypress.env('ELASTIC_PASSWORD') | |
}, | |
failOnStatusCode: false, | |
body: elasticData | |
}).then(resp => { | |
if (resp.status > 201) { | |
cy.log(`Received status code ${resp.status} when sending data to Elastic - ${resp.statusText}`) | |
} | |
}) | |
} | |
} | |
Cypress.Commands.add('sendDataToElastic', currentTest => { | |
sendDataToElastic(currentTest) | |
}) |
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
afterEach(function() { | |
cy.sendDataToElastic(this.currentTest) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment