Skip to content

Commit

Permalink
Add env variables to control cypress video (#44)
Browse files Browse the repository at this point in the history
* Add env variables to control cypress video

* Update according to comments

* Add unit test for shouldRecordVideo
  • Loading branch information
tianfeng92 authored Oct 27, 2020
1 parent e6dfcdb commit 96b83ec
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ jobs:
- name: Install dependencies
run: npm ci --production
- name: Run integration tests
env:
SAUCE_CYPRESS_VIDEO_RECORDING: false
run: bash tests/integration/integration-tests.sh

9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ $ docker pull saucelabs/stt-cypress-mocha-node:latest
### Integration tests
`npm run test-integration` triggers the [integration tests script](/tests/integration/integration-tests.sh). Which triggers the tests in (/tests/integration/fixtures/cypress-tests) and runs them through `cypress-runner.js`.

### Env Variables
#### `SAUCE_CYPRESS_VIDEO_RECORDING`
This env variable is for controlling cypress native video recording.
`true`/`1` will enable cypress native video recording.
`false`/`0` will stop cypress native video recording.
```sh
saucectl run -e SAUCE_CYPRESS_VIDEO_RECORDING=true
```

## Publishing to Docker Hub
To publish the Docker image:
* Create a [new release](https://github.com/saucelabs/sauce-cypress-runner/releases)
Expand Down
6 changes: 3 additions & 3 deletions src/cypress-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { promisify } = require('util');
const yaml = require('js-yaml');
const cypress = require('cypress');
let { exec } = require('child_process');
const { getAbsolutePath } = require('./utils');
const { getAbsolutePath, shouldRecordVideo } = require('./utils');

// Promisify the callback functions
const fileExists = promisify(fs.exists);
Expand Down Expand Up @@ -105,7 +105,7 @@ const cypressRunner = async function () {
configFile,
config: {
env,
video: true,
video: shouldRecordVideo(),
videosFolder: reportsDir,
videoCompression: false,
videoUploadOnPasses: false,
Expand All @@ -132,4 +132,4 @@ if (require.main === module) {
cypressRunner();
}

exports.cypressRunner = cypressRunner;
exports.cypressRunner = cypressRunner;
12 changes: 11 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ function getAbsolutePath (pathToDir) {
return path.join(process.cwd(), pathToDir);
}

module.exports.getAbsolutePath = getAbsolutePath;
function shouldRecordVideo () {
let isVideoRecording = process.env.SAUCE_CYPRESS_VIDEO_RECORDING;
if (isVideoRecording === undefined) {
return true;
}
let videoOption = String(isVideoRecording).toLowerCase();
return videoOption === 'true' || videoOption === '1';
}

module.exports.getAbsolutePath = getAbsolutePath;
module.exports.shouldRecordVideo = shouldRecordVideo;
6 changes: 6 additions & 0 deletions tests/env-tests/cypress/integration/env.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// <reference types="cypress" />

const { shouldRecordVideo } = require('../../../../src/utils');

context('Actions', function () {
beforeEach(function () {
cy.visit('https://example.cypress.io/commands/actions');
Expand All @@ -8,4 +10,8 @@ context('Actions', function () {
it('should use .env.json', function () {
expect(Cypress.env('foo')).to.equal('BAR');
});

it('should skip recording cypress video', function () {
expect(shouldRecordVideo()).to.not.equal('true');
});
});
33 changes: 32 additions & 1 deletion tests/unit/src/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getAbsolutePath } = require('../../../src/utils');
const { getAbsolutePath, shouldRecordVideo } = require('../../../src/utils');

describe('.getAbsolutePath', function () {
it('returns absolute path unmodified', function () {
Expand All @@ -7,4 +7,35 @@ describe('.getAbsolutePath', function () {
it('translates relative path to absolute', function () {
expect(getAbsolutePath('path/to/asset/')).toMatch(/\/path\/to\/asset\/$/);
});
});

describe('.shouldRecordVideo', function () {
let previousEnv;
beforeEach(function () {
previousEnv = process.env.SAUCE_CYPRESS_VIDEO_RECORDING;
});

it('returns true when SAUCE_CYPRESS_VIDEO_RECORDING is undefined', function () {
expect(shouldRecordVideo()).toEqual(true);
});
it('returns false when SAUCE_CYPRESS_VIDEO_RECORDING is 0', function () {
process.env.SAUCE_CYPRESS_VIDEO_RECORDING = 0;
expect(shouldRecordVideo()).toEqual(false);
});
it('returns true when SAUCE_CYPRESS_VIDEO_RECORDING is 1', function () {
process.env.SAUCE_CYPRESS_VIDEO_RECORDING = 1;
expect(shouldRecordVideo()).toEqual(true);
});
it('returns true when SAUCE_CYPRESS_VIDEO_RECORDING is true', function () {
process.env.SAUCE_CYPRESS_VIDEO_RECORDING = true;
expect(shouldRecordVideo()).toEqual(true);
});
it('returns false when SAUCE_CYPRESS_VIDEO_RECORDING is false', function () {
process.env.SAUCE_CYPRESS_VIDEO_RECORDING = false;
expect(shouldRecordVideo()).toEqual(false);
});

afterEach(function () {
process.env.SAUCE_CYPRESS_VIDEO_RECORDING = previousEnv;
});
});

0 comments on commit 96b83ec

Please sign in to comment.