Mateusz Kleinert
Mateusz Kleinert

Reputation: 1376

Dynamically create test cases in Nightwatch.js

Is there a way to create test cases dynamically in Nightwatch.js?

An example use case:

I would like to run the "Conformance" test suite from the Qual-E test engine and use Nightwatch.js to simply read the results of the test cases from the page. At this moment I have a single module file with each test case defined as a separate function:

module.exports = {
    'AudioContext' : function (browser) {
        // test's code
    },

    ...

    'MediaList.length' : function (browser) {
        // test's code
    }
};

When the "Conformance" test suite from the Qual-E test engine changes (which happens from time to time) I need to update the list of test cases in my module file. I would like to have only a single function inside this module file (e.g. the before function) that will read the Qual-E page as a first step and spawn test cases in runtime, so I will always have an up-to-date test suite.

Upvotes: 1

Views: 782

Answers (1)

Mateusz Kleinert
Mateusz Kleinert

Reputation: 1376

It turned out every exported function is treated as a test case function (except some reserved functions like before, after, etc.). Here is an example solution:

module.exports = {
    ...
};

(function() {
    var testCasesList = [
        // [testCaseID, testCaseName]
    ];

    function testFunction(browser, testCaseID) {
        // Generic test case body
    }

    function createTests(object) {
        function createFunction(testCaseID) {
            return function(browser) {
                testFunction(browser, testCaseID);
            };
        }

        for (var i = 0; i < testCasesList.length; i++) { 
            testCaseID = testCasesList[i][0];
            object[testCasesList[i][1]] = createFunction(testCaseID);
        }
    }

    createTests(module.exports);
})();

Upvotes: 4

Related Questions