Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Attachment support #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ example config file
"enableLogs": false, // disables console logging. Default value: true.*
"hideWarning": true, // hides "results won't be published" message. Default value: false.
"hideResultUrl": true // skip printing out of suite url in the end. Default value: false.
"attachmentTrigger": "ALL" // send attachments depending on test state. Default value === value of stateFailed. Special option - "ALL"
"attachmentFolder": "cypress/screenshots" // path to attachments. Default value: report/screenshot
"attachmentType": "image/png" // media type of attachments. More info https://www.iana.org/assignments/media-types/media-types.xhtml
}
```
you can pass options one by one or pass path to json like this:
Expand All @@ -59,6 +62,7 @@ You can either use existing Test Suite or create new one
`QTEST_SUITE_ID` - testSuiteId. **Required**

### Creating Test Suite

`QTEST_PARENT_TYPE` - one of *root / release / test-cycle / test-suite*. **Required**
`QTEST_PARENT_ID` - parent id. Set to 0 if parent is root. **Required**
`QTEST_SUITE_NAME` - Test Suite name. **Required**
Expand All @@ -68,6 +72,14 @@ You can either use existing Test Suite or create new one
`QTEST_BUILD_URL` - url to your build system or any other url. *Optional*
`QTEST_CREATE_TEST_RUNS` - specify if test runs have to be created in qTest or just update existing ones. *Optional*

### Attachments

Reporter can attach files to a test runs.
Default trigger to send attachment - failed test.
By default it send files with full test.title included in filename from folder *report/screenshot* (+ search in subfolders) with media type [image/png].

Attachment folder / media type / trigger can be configured in config file.

## FAQ

**Q**: What is testCaseId, testRunId, testSuiteId
Expand All @@ -87,5 +99,5 @@ Cypress https://github.com/mgrybyk/mocha-qtest-mapping-reporter/tree/master/boil


## TODOs / known issues
1. TODO: attachments support;
1. Mark test steps as executed;
2. Any questions/suggestions are welcomed!
4 changes: 3 additions & 1 deletion boilerplates/cypress/cypress/qTestReporter.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"statePending": "PENDING",
"enableLogs": true,
"hideWarning": false,
"hideResultUrl": false
"hideResultUrl": false,
"attachmentFolder": "cypress/screenshots",
"attachmentType": "image/png"
}
42 changes: 36 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ function qTest(runner, options = {}) {
const stateFailed = qTestConfig.stateFailed || 'FAIL'
const statePassed = qTestConfig.statePassed || 'PASS'
const statePending = qTestConfig.statePending || 'PENDING'

const attachmentFolder = qTestConfig.attachmentFolder || 'report/screenshot'
const attachmentType = qTestConfig.attachmentType || 'image/png'
const attachmentTrigger = qTestConfig.attachmentTrigger || stateFailed

if (!testSuiteId && (!parentId || !testSuiteName || !parentType)) {
if (!qTestConfig.hideWarning) {
Expand Down Expand Up @@ -106,11 +110,6 @@ function qTest(runner, options = {}) {

test.qTest.executionLog.status = stateFailed
test.qTest.executionLog.note = err.stack
// test.qTest.executionLog.attachments: [{
// name: 'screenshot.png',
// content_type: 'image/png',
// data: 'base64 string of Sample.docx'
// }]
},

onTestSkip(test) {
Expand All @@ -135,6 +134,10 @@ function qTest(runner, options = {}) {
if (!test.qTest.executionLog.status) {
test.qTest.executionLog.status = statePending
}

if ([test.qTest.executionLog.status, 'ALL'].some(tr => attachmentTrigger === tr)) {
test.qTest.executionLog.file = getAttachment(attachmentFolder, test.title)
}
},

onSuiteStart(suite) {
Expand Down Expand Up @@ -205,7 +208,9 @@ function qTest(runner, options = {}) {
{
name: testRun.name,
automation_content: idsLog,
note: `${testTitle} \n${idsLog}` + (executionLog.note ? `\n\r\n ${executionLog.note}` : '')
note: `${testTitle} \n${idsLog}` + (executionLog.note ? `\n\r\n ${executionLog.note}` : ''),
attachments: executionLog.file ? [
{ name: `${testTitle}`, content_type: attachmentType, data: executionLog.file, author: {} } ] : undefined
})

await qTestClient.postLog(testRun.id, logBody)
Expand Down Expand Up @@ -339,6 +344,31 @@ function addTest(testTitle, testCaseId, buildUrl) {
}
}

function getAttachment(attachmentFolder, testTitle) {
const attachmentsFolder = path.join(process.cwd(), `${attachmentFolder}`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think attachmentFolder can be declared once in the top.
Can't see any reason to pass attachmentFolder in current implementation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, it's a property of qTestConfig


if (fs.existsSync(attachmentsFolder)) {
const attachmentPath = getFiles(attachmentsFolder).find(filename => filename.includes(testTitle))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please mention somewhere (in doc/readme) how are you attaching screenshots, please?
It is not obvious at all

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (attachmentPath) {
return fs.readFileSync(attachmentPath).toString('base64')
} else {
console.log("Attachments with qTestId in name not found")
}
} else {
console.log("Attachments path not found")
}
return undefined
}

function getFiles (folderPath) {
const dirents = fs.readdirSync(folderPath)
const files = dirents.map((dirent) => {
const res = path.resolve(folderPath, dirent)
return fs.existsSync(res) && fs.lstatSync(res).isDirectory() ? getFiles(res) : res
})
return [].concat(...files)
}

/**
* Expose `Mocha qTest Mapping Reporter`.
*/
Expand Down