CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

Passing a function as a parameter to a helper method

Open dwaned opened this issue 8 years ago • 4 comments

What are you trying to achieve?

Since functions which are not assigned to the I (actor) are not outputted as part of the printed steps, I was thinking of making a generic function in my helper and its param would be a function.

What do you get instead?

I am getting the error saying that the passed parameter is not a function.

Provide console output if related. Use --verbose mode for more details.

# paste output here

Provide test source code if related

In my Scenario:

... let actualLimits = await depositPage.getDisplayedDepositLimits(); let expectedLimits = await depositPage.getLimitsFromOptionsService(payment.NAME); I.doAssertion(depositPage.verifyingDepositLimits(actualLimits,expectedLimits));

In my helper

... doAssertion(assertFunc){ assertFunc(); };

This way I would be able to keep my existing assertion methods within the page objects and start seeing these steps as part of the output.

Details

  • CodeceptJS version: 1.1.4
  • NodeJS Version: v.9.3.0
  • Operating System: Mac OS High Sierra
  • Protractor || WebDriverIO || Nightmare version (if related): WebdriverIO

dwaned avatar Feb 10 '18 14:02 dwaned

You have a mistake in your code.

I.doAssertion(depositPage.verifyingDepositLimits(actualLimits,expectedLimits));

The problem is that you are not passing a function, instead you’re passing the result of your function.

Try

I.doAssertion(() => depositPage.verifyingDepositLimits(actualLimits,expectedLimits));

reubenmiller avatar Feb 11 '18 20:02 reubenmiller

You're right :-) I realized afterwards that it evaluates and since the function does not have a return value, was returning as undefined. Thank you for the solution.

dwaned avatar Feb 11 '18 21:02 dwaned

Don’t forget to close the issue once it’s resolved ;)

reubenmiller avatar Feb 12 '18 04:02 reubenmiller

Thank you!

dwaned avatar Feb 13 '18 08:02 dwaned