Skip to content

Instantly share code, notes, and snippets.

@pmicko
Created October 3, 2022 10:38
Show Gist options
  • Save pmicko/0a7968344f23a94231bb0ab4765a4ff0 to your computer and use it in GitHub Desktop.
Save pmicko/0a7968344f23a94231bb0ab4765a4ff0 to your computer and use it in GitHub Desktop.
Cypress - Send test results to elastic server
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)
})
afterEach(function() {
cy.sendDataToElastic(this.currentTest)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment