diff --git a/src/runner/test-run-controller.js b/src/runner/test-run-controller.js index bf6b6aaf4a..6e994a7690 100644 --- a/src/runner/test-run-controller.js +++ b/src/runner/test-run-controller.js @@ -90,6 +90,8 @@ export default class TestRunController extends AsyncEventEmitter { this.testRun = new TestRunCtor(this.test, connection, screenshotCapturer, this.warningLog, this.opts); + this.screenshots.watchForTestRun(this.test, this.testRun, connection); + if (this.testRun.addQuarantineInfo) this.testRun.addQuarantineInfo(this.quarantine); diff --git a/src/screenshots/capturer.js b/src/screenshots/capturer.js index 66f644f180..17f7378aa7 100644 --- a/src/screenshots/capturer.js +++ b/src/screenshots/capturer.js @@ -147,7 +147,11 @@ export default class Capturer { await generateThumbnail(screenshotPath, thumbnailPath); }); + const testRun = this.testEntry.testRuns[this.browserId]; + const testRunId = testRun && testRun.id; + const screenshot = { + testRunId, screenshotPath, thumbnailPath, userAgent: escapeUserAgent(this.pathPattern.data.parsedUserAgent.prettyUserAgent), diff --git a/src/screenshots/index.js b/src/screenshots/index.js index a86829881d..ee6a798fe9 100644 --- a/src/screenshots/index.js +++ b/src/screenshots/index.js @@ -20,6 +20,7 @@ export default class Screenshots { _addTestEntry (test) { const testEntry = { test: test, + testRuns: {}, screenshots: [] }; @@ -69,4 +70,10 @@ export default class Screenshots { return new Capturer(this.screenshotsPath, testEntry, connection, pathPattern, this.fullPage, warningLog); } + + watchForTestRun (test, testRun, connection) { + const testEntry = this._ensureTestEntry(test); + + testEntry.testRuns[connection.id] = testRun; + } } diff --git a/test/functional/fixtures/api/es-next/take-screenshot/test.js b/test/functional/fixtures/api/es-next/take-screenshot/test.js index 6b8ca507c9..5a87ba95dd 100644 --- a/test/functional/fixtures/api/es-next/take-screenshot/test.js +++ b/test/functional/fixtures/api/es-next/take-screenshot/test.js @@ -24,6 +24,7 @@ const getReporter = function (scope) { screenshot.screenshotPath = patchScreenshotPath(screenshot.screenshotPath); screenshot.thumbnailPath = patchScreenshotPath(screenshot.thumbnailPath); screenshot.isPassedAttempt = quarantine[screenshot.quarantineAttempt].passed; + screenshot.testRunId = scope.testRunIds.includes(screenshot.testRunId); userAgents[screenshot.userAgent] = true; } @@ -37,6 +38,9 @@ const getReporter = function (scope) { scope.userAgents = Object.keys(userAgents); scope.unstable = testRunInfo.unstable; }, + reportTestStart: (name, meta, { testRunIds }) => { + scope.testRunIds = testRunIds; + }, reportFixtureStart: () => { }, reportTaskStart: () => { @@ -218,6 +222,7 @@ describe('[API] t.takeScreenshot()', function () { } return { + testRunId: true, screenshotPath, thumbnailPath, takenOnFail, diff --git a/test/server/capturer-test.js b/test/server/capturer-test.js index 5a3eeceabc..5c6b1ca896 100644 --- a/test/server/capturer-test.js +++ b/test/server/capturer-test.js @@ -1,8 +1,10 @@ -const nanoid = require('nanoid'); -const expect = require('chai').expect; -const { resolve, dirname } = require('path'); -const { statSync } = require('fs'); -const Capturer = require('../../lib/screenshots/capturer'); +const nanoid = require('nanoid'); +const expect = require('chai').expect; +const { resolve, dirname, join } = require('path'); +const { statSync } = require('fs'); +const Capturer = require('../../lib/screenshots/capturer'); +const TestRunController = require('../../lib/runner/test-run-controller'); +const Screenshots = require('../../lib/screenshots'); const filePath = resolve(process.cwd(), `temp${nanoid(7)}`, 'temp.png'); @@ -15,6 +17,27 @@ class CapturerMock extends Capturer { } } +class ScreenshotsMock extends Screenshots { + constructor (options) { + super(options); + } + + createCapturerFor (test, testIndex, quarantine, connection, warningLog) { + this.capturer = super.createCapturerFor(test, testIndex, quarantine, connection, warningLog); + + this.capturer.pathPattern = { + data: { + parsedUserAgent: { + prettyUserAgent: 'user-agent' + }, + quarantineAttempt: 1 + } + }; + + return this.capturer; + } +} + const emptyProvider = { takeScreenshot: () => { } @@ -37,4 +60,41 @@ describe('Capturer', () => { expect(errCode).eql('ENOENT'); }); + + it('Screenshot properties for reporter', async () => { + const screenshots = new ScreenshotsMock({ enabled: true, path: process.cwd(), pathPattern: '', fullPage: false }); + + const testRunControllerMock = { + screenshots, + test: { fixture: {} }, + emit: () => { + }, + TestRunCtor: function () { + this.id = 'test-run-id'; + } + }; + + await TestRunController.prototype._createTestRun.call(testRunControllerMock, { + id: 'browser-connection-id', + provider: emptyProvider, + browserInfo: { + parsedUserAgent: { + os: { + name: 'os-name' + } + } + } + }); + + await screenshots.capturer._capture(false, { customPath: 'screenshot.png' }); + + expect(screenshots.capturer.testEntry.screenshots[0]).eql({ + testRunId: 'test-run-id', + screenshotPath: join(process.cwd(), 'screenshot.png'), + thumbnailPath: join(process.cwd(), 'thumbnails', 'screenshot.png'), + userAgent: 'user-agent', + quarantineAttempt: 1, + takenOnFail: false + }); + }); }); diff --git a/test/server/reporter-test.js b/test/server/reporter-test.js index 6bcf8925ff..24930e7370 100644 --- a/test/server/reporter-test.js +++ b/test/server/reporter-test.js @@ -46,6 +46,7 @@ describe('Reporter', () => { fixture: fixtureMocks[0], skip: false, screenshots: [{ + testRunId: 'idf1t1-1', screenshotPath: 'screenshot1.png', thumbnailPath: 'thumbnail1.png', userAgent: 'chrome', @@ -64,12 +65,14 @@ describe('Reporter', () => { fixture: fixtureMocks[0], skip: false, screenshots: [{ + testRunId: 'idf1t2-1', screenshotPath: 'screenshot1.png', thumbnailPath: 'thumbnail1.png', userAgent: 'chrome', takenOnFail: false, quarantineAttempt: null }, { + testRunId: 'idf1t2-2', screenshotPath: 'screenshot2.png', thumbnailPath: 'thumbnail2.png', userAgent: 'chrome', @@ -561,6 +564,7 @@ describe('Reporter', () => { }, screenshotPath: '/screenshots/1445437598847', screenshots: [{ + testRunId: 'idf1t1-1', screenshotPath: 'screenshot1.png', thumbnailPath: 'thumbnail1.png', userAgent: 'chrome', @@ -619,12 +623,14 @@ describe('Reporter', () => { quarantine: null, screenshotPath: '/screenshots/1445437598847', screenshots: [{ + testRunId: 'idf1t2-1', screenshotPath: 'screenshot1.png', thumbnailPath: 'thumbnail1.png', userAgent: 'chrome', takenOnFail: false, quarantineAttempt: null }, { + testRunId: 'idf1t2-2', screenshotPath: 'screenshot2.png', thumbnailPath: 'thumbnail2.png', userAgent: 'chrome',