get total number of tests to be executed in from a spec file get total number of tests to be executed in from a spec file selenium selenium

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

  1. Pass in a custom flag as a command line argument when launching protractor
  2. In the onPrepare check if this flag is found
  3. If it is then do the next 3 steps:
  4. enable the jasmine-fail-fast package (you will need to download with npm i jasmine-fail-fast --save
  5. Add a custom reporter which only logs number of tests in jasmineStarted lifecycle hook
  6. 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.

enter image description here

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");            })        }    }}

Outputenter image description here