codeceptjs / CodeceptJS

Supercharged End 2 End Testing Framework for NodeJS
http://codecept.io
MIT License
4.11k stars 727 forks source link

Not possible to use Before or BeforeSuite with BDD #1298

Open jordiponsllaurado opened 6 years ago

jordiponsllaurado commented 6 years ago

What are you trying to achieve?

I want to implemente a Before/BeforeSuite hook for all my tests

What do you get instead?

BeforeSuite is not defined

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

Could not include object Step Definition from ./tests/step_definitions/steps.js from module 'project/tests/step_definitions/steps.js'
BeforeSuite is not defined

Error:
    at loadSupportObject (/Users/xxx/.nvm/versions/node/v8.10.0/lib/node_modules/codeceptjs/lib/container.js:212:11)
    at loadGherkinSteps (/Users/xxx/.nvm/versions/node/v8.10.0/lib/node_modules/codeceptjs/lib/container.js:201:5)
    at Function.create (/Users/xxx/.nvm/versions/node/v8.10.0/lib/node_modules/codeceptjs/lib/container.js:34:25)
    at Codecept.init (/Users/xxx/.nvm/versions/node/v8.10.0/lib/node_modules/codeceptjs/lib/codecept.js:37:15)
    at Command.module.exports (/Users/xxx/.nvm/versions/node/v8.10.0/lib/node_modules/codeceptjs/lib/command/run.js:35:14)
    at Command.listener (/Users/xxx/.nvm/versions/node/v8.10.0/lib/node_modules/codeceptjs/node_modules/commander/index.js:315:8)
    at emitTwo (events.js:126:13)
    at Command.emit (events.js:214:7)
    at Command.parseArgs (/Users/xxx/.nvm/versions/node/v8.10.0/lib/node_modules/codeceptjs/node_modules/commander/index.js:651:12)
    at Command.parse (/Users/xxx/.nvm/versions/node/v8.10.0/lib/node_modules/codeceptjs/node_modules/commander/index.js:474:21)

Provide test source code if related

// Scenario:
Feature: Change published block type

  As a Typeform user
  I want to change the type of published blocks
  So I can edit my forms quickly

  Scenario: Change published block type

    Given an editor with a multiple choice block
    And I have changed the block type to dropdown
    When the form is saved
    Then the payload should be valid

//Steps definition
const I = actor()

const assert = require('assert')

BeforeSuite((I) => {
  I.amOnPage('/')
})

Details

{
  "output": "./output",
  "helpers": {
    "Puppeteer": {
      "url": "http://localhost:9000",
      "waitForNavigation": [
        "domcontentloaded",
        "networkidle0"
      ],
      "windowSize": "1280x800",
      "chrome": {
        "args": [
          "--no-sandbox",
          "--disable-setuid-sandbox"
        ]
      },
      "show": false
    },
    "RequestInterceptionHelper": {
      "require": "./tests/request_interception_helper.js"
    },    
    "StepHelper": {
      "require": "./tests/step_helper.js"
    }
  },
  "include": {
    "customizeThemePage": "./tests/pages/customize-theme.js",
    "notificationPanelPage": "./tests/pages/notifications_panel.js"
  },
  "mocha": {},
  "bootstrap": false,
  "teardown": null,
  "hooks": [],
  "gherkin": {
    "features": "./tests/features/*.feature",
    "steps": [
      "./tests/step_definitions/steps.js"
    ]
  },
  "timeout": 10000,
  "name": "xxx"
}
kobenguyent commented 6 years ago

I think you mixed the beforeSuite with Background of gherkin. For feature file, you can only add Background which is literally beforeSuite when you develop non gherkin style. Please check this document https://codecept.io/bdd/#advanced-gherkin

jordiponsllaurado commented 6 years ago

But then this force you to repeat again the same code on all the Given statements, like if you want to go to a specific URL for all the tests. No?

kobenguyent commented 6 years ago

Background in Cucumber is used to define a step or series of steps which are common to all the tests in the feature file. It allows you to add some context to the scenarios for a feature where it is defined. A Background is much like a scenario containing a number of steps. But it runs before each and every scenario where for a feature in which it is defined.

That's how thing go when you go with Gherkin.

jordiponsllaurado commented 6 years ago

Yes I know but what if I don't want to add a background on each feature, is it possible to not do it?

In Ruby for example is possible to create hooks so you don't need to

akdasa commented 5 years ago

Here is a snippet from documentation:

// inside step_definitions
Before((test) => {
  // perform your code
  test.retries(3); // retry test 3 times
});

It throws the following exception:

Could not include object Step Definition from ./step_definitions/users.steps.js from module 'XXXX' Before is not defined

I understood what Background is a better way to organize steps. Here is my point:

  1. Is documentation outdated?
  2. How to organize steps like "clean DB" better with Background?

Thanks.

kobenguyent commented 1 month ago

I'm not really understanding much the bdd implementation but I think you could achieve this by using event listeners

For example

const event = require('codeceptjs').event;

module.exports = function() { event.dispatcher.on(event.all.before, function () { console.log('--- I am before all --'); // your pre-requisite steps goes here }); }