Forum Discussion

Steve48's avatar
Steve48
New Contributor
5 years ago

Cucumber Puppeteer Node JS: error trying to call step function with arguments

Below is code where I try to include arguments in a Then statement (last line) when I am calling a step function I get an error message stating invalid third argument. So how do I pass parameters to ...
  • tooky's avatar
    5 years ago

    Cucumber step definitions take a matcher, (some options - which we'll ignore for now) and a function. This example defines an anonymous function inline with the step definition:

     

     

    When('step matcher', () => {
      // function to run when the step matches
    });

     

     

    It is also possible to use a named function and pass the function to the step definition, e.g.:

     

    function stepFunction() {
     // function to run when the step matches
    }
    
    When('step matcher', stepFunction);

     

    You can define steps so that they will extract part of the step text and pass it as an argument to the function that Cucumber executes when it runs, e.g.

     

     

    When('step matches {string}', (theMatchedString) => {
    // function to run when the step matches
    });

     

     

    You can of course extract a named function which takes an argument and pass that to Cucumber:

    stepFunctionWithArgument(theMatchedString) {
    // function to run when the step matches
    }
    
    When('step matches {string}', stepFunctionWithArgument);

     

    If we look at your example you are calling the confirmSheetCreated function when you are creating the step definition, not when you are executing your Gherkin specification.

    You probably need to capture the arguments you want to pass to the function in the step matcher, something like:

    async function confirmSheetCreated(folderName,sheetName) {
        await this.page.waitFor(1500);
        let sheetExists=await this.sheetExists(folderName,sheetName)
        await this.page.isTrue(sheetExists);
    }
    
    // this will match: I confirm in folder: "Revenue" new sheet created: "Test Sheet 1"
    // and pass the folderName and sheetName parameters at runtime
    Then( 'I confirm in folder: {string} new sheet created: {string}',{ timeout: 5*1000 },  confirmSheetCreated);


    Alternatively, if those folder and sheet details are always the same you could push them into the function:

    async function confirmSheetCreated() {
        await this.page.waitFor(1500);
        let sheetExists=await this.sheetExists('Revenue','Test Sheet 1')
        await this.page.isTrue(sheetExists);
    }
    Then( 'I confirm new sheet created',{ timeout: 5*1000 },  confirmSheetCreated);