Search code examples
javascriptasynchronouspromiseasync-awaittestcafe

Create dynamic tests for TestCafe asynchronously


I am creating tests in TestCafe. The goal is to have the tests written in Gherkin. I looked at some GitHub repositories which integrate Cucumber and TestCafe but I am trying a different angle.

I would like to use the Gherkin parser and skip Cucumber. Instead I will create my own implementation to run the teststeps. But currently I am stuck trying to get TestCafe to run the tests.

If I am correct the issue is that TestCafe is running my test file, and then sees no fixtures or tests anywhere. Which is correct because the Gherkin parser is using the stream API (it uses a seperate Go process to parse the feature files) to deliver the data, which means that in my current code the Promise is still pending when TestCafe quits. Or if I remove that the end callback hasn't happened yet.

Is my analysis correct? If yes how can I get all the data from the stream and create my tests such that TestCafe will run it?

gherkin_executor.js

var Gherkin = require('gherkin');

console.log('start')

const getParsedGherkin = new Promise((resolve, reject) => {
    let stream = Gherkin.fromPaths(['file.feature'])

    let data = []
    stream.on('data', (chunk) => {
        if(chunk.hasOwnProperty('source')){
            data.push({source: chunk.source, name: null, pickles: []})
        }
        else if (chunk.hasOwnProperty('gherkinDocument')){
            data[data.length-1].name = chunk.gherkinDocument.feature.name
        }
        else {
            data[data.length-1].pickles.push(chunk.pickle)
        }
    })
    stream.on('end', () => {
        resolve(data)
    })
})
let data = getParsedGherkin.then((data) => {return data})
console.log(data)

function createTests(data){
    for(let feature of data){
        fixture(feature.name)
        for(let testcase of feature.pickles){
            test(testcase.name, async t => {
                console.log('test')
            })
        }
    }
}

file.feature

Feature: A test feature

    Scenario: A test case
        Given some data
        When doing some action
        Then there is some result

Solution

  • Nice initiative!

    To go further in your approach, the method createTests must generate the TestCafe code in at least one JavaScript or TypeScript file. Then you must start the TestCafe runner from these files.

    So now, to go further in your approach, you must write a TestCafe source code generator.

    Maybe the hdorgeval/testcafe-starter repo on GitHub could be an alternative until Cucumber is officially supported by the TestCafe Team.