get total number of tests to be executed in from a spec file
I was able to achieve this with a custom command line flag, installing jasmin-fail-fast package, and by failing the first test when that flag is passed. But note that there must always be 1 failure.
Detailed steps
- Pass in a custom flag as a command line argument when launching protractor
- In the onPrepare check if this flag is found
- If it is then do the next 3 steps:
- enable the jasmine-fail-fast package (you will need to download with
npm i jasmine-fail-fast --save
- Add a custom reporter which only logs number of tests in jasmineStarted lifecycle hook
- Add beforeAll with a custom fail message
Project structureNote the app.1.js and app.2.js are identical so there are 8 specs in total.
Command Used
C:\ProtractorProjects\jasmine-test-count>node_modules\.bin\protractor conf.js --params.countSpecs
app.1.js
describe("suite 1", function(){ it("spec 1", function(){ expect(true).toBe(true); }); it("spec 2", function(){ expect(true).toBe(false); }) xit("spec 3", function(){ expect(true).toBe(false); }) xit("spec 4", function(){ expect(true).toBe(false); })});
conf.js
exports.config = { framework: 'jasmine', specs: "app.*.js", onPrepare: function () { if (browser.params.countSpecs) { //Add jasmine fail fast package const failFast = require('jasmine-fail-fast'); jasmine.getEnv().addReporter(failFast.init()); //Add custom reporter which only counts jasmine.getEnv().addReporter({ jasmineStarted: function (suiteInfo) { console.log(`Due to execute #${suiteInfo.totalSpecsDefined} specs in total`); } }); beforeAll(function () { fail("Failing because only a count was required"); }) } }}