First is navigate to your intended directory and initialized npm by:
npm init
This will create a package.json file on your directory.
Next is to install Cypress via npm:
npm install cypress --save-dev
This will install Cypress locally as a dev dependency for your project.
Running the test with the command below will open the cypress test window:
npx cypress open
Notice that node_modules and cypress folder will be added in the directory as well as other files like cypress-config.js
Now that cypress is installed we can proceed to installation of cucumber plugins and Typescript.
To install Typescript, execute this command:
npm install typescript --save-dev
Initialized typescript by running:
npx tsc --init
This is to define and create your tsconfig file.
Change your tsconfig.json with this configuration:
Next is to install Cypress cucumber preprocessor
In version 10, plugins File is removed because Cypress now supports JavaScript and TypeScript configuration files, a separate "plugins file" (which used to default to cypress/plugins/index.js)
Support for the plugins file has been removed, and it has been replaced with the new setupNodeEvents() and devServer config options.
If you wan't to read further about the depreciation of plugins file follow this link.
Global step definitions exaple
Step definitions besides scenario
Step definitions in a directory besides the scenario
Combination of all of the above (default)
Installing of bundler Browserify and Webpack
Browesrify-ts installation:
And add this under cypress-config.ts file.
Webpack-ts installation:
And add this under cypress-config.ts file.
Install cucumber-json-formatter
And add this at package.json
Now that everything are set, we can start writing our test.
Write a test
Write Gherkin documents and add a file for type definitions with a corresponding name (read more about how step definitions are resolved in docs/step-definitions.md). Reading docs/cucumber-basics.md is highly recommended.
Running a test in browserless mode:
Running the test with browser that directs into the spec file:
For further details and changes about the migration, your can read it in here.
import { defineConfig } from "cypress";
import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor";
import browserify from "@badeball/cypress-cucumber-preprocessor/browserify";
async function setupNodeEvents(
on: Cypress.PluginEvents,
config: Cypress.PluginConfigOptions
): Promise<Cypress.PluginConfigOptions> {
await addCucumberPreprocessorPlugin(on, config);
on(
"file:preprocessor",
browserify(config, {
typescript: require.resolve("typescript"),
})
);
// Make sure to return the config object as it might have been modified by the plugin.
return config;
}
export default defineConfig({
e2e: {
specPattern: "**/*.feature",
setupNodeEvents,
},
});ter
# cypress/e2e/duckduckgo.feature
Feature: duckduckgo.com
Scenario: visiting the frontpage
When I visit duckduckgo.com
Then I should see a search bar
// cypress/e2e/duckduckgo.ts
import { When, Then } from "@badeball/cypress-cucumber-preprocessor";
When("I visit duckduckgo.com", () => {
cy.visit("https://www.duckduckgo.com");
});
Then("I should see a search bar", () => {
cy.get("input").should(
"have.attr",
"placeholder",
"Search the web without being tracked"
);
});