Skip to content

Enable headless key in run_settings #103

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

Merged
merged 4 commits into from
Feb 1, 2021
Merged
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
3 changes: 3 additions & 0 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ module.exports = function run(args) {
//accept the local identifier from env variable if provided
utils.setLocalIdentifier(bsConfig);

// run test in headed mode
utils.setHeaded(bsConfig, args);

// Validate browserstack.json values and parallels specified via arguments
return capabilityHelper.validate(bsConfig, args).then(function (cypressJson) {

Expand Down
6 changes: 6 additions & 0 deletions bin/helpers/capabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ const caps = (bsConfig, zip) => {
if (!Utils.isUndefined(bsConfig.run_settings.cypress_version)){
obj.cypress_version = bsConfig.run_settings.cypress_version;
}

if (!Utils.isUndefined(bsConfig.run_settings.headless) && String(bsConfig.run_settings.headless) === "false"){
obj.headless = bsConfig.run_settings.headless;
} else {
logger.info(`Running your tests in headless mode. Use --headed arg to run in headful mode.`);
}
}

if(obj.parallels === Constants.cliMessages.RUN.DEFAULT_PARALLEL_MESSAGE) obj.parallels = undefined
Expand Down
1 change: 1 addition & 0 deletions bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const cliMessages = {
ENV_DESCRIPTION: "Specify the environment variables for your spec files",
SYNC_DESCRIPTION: "Makes the run command in sync",
BUILD_REPORT_MESSAGE: "See the entire build report here",
HEADED: "Run your tests in a headed browser instead of a headless browser",
},
COMMON: {
DISABLE_USAGE_REPORTING: "Disable usage reporting",
Expand Down
6 changes: 6 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ exports.setLocalIdentifier = (bsConfig) => {
}
};

exports.setHeaded = (bsConfig, args) => {
if (!this.isUndefined(args.headed) && args.headed === true) {
bsConfig.run_settings.headless = false;
}
};

exports.getNumberOfSpecFiles = (bsConfig, args, cypressJson) => {
let testFolderPath = cypressJson.integrationFolder || Constants.DEFAULT_CYPRESS_SPEC_PATH;
let globSearchPatttern = bsConfig.run_settings.specs || `${testFolderPath}/**/*.+(${Constants.specFileTypes.join("|")})`;
Expand Down
5 changes: 5 additions & 0 deletions bin/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ var argv = yargs
default: false,
describe: Constants.cliMessages.RUN.SYNC_DESCRIPTION,
type: "boolean"
},
'headed': {
default: false,
describe: Constants.cliMessages.RUN.HEADED,
type: "boolean"
}
})
.help('help')
Expand Down
3 changes: 2 additions & 1 deletion bin/templates/configTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ module.exports = function () {
"npm_dependencies": {
},
"package_config_options": {
}
},
"headless": true
},
"connection_settings": {
"local": false,
Expand Down
16 changes: 16 additions & 0 deletions test/unit/bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const chai = require("chai"),
const Constants = require("../../../../bin/helpers/constants"),
logger = require("../../../../bin/helpers/logger").winstonLogger,
testObjects = require("../../support/fixtures/testObjects");
const { setHeaded } = require("../../../../bin/helpers/utils");

const proxyquire = require("proxyquire").noCallThru();

Expand Down Expand Up @@ -97,6 +98,7 @@ describe("runs", () => {
capabilityValidatorStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
setHeadedStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
setDefaultsStub = sandbox.stub();
});
Expand Down Expand Up @@ -126,6 +128,7 @@ describe("runs", () => {
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
setHeaded: setHeadedStub,
deleteResults: deleteResultsStub,
setDefaults: setDefaultsStub
},
Expand All @@ -150,6 +153,7 @@ describe("runs", () => {
sinon.assert.calledOnce(getErrorCodeFromMsgStub);
sinon.assert.calledOnce(setLocalStub);
sinon.assert.calledOnce(setLocalIdentifierStub);
sinon.assert.calledOnce(setHeadedStub);
sinon.assert.calledOnce(deleteResultsStub);
sinon.assert.calledOnce(setDefaultsStub);
sinon.assert.calledOnceWithExactly(
Expand Down Expand Up @@ -187,6 +191,7 @@ describe("runs", () => {
deleteZipStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
setHeadedStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
setDefaultsStub = sandbox.stub();
Expand Down Expand Up @@ -217,6 +222,7 @@ describe("runs", () => {
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
setHeaded: setHeadedStub,
deleteResults: deleteResultsStub,
setDefaults: setDefaultsStub,
getNumberOfSpecFiles: getNumberOfSpecFilesStub
Expand Down Expand Up @@ -247,6 +253,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setParallelsStub);
sinon.assert.calledOnce(setLocalStub);
sinon.assert.calledOnce(setLocalIdentifierStub);
sinon.assert.calledOnce(setHeadedStub);
sinon.assert.calledOnce(validateBstackJsonStub);
sinon.assert.calledOnce(capabilityValidatorStub);
sinon.assert.calledOnce(archiverStub);
Expand Down Expand Up @@ -290,6 +297,7 @@ describe("runs", () => {
deleteZipStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
setHeadedStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
setDefaultsStub = sandbox.stub();
Expand Down Expand Up @@ -320,6 +328,7 @@ describe("runs", () => {
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
setHeaded: setHeadedStub,
deleteResults: deleteResultsStub,
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
setDefaults: setDefaultsStub
Expand Down Expand Up @@ -354,6 +363,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setParallelsStub);
sinon.assert.calledOnce(setLocalStub);
sinon.assert.calledOnce(setLocalIdentifierStub);
sinon.assert.calledOnce(setHeadedStub);
sinon.assert.calledOnce(validateBstackJsonStub);
sinon.assert.calledOnce(capabilityValidatorStub);
sinon.assert.calledOnce(archiverStub);
Expand Down Expand Up @@ -401,6 +411,7 @@ describe("runs", () => {
deleteZipStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
setHeadedStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
setDefaultsStub = sandbox.stub();
Expand Down Expand Up @@ -431,6 +442,7 @@ describe("runs", () => {
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
setHeaded: setHeadedStub,
deleteResults: deleteResultsStub,
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
setDefaults: setDefaultsStub
Expand Down Expand Up @@ -473,6 +485,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setParallelsStub);
sinon.assert.calledOnce(setLocalStub);
sinon.assert.calledOnce(setLocalIdentifierStub);
sinon.assert.calledOnce(setHeadedStub);
sinon.assert.calledOnce(archiverStub);
sinon.assert.calledOnce(setUsageReportingFlagStub);
sinon.assert.calledOnce(zipUploadStub);
Expand Down Expand Up @@ -525,6 +538,7 @@ describe("runs", () => {
isUndefinedStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
setHeadedStub = sandbox.stub();
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
});

Expand Down Expand Up @@ -554,6 +568,7 @@ describe("runs", () => {
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
setHeaded: setHeadedStub,
exportResults: exportResultsStub,
deleteResults: deleteResultsStub,
setDefaults: setDefaultsStub,
Expand Down Expand Up @@ -601,6 +616,7 @@ describe("runs", () => {
sinon.assert.calledOnce(setParallelsStub);
sinon.assert.calledOnce(setLocalStub);
sinon.assert.calledOnce(setLocalIdentifierStub);
sinon.assert.calledOnce(setHeadedStub);
sinon.assert.calledOnce(archiverStub);
sinon.assert.calledOnce(setUsageReportingFlagStub);
sinon.assert.calledOnce(zipUploadStub);
Expand Down
130 changes: 130 additions & 0 deletions test/unit/bin/helpers/capabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,136 @@ describe("capabilityHelper.js", () => {
});
});
});

context("headless in run_settings", () => {
it("sets headless if false", () => {
let headless = false;
let zip_url = "bs://<random>";
let bsConfig = {
auth: {
username: "random",
access_key: "random",
},
browsers: [
{
browser: "chrome",
os: "Windows 10",
versions: ["78", "77"],
},
],
run_settings: {
headless: headless
},
};

return capabilityHelper
.caps(bsConfig, { zip_url: zip_url })
.then(function (data) {
let parsed_data = JSON.parse(data);
chai.assert.equal(parsed_data.headless, headless);
chai.assert.equal(parsed_data.env, undefined);
})
.catch((error) => {
chai.assert.fail("Promise error");
});
});

it("sets headless if string false", () => {
let headless = "false";
let zip_url = "bs://<random>";
let bsConfig = {
auth: {
username: "random",
access_key: "random",
},
browsers: [
{
browser: "chrome",
os: "Windows 10",
versions: ["78", "77"],
},
],
run_settings: {
headless: headless
},
};

return capabilityHelper
.caps(bsConfig, { zip_url: zip_url })
.then(function (data) {
let parsed_data = JSON.parse(data);
chai.assert.equal(parsed_data.headless, headless);
chai.assert.equal(parsed_data.env, undefined);
})
.catch((error) => {
chai.assert.fail("Promise error");
});
});

it("does not set headless if true", () => {
let headless = true;
let zip_url = "bs://<random>";
let bsConfig = {
auth: {
username: "random",
access_key: "random",
},
browsers: [
{
browser: "chrome",
os: "Windows 10",
versions: ["78", "77"],
},
],
run_settings: {
headless: headless
},
};

return capabilityHelper
.caps(bsConfig, { zip_url: zip_url })
.then(function (data) {
let parsed_data = JSON.parse(data);
chai.assert.equal(parsed_data.headless, undefined);
chai.assert.equal(parsed_data.env, undefined);
})
.catch((error) => {
chai.assert.fail("Promise error");
});
});

it("does not set headless if truthy", () => {
let headless = "enable";
let zip_url = "bs://<random>";
let bsConfig = {
auth: {
username: "random",
access_key: "random",
},
browsers: [
{
browser: "chrome",
os: "Windows 10",
versions: ["78", "77"],
},
],
run_settings: {
headless: headless
},
};

return capabilityHelper
.caps(bsConfig, { zip_url: zip_url })
.then(function (data) {
let parsed_data = JSON.parse(data);
chai.assert.equal(parsed_data.headless, undefined);
chai.assert.equal(parsed_data.env, undefined);
})
.catch((error) => {
chai.assert.fail("Promise error");
});
});
});
});

describe("validate", () => {
Expand Down
28 changes: 27 additions & 1 deletion test/unit/bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('utils', () => {
sandbox = sinon.createSandbox();
sandbox.stub(utils,'getBrowserCombinations').returns(['a','b']);
});

afterEach(() => {
sandbox.restore();
sinon.restore();
Expand Down Expand Up @@ -545,6 +545,32 @@ describe('utils', () => {
});
});

describe('setHeaded', () => {
it('sets the headless to false', () => {
let args = {
headed: true
};
let bsConfig = {
run_settings: {}
};

utils.setHeaded(bsConfig, args);
expect(bsConfig.run_settings.headless).to.be.eq(false);
});

it('sets the headless to false', () => {
let args = {
headed: false
};
let bsConfig = {
run_settings: {}
};

utils.setHeaded(bsConfig, args);
expect(bsConfig.run_settings.headless).to.be.eq(undefined);
});
});

describe('exportResults', () => {
it('should export results to log/build_results.txt', () => {
sinon.stub(fs, 'writeFileSync').returns(true);
Expand Down