You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Run cucumber/gherkin-syntaxed specs with cypress.io
4
4
5
-
Follow the Setup steps, or if you prefer to hack on a working example, take a look at [https://github.com/TheBrainFamily/cypress-cucumber-example](https://github.com/TheBrainFamily/cypress-cucumber-example
5
+
Follow the Setup steps, or if you prefer to hack on a working example, take a look [https://github.com/TheBrainFamily/cypress-cucumber-example](https://github.com/TheBrainFamily/cypress-cucumber-example
Step definition files are by default in: cypress/support/step_definitions. If you want to put them somewhere please use [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) format. For example, add to your package.json :
First please use [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) to create a configuration for the plugin, for example put this section:
51
+
52
+
```json
53
+
"cypress-cucumber-preprocessor": {
54
+
"nonGlobalStepDefinitions": true
55
+
}
56
+
```
57
+
inside your package.json. (this will become the default option in a future version)
58
+
59
+
###### Step definitions creation
60
+
Then put your step definitions in cypress/integration with the folder name matching the .feature filename.
61
+
Easier to show than to explain, so, assuming the feature file is in cypress/integration/Google.feature , as proposed above, the preprocessor will read all the files inside cypress/integration/Google/, so:
62
+
63
+
cypress/integration/Google/google.js (or any other .js file in the same path)
64
+
```javascript
65
+
import { Given } from"cypress-cucumber-preprocessor/steps";
66
+
67
+
consturl='https://google.com'
68
+
Given('I open Google page', () => {
69
+
cy.visit(url)
70
+
})
71
+
```
72
+
73
+
This is a good place to put before/beforeEach/after/afterEach hooks related to THAT PARTICULAR FEATURE. This is incredibly hard to get right with pure cucumber.
74
+
75
+
##### Reusable step definitions
76
+
77
+
We also have a way to create reusable step definitions. Put them in cypress/integration/common/
import { Then } from"cypress-cucumber-preprocessor/steps";
83
+
84
+
Then(`I see {string} in the title`, (title) => {
85
+
cy.title().should('include', title)
86
+
})
87
+
```
88
+
89
+
This is a good place to put global before/beforeEach/after/afterEach hooks.
90
+
91
+
##### Why a new pattern?
92
+
The problem with the legacy structure is that everything is global. This is problematic for multiple reasons.
93
+
- It makes it harder to create .feature files that read nicely - you have to make sure you are not stepping on toes of already existing step definitions. You should be able to write your tests without worrying about reusability, complex regexp matches, or anything like that. Just write a story. Explain what you want to see without getting into the details. Reuse in the .js files, not in something you should consider an always up-to-date, human-readable documentation.
94
+
- The startup times get much worse - because we have to analyze all the different step definitions so we can match the .feature files to the test.
95
+
- Hooks are problematic. If you put before() in a step definition file, you might think that it will run only for the .feature file related to that step definition. You try the feature you work on, everything seems fine and you push the code. Here comes a surprise - it will run for ALL .feature files in your whole project. Very unintuitive. And good luck debugging problems caused by that! This problem was not unique to this plugin, bo to the way cucumberjs operates.
96
+
Let's look how this differs with the proposed structure. Assuming you want to have a hook before ./Google.feature file, just create a ./Google/before.js and put the hook there. This should take care of long requested feature - (https://github.com/TheBrainFamily/cypress-cucumber-preprocessor/issues/25)[#25]
97
+
98
+
If you have a few tests the "oldschool" style is completely fine. But for a large enterprise-grade application, with hundreds or sometimes thousands of .feature files, the fact that everything is global becomes a maintainability nightmare.
99
+
100
+
#### Oldschool/Legacy Cucumber style (please let us know if you decide to use it!)
101
+
102
+
##### Step Definition location configuration
103
+
Step definition files are by default in: cypress/support/step_definitions. If you want to put them somewhere please use [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) format. For example, add to your package.json :
0 commit comments