Can I run a Before hook once per feature file in cucumber?

Viewed 2640

I have a bunch of feature files with a bunch of Scenarios each. The first 2 or 3 steps depending on the case, are the same for each Scenario.

  • Login
  • Navigate to X page

So in a .feature file I have 10 scenarios, I'm running 20 steps that I could just run once and execute all my tests.

Is that possible with cucumber? Using Background executes the step for each scenario, I want to do that for each feature files. Sort of context for all my scenarios.

BTW, I'm using cypress-cucumber-preprocessor? But I think is the same.

1 Answers

Did you try using before/after ? In my case, I want to skip the part of adding login/logout steps before each feature. So, I did the following in support/stepdefinitions/global.js

 before(() => {
  cy.log(
    "This will run once before all tests, you can use this to for example start up your server, if that's your thing"
  );
  cy.visit(Cypress.env("url") + "/login");
  cy.fixture("login").then((inputdata) => {
    cy.log("Login to application");
    LoginPage.changeLanguage("en");
    LoginPage.login(inputdata.username, inputdata.password, inputdata.pin);
  });
  cy.on("uncaught:exception", (err) => {
    // expect(err.message).to.include('Ignoring error for now');
    return false;
  });
});

after(() => {
  cy.log("Logout from application");
  HomePage.clickUserDropdown();
  HomePage.clickLogout();
});

Related