Search code examples
javascriptnightwatch.jsnightwatch

Custom commands or utility classes/functions?


I'm working with a team that has a project in Nightwatch.js. They are defining commands for almost everything:

  • Datetime functions as commands (return current day, array of days in a period, etc.)
  • SQL queries to get data as commands

To me, some of these fit better to utility classes, but they prefer to have them as commands, so they can access them via browser.Command.

What's the correct approach or recommendation? All in commands looks odd to me, but also the codes look more clean, as you don't need to do imports.

Thanks,


Solution

  • This question will probably get closed for being opinion based, but anyway.

    I would agree that not all utilities should be commands. I would generally see the scope of command to be more to do with acting on things in the browser as the user would, or a way to group user actions, e.g. in a typical system I would see commands like -

    • login(), which would group the clicking into text fields and entering the username password and eventually clicking the login button.
    • addUser(), which would maybe be a small, but repetitive group of commands to add a user to an admin panel for example.
    • dragTo(x), which would maybe handle some drag and drop functionality.

    etc.

    I wouldn't see date utils etc as commands, but as you say, just utility functions, that are more about the underlying implementation of the test, and not how the user would interact with the system.

    Although not Nightwatch, maybe take some advice from the Best Practices of Custom Commands from Cypress - https://docs.cypress.io/api/cypress-api/custom-commands#Best-Practices