Skip to content

Commit

Permalink
Add the testRunId property to the Screenshot interface (closes #…
Browse files Browse the repository at this point in the history
…4923) (#4925)

* add the `testRunId` property to the `Screenshot ` interface (closes #4923)

* fix review remarks
  • Loading branch information
AlexKamaev committed Mar 31, 2020
1 parent 462193e commit f7f915b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/runner/test-run-controller.js
Expand Up @@ -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);

Expand Down
12 changes: 9 additions & 3 deletions src/screenshots/capturer.js
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/screenshots/index.js
Expand Up @@ -20,6 +20,7 @@ export default class Screenshots {
_addTestEntry (test) {
const testEntry = {
test: test,
testRuns: {},
screenshots: []
};

Expand Down Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions test/functional/fixtures/api/es-next/take-screenshot/test.js
Expand Up @@ -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;
}
Expand All @@ -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: () => {
Expand Down Expand Up @@ -218,6 +222,7 @@ describe('[API] t.takeScreenshot()', function () {
}

return {
testRunId: true,
screenshotPath,
thumbnailPath,
takenOnFail,
Expand Down
71 changes: 66 additions & 5 deletions 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');
Expand All @@ -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: () => {
}
Expand All @@ -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
});
});
});
6 changes: 6 additions & 0 deletions test/server/reporter-test.js
Expand Up @@ -46,6 +46,7 @@ describe('Reporter', () => {
fixture: fixtureMocks[0],
skip: false,
screenshots: [{
testRunId: 'idf1t1-1',
screenshotPath: 'screenshot1.png',
thumbnailPath: 'thumbnail1.png',
userAgent: 'chrome',
Expand All @@ -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',
Expand Down Expand Up @@ -561,6 +564,7 @@ describe('Reporter', () => {
},
screenshotPath: '/screenshots/1445437598847',
screenshots: [{
testRunId: 'idf1t1-1',
screenshotPath: 'screenshot1.png',
thumbnailPath: 'thumbnail1.png',
userAgent: 'chrome',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit f7f915b

Please sign in to comment.