|
1 | 1 | var q = require('q'); |
2 | 2 | var fs = require('fs'); |
| 3 | +var fse = require('fs-extra'); |
3 | 4 | var mkdirp = require('mkdirp'); |
| 5 | +var jasmine2Reporter = require('./reporter/jasmine2_reporter.js'); |
4 | 6 |
|
5 | 7 | /** |
6 | 8 | * This plugin does few things: |
7 | 9 | * 1. Takes a screenshot for each jasmine expect/matcher failure |
8 | 10 | * 2. Takes a screenshot for each test/spec failure |
9 | | - * 3. Marks the test as failure if browser console log has error - Chrome only //TODO |
| 11 | + * 3. Genrates a HTML report |
| 12 | + * 4. Marks the test as failure if browser console log has error - Chrome only //TODO |
10 | 13 | * |
11 | 14 | * exports.config = { |
12 | 15 | * plugins: [{ |
13 | | - * path: 'node_modules/jasmine2-protractor-utils/index.js', |
14 | | - * screenshotOnExpectFailure: {Boolean} (Default - false), |
15 | | - * screenshotOnSpecFailure: {Boolean} (Default - false), |
16 | | - * screenshotPath: {String} (Default - 'reports/screenshots') |
17 | | - * }] |
| 16 | + * package: 'jasmine2-protractor-utils', |
| 17 | + * screenshotOnExpectFailure: {Boolean} (Default - false), |
| 18 | + * screenshotOnSpecFailure: {Boolean} (Default - false), |
| 19 | + * screenshotPath: {String} (Default - 'reports/screenshots') |
| 20 | + * clearFoldersBeforeTest: {Boolean} (Default - false), |
| 21 | + * htmlReportDir: {String} (Default - './reports/htmlReports') |
| 22 | + * failTestOnErrorLog: { |
| 23 | + * failTestOnErrorLogLevel: {Number}, (Default - 900) |
| 24 | + * excludeKeywords: {A JSON Array} |
| 25 | + * } |
| 26 | + * }] |
18 | 27 | * }; |
19 | 28 | * @author Abhishek Swain |
20 | 29 | * @blog www.qaautomationsimplified.com |
@@ -88,6 +97,24 @@ protractorUtil.takeScreenshotOnSpecFail = function (context) { |
88 | 97 | } |
89 | 98 | }; |
90 | 99 |
|
| 100 | +/** |
| 101 | + * Generates HTML report for tests |
| 102 | + * |
| 103 | + * @param {Object} context The plugin context object |
| 104 | + * @return {!webdriver.promise.Promise.<R>} A promise |
| 105 | + */ |
| 106 | +protractorUtil.generateHTMLReport = function (context) { |
| 107 | + |
| 108 | + return global.browser.getProcessedConfig().then(function (config) { |
| 109 | + |
| 110 | + if (context.config.htmlReportDir) { |
| 111 | + return global.browser.getProcessedConfig().then(function (config) { |
| 112 | + jasmine.getEnv().addReporter(new jasmine2Reporter(context.config.htmlReportDir)); |
| 113 | + }); |
| 114 | + } |
| 115 | + }); |
| 116 | +}; |
| 117 | + |
91 | 118 | /** |
92 | 119 | * Fails the test/spec if browser has console logs |
93 | 120 | * |
@@ -179,17 +206,53 @@ protractorUtil.prototype.setup = function () { |
179 | 206 | } |
180 | 207 | } |
181 | 208 | else { |
182 | | - |
| 209 | + if (this.config.clearFoldersBeforeTest) { |
| 210 | + try { |
| 211 | + fse.removeSync(this.config.screenshotPath); |
| 212 | + } catch (err) { |
| 213 | + console.error(err); |
| 214 | + } |
| 215 | + } |
183 | 216 |
|
184 | 217 | mkdirp.sync(this.config.screenshotPath, function (err) { |
185 | 218 | if (err) console.error(err); |
186 | 219 | else console.log(self.config.screenshotPath + ' folder created!'); |
187 | 220 | }); |
188 | 221 | } |
189 | 222 |
|
| 223 | + |
| 224 | + if (!this.config.htmlReportDir) { |
| 225 | + //creates reports folder if does not exist |
| 226 | + var reportsDir = './reports'; |
| 227 | + if (!fs.existsSync(reportsDir)) { |
| 228 | + fs.mkdirSync(reportsDir); |
| 229 | + } |
| 230 | + |
| 231 | + //creates htmlReports folder if does not exist |
| 232 | + var htmlReportsDir = './reports/htmlReports'; |
| 233 | + if (!fs.existsSync(htmlReportsDir)) { |
| 234 | + fs.mkdirSync(htmlReportsDir); |
| 235 | + } |
| 236 | + } |
| 237 | + else { |
| 238 | + if (this.config.clearFoldersBeforeTest) { |
| 239 | + try { |
| 240 | + fse.removeSync(this.config.htmlReportDir); |
| 241 | + } catch (err) { |
| 242 | + console.error(err); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + mkdirp.sync(this.config.htmlReportDir, function (err) { |
| 247 | + if (err) console.error(err); |
| 248 | + else console.log(self.config.htmlReportDir + ' folder created!'); |
| 249 | + }); |
| 250 | + } |
| 251 | + |
190 | 252 | protractorUtil.takeScreenshotOnExpectFail(this); |
191 | 253 | protractorUtil.takeScreenshotOnSpecFail(this); |
192 | 254 | protractorUtil.failTestOnErrorLog(this); |
| 255 | + protractorUtil.generateHTMLReport(this); |
193 | 256 |
|
194 | 257 | }; |
195 | 258 |
|
|
0 commit comments