From f7f915b31d809d625f16cd4f963747a52dba994e Mon Sep 17 00:00:00 2001 From: AlexKamaev Date: Tue, 31 Mar 2020 19:11:22 +0300 Subject: [PATCH] Add the `testRunId` property to the `Screenshot ` interface (closes #4923) (#4925) * add the `testRunId` property to the `Screenshot ` interface (closes #4923) * fix review remarks --- src/runner/test-run-controller.js | 2 + src/screenshots/capturer.js | 12 +++- src/screenshots/index.js | 7 ++ .../api/es-next/take-screenshot/test.js | 5 ++ test/server/capturer-test.js | 71 +++++++++++++++++-- test/server/reporter-test.js | 6 ++ 6 files changed, 95 insertions(+), 8 deletions(-) diff --git a/src/runner/test-run-controller.js b/src/runner/test-run-controller.js index bf6b6aaf4a..12f985558b 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.addTestRun(this.test, this.testRun); + if (this.testRun.addQuarantineInfo) this.testRun.addQuarantineInfo(this.quarantine); diff --git a/src/screenshots/capturer.js b/src/screenshots/capturer.js index 66f644f180..d889df5e14 100644 --- a/src/screenshots/capturer.js +++ b/src/screenshots/capturer.js @@ -147,12 +147,18 @@ export default class Capturer { await generateThumbnail(screenshotPath, thumbnailPath); }); + const testRunId = this.testEntry.testRuns[this.browserId].id; + const userAgent = escapeUserAgent(this.pathPattern.data.parsedUserAgent.prettyUserAgent); + const quarantineAttempt = this.pathPattern.data.quarantineAttempt; + const takenOnFail = forError; + const screenshot = { + testRunId, screenshotPath, thumbnailPath, - userAgent: escapeUserAgent(this.pathPattern.data.parsedUserAgent.prettyUserAgent), - quarantineAttempt: this.pathPattern.data.quarantineAttempt, - takenOnFail: forError, + userAgent, + quarantineAttempt, + takenOnFail }; this.testEntry.screenshots.push(screenshot); diff --git a/src/screenshots/index.js b/src/screenshots/index.js index a86829881d..78e2ef93ef 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); } + + addTestRun (test, testRun) { + const testEntry = this._getTestEntry(test); + + testEntry.testRuns[testRun.browserConnection.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..9d454fbebc 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,42 @@ 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 (test, connection) { + this.id = 'test-run-id'; + this.browserConnection = connection; + } + }; + + 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',