Steps
Local Steps
Steps definitions are created when one of the Given
When
or Then
functions
are executed. They can be placed at the top level of a file, or nested inside either
Feature
, Rule
, Scenario Outline
Scenario
functions.
import { Given, When, Then, Feature, Scenario } = '@autometa/cucumber-runner';
// Top Level Steps
Given('some given step text', ()=>{});
When('some when step text', ()=>{});
Then('some then step text', ()=>{});
// Feature level steps
// these will overrride top level steps is clashing
Feature(() => {
// Override the top level step for
// all scenarios in this feature.
// Also overrides global steps.
Given("some given step text", () => {})
Scenario('My Scenario', ()=>{
// Override the feature step of the same text
Given('some given step text', () => {})
// define a new step unique to this scenario
Given('a new step', ()=>{})
})
}, './my-file.feature')
If a step is defined at two or more levels of nesting (scope) and both
match a .feature
step, the innermost scope will be used and higher level
matches will be ignored.
Global Steps
The easiest way to create steps is to use global steps. Global Steps behave like steps in official Cucumber, where they are registered and will automatically be assembled into scenarios.
To use global steps, the globalsRoot
must be defined in autometa.config.ts
.
import "reflect-metadata";
import { defineConfig } from "@autometa/cucumber-runner";
defineConfig({
globals: "globals"
});
And ensure the config fill is executed by your test runner
- Vitest
- Jest
import { defineConfig } from 'vitest/config'
defineConfig({
...
setupFiles: ['autometa.config.ts']
include: ['**/*.{test,spec,feature}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}']
...
})
export default {
...
setupFilesAfterEnv: ['autometa.config.ts']
testMatch: ['**/?(*.)+(spec|test|feature).[jt]s?(x)']
...
}
Defining Global Steps
Once a global step root has been configured, any steps within the globalsRoot
directory will be registered in a global cache. Test files, which are files
executing the Feature
function from @autometa/cucumber-runner
, will automatically
assemble their scenarios, rules and scenario outlines using these steps
import { Given } from "@autometa/cucumber-runner";
Given("a user logs in", () => {});
Then, assuming all relevant steps are defined globally, we can execute our feature by simply referencing the file.
import { Feature } from "@autometa/cucumber-runner";
Feature("./my-feature.feature");
Importing Global Steps manually
It may be desireable to declare global steps that are not automatically imported, for example they cover a seperate or integrating domain, and may clash with the standard step setup defined.
To accomodate this, you can define global steps in a file not
inside the globalsRoot
, and import
it. This will register
the defined steps for the file being executed.
Given("a user performs obscure action", () => {});
import "../domain-b/user.steps";
import {
Given,
When,
Then,
Feature,
Scenario
} from "@autometa/cucumber-runner";
Feature("./my-feature.feature");