Skip to content

Updated to upload video file to suite #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/cypressReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const { reporterEvents, testItemStatuses } = require('./constants');
const { IPC_EVENTS } = require('./ipcEvents');
const {
getConfig,
getStringDifference,
getLaunchStartObject,
getSuiteStartObject,
getSuiteEndObject,
Expand Down Expand Up @@ -64,6 +65,18 @@ class CypressReporter extends Mocha.reporters.Base {
const configListener = (cypressFullConfig) => {
CypressReporter.cypressConfig = cypressFullConfig;
CypressReporter.calcTotalLaunches();

/* Set Test and Video Relative Paths to each other */
if (!cypressFullConfig.integrationFolder) cypressFullConfig.integrationFolder = 'cypress/integration';
if (!cypressFullConfig.videosFolder) cypressFullConfig.videosFolder = 'cypress/videos';
CypressReporter.cypressConfig.testRelPath = getStringDifference(cypressFullConfig.videosFolder, cypressFullConfig.integrationFolder);
if (!CypressReporter.cypressConfig.testRelPath.endsWith('\\') || !CypressReporter.cypressConfig.testRelPath.endsWith('/')) {
CypressReporter.cypressConfig.testRelPath += '/';
}
CypressReporter.cypressConfig.videoRelPath = getStringDifference(cypressFullConfig.integrationFolder, cypressFullConfig.videosFolder);
if (!CypressReporter.cypressConfig.videoRelPath.endsWith('\\') || !CypressReporter.cypressConfig.videoRelPath.endsWith('/')) {
CypressReporter.cypressConfig.videoRelPath += '/';
}
};
const logListener = (log) => this.worker.send({ event: reporterEvents.LOG, log });
const launchLogListener = (log) =>
Expand Down Expand Up @@ -129,7 +142,7 @@ class CypressReporter extends Mocha.reporters.Base {

this.runner.on(EVENT_SUITE_END, (suite) => {
if (!suite.title) return;
this.worker.send({ event: EVENT_SUITE_END, suite: getSuiteEndObject(suite) });
this.worker.send({ event: EVENT_SUITE_END, suite: getSuiteEndObject(suite, CypressReporter.cypressConfig) });
});

this.runner.on(EVENT_TEST_BEGIN, (test) => {
Expand Down
16 changes: 16 additions & 0 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
getFailedScreenshot,
getPassedScreenshots,
getCustomScreenshots,
getVideoFile,
getTestStartObject,
getTestEndObject,
getHookStartObject,
Expand Down Expand Up @@ -112,6 +113,21 @@ class Reporter {
const suiteId = this.testItemIds.get(suite.id);
const suiteTestCaseId = this.suiteTestCaseIds.get(suite.title);
const suiteStatus = this.suiteStatuses.get(suite.title);

const videoFileDetails = getVideoFile(suite.videoFile);
if (videoFileDetails) {
const sendVideoPromise = this.client.sendLog(
suiteId,
{
message: `Video recording of '${suite.title}' tests'`,
level: logLevels.INFO,
time: new Date().valueOf(),
},
videoFileDetails,
).promise;
promiseErrorHandler(sendVideoPromise, 'Fail to save video');
}

const finishTestItemPromise = this.client.finishTestItem(
suiteId,
Object.assign(
Expand Down
38 changes: 37 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ const base64Encode = (file) => {
return Buffer.from(bitmap).toString('base64');
};

const getVideoFile = (videoFileName) => {
const videoFilePath = `**/${videoFileName}`
const videoFile = glob.sync(videoFilePath);
if (videoFile.length) {
return {
name: videoFilePath.split('/').pop(),
type: 'video/mp4',
content: base64Encode(videoFile[0]),
};
}

return undefined;
}

const getCustomScreenshots = (customScreenshotsFileNames, specFilePath) => {
if (!customScreenshotsFileNames.length) return [];

Expand Down Expand Up @@ -121,13 +135,31 @@ const getConfig = (initialConfig) => {
};
};

const getStringDifference = (stringA, stringB) => {
var stringAIndex = 0;
var stringBIndex = 0;
var result = "";

while (stringBIndex < stringB.length)
{
if (stringAIndex != stringBIndex || stringA[stringAIndex] != stringB[stringBIndex] || stringAIndex == stringA.length) {
result += stringB[stringBIndex];
} else {
stringAIndex++;
}
stringBIndex++;
}
return result;
}

const getLaunchStartObject = (config) => {
const launchAttributes = (config.reporterOptions.attributes || []).concat(
getSystemAttributes(config),
);

return {
launch: config.reporterOptions.launch,
mode: config.reporterOptions.mode,
description: config.reporterOptions.description,
attributes: launchAttributes,
rerun: config.reporterOptions.rerun,
Expand All @@ -147,9 +179,11 @@ const getSuiteStartObject = (suite, testFileName) => ({
parentId: !suite.root ? suite.parent.id : undefined,
});

const getSuiteEndObject = (suite) => ({
const getSuiteEndObject = (suite, cypressConfig) => ({
id: suite.id,
title: suite.title,
testFile: suite.parent.file,
videoFile: `${suite.parent.file.replace(cypressConfig.testRelPath, cypressConfig.videoRelPath)}.mp4`,
endTime: new Date().valueOf(),
});

Expand Down Expand Up @@ -263,9 +297,11 @@ const getTotalSpecs = ({
};

module.exports = {
getStringDifference,
getFailedScreenshot,
getPassedScreenshots,
getCustomScreenshots,
getVideoFile,
getAgentInfo,
getCodeRef,
getSystemAttributes,
Expand Down