Forum Discussion

Steve48's avatar
Steve48
New Contributor
4 years ago
Solved

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 the function?

async function confirmSheetCreated(folderName,sheetName) {
    await this.page.waitFor(1500);
    let sheetExists=await this.sheetExists(folderName,sheetName)
    await this.page.isTrue(sheetExists);
}



When('I click plus button in top menu', { timeout: 5*1000 }, clickAddSheet);
When('I click Sheet option in dropdown', { timeout: 5*1000 }, clickSelectSheet);
When('I fill in New Sheet Lookup dialog', { timeout: 5*1000 },fillNewLookupSheetDialog);
When('I click Create New Sheet button',{ timeout: 5*1000 },clickCreateNewSheet);
Then( 'I confirm new sheet created',{ timeout: 5*1000 },  confirmSheetCreated('Revenue','Test Sheet 1'));
  • 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);

     

1 Reply

  • tooky's avatar
    tooky
    SmartBear Alumni (Retired)

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