From c720da6435b7be07fb8366381c4f62d3067d6078 Mon Sep 17 00:00:00 2001 From: lamkovod Date: Wed, 10 Jul 2019 09:23:21 +0200 Subject: [PATCH 1/2] Attachment support --- README.md | 4 +- .../cypress/cypress/qTestReporter.json | 4 +- index.js | 39 ++++++++++++++++--- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 65f362c..f5a60f7 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ 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. + "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: @@ -87,5 +89,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! diff --git a/boilerplates/cypress/cypress/qTestReporter.json b/boilerplates/cypress/cypress/qTestReporter.json index 5379384..f4ec004 100644 --- a/boilerplates/cypress/cypress/qTestReporter.json +++ b/boilerplates/cypress/cypress/qTestReporter.json @@ -7,5 +7,7 @@ "statePending": "PENDING", "enableLogs": true, "hideWarning": false, - "hideResultUrl": false + "hideResultUrl": false, + "attachmentFolder": "cypress/screenshots", + "attachmentType": "image/png" } diff --git a/index.js b/index.js index 8835e32..a906296 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,9 @@ 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' if (!testSuiteId && (!parentId || !testSuiteName || !parentType)) { if (!qTestConfig.hideWarning) { @@ -106,11 +109,19 @@ 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' - // }] + + const attachmentsFolder = path.join(process.cwd(), `${attachmentFolder}`) + + if (fs.existsSync(attachmentsFolder)) { + const attachmentPath = getAttachmentPath(path.join(process.cwd(), `${attachmentFolder}`), getQTestId(test.title)) + if (attachmentPath) { + test.qTest.executionLog.file = fs.readFileSync(attachmentPath).toString('base64') + } else { + log(log.TEST_PAD, "Attachments with qTestId in name not found") + } + } else { + log(log.TEST_PAD, "Attachments path not found") + } }, onTestSkip(test) { @@ -205,7 +216,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) @@ -339,6 +352,20 @@ function addTest(testTitle, testCaseId, buildUrl) { } } +function getAttachmentPath(attachmentPath, testCaseId) { + const files = getFiles(attachmentPath) + return files.find(filename => filename.includes(testCaseId)) +} + +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 Array.prototype.concat(...files) +} + /** * Expose `Mocha qTest Mapping Reporter`. */ From 6578a20c83b00fad3aeea139f45be6f58ded5038 Mon Sep 17 00:00:00 2001 From: lamkovod Date: Thu, 11 Jul 2019 10:02:06 +0200 Subject: [PATCH 2/2] Attachments optional trigger --- README.md | 10 ++++++++++ index.js | 37 ++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f5a60f7..87fd6c9 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ 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 } @@ -61,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** @@ -70,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 diff --git a/index.js b/index.js index a906296..5f5ef2d 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,7 @@ function qTest(runner, options = {}) { 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) { @@ -109,19 +110,6 @@ function qTest(runner, options = {}) { test.qTest.executionLog.status = stateFailed test.qTest.executionLog.note = err.stack - - const attachmentsFolder = path.join(process.cwd(), `${attachmentFolder}`) - - if (fs.existsSync(attachmentsFolder)) { - const attachmentPath = getAttachmentPath(path.join(process.cwd(), `${attachmentFolder}`), getQTestId(test.title)) - if (attachmentPath) { - test.qTest.executionLog.file = fs.readFileSync(attachmentPath).toString('base64') - } else { - log(log.TEST_PAD, "Attachments with qTestId in name not found") - } - } else { - log(log.TEST_PAD, "Attachments path not found") - } }, onTestSkip(test) { @@ -146,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) { @@ -352,9 +344,20 @@ function addTest(testTitle, testCaseId, buildUrl) { } } -function getAttachmentPath(attachmentPath, testCaseId) { - const files = getFiles(attachmentPath) - return files.find(filename => filename.includes(testCaseId)) +function getAttachment(attachmentFolder, testTitle) { + const attachmentsFolder = path.join(process.cwd(), `${attachmentFolder}`) + + if (fs.existsSync(attachmentsFolder)) { + const attachmentPath = getFiles(attachmentsFolder).find(filename => filename.includes(testTitle)) + 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) { @@ -363,7 +366,7 @@ function getFiles (folderPath) { const res = path.resolve(folderPath, dirent) return fs.existsSync(res) && fs.lstatSync(res).isDirectory() ? getFiles(res) : res }) - return Array.prototype.concat(...files) + return [].concat(...files) } /**