Search code examples
testingbddcucumberjswebdriver-iochimp.js

Cucumber - Javascript Invoke Login Step Definitions Before Other Step Definitions


Using Chimp.js, Cucumberjs and WebdriverIO, I'm trying to run login step definitions in a browser instance, before other step definitions that depend on a user to be logged in. And possibly without adding them into the Background over and over again for each feature file.

Is this possible? I'm quite new to Wedbdriver.io and Cucumber and any advice would be a great help. Please let me know if more info is needed.


Solution

  • Personally I don't think this is a good idea. To log someone in you have to specify 'who' the user is. Later when your application becomes more complex you might have interactions between different users. Hiding any of this from the scenario is not good.

    What you can do is combine user specification and login in single steps e.g.

    Given I am logged in as an admin
    Given Fred is logged in as a sales executive
    

    etc.

    If you are clever about how you implement these steps you can keep things fairly dry by extracting helper methods from the step definitions and using global variables to store people e.g.

    Given 'I am logged in as an admin' do
      @i = create_user role: admin
      login as: admin, user: @i
    

    and reuse these methods in other login steps.

    If you organise your features well, you can background alot of these calls e.g.

    Feature: Basic admin ops
      Background: 
        Given I am logged in as an admin
    
      Scenario: I can foo
        When I foo
    
      Scenario: I can bar
        When I bar
    

    Some final thoughts ...

    Each scenario is there to drive a particular piece of development. Compared to the work of doing the development writing "Given I am logged in" is trivial.

    When something goes wrong knowing that you were supposed to be logged in is an essential piece of information.