Talking About TestingATT</>
Cypress and Cucumber

Score 100% on the quiz to continue

Lesson
Free Preview

Testing your first feature

Write your first feature file in Gherkin, the language Cucumber understands.

When using Cucumber together with automated testing tools such as Cypress, we write tests in a language known as Gherkin.

Gherkin is a domain-specific language that allows the use of some keywords to describe features and their behaviors.

Example:

# cypress/e2e/login.feature

Feature: Login

  Scenario: successful login
    Given I access the Login form
    When I submit it with valid credentials
    Then I see the logged-in features available
  • The Feature keyword defines the feature under test
  • The Scenario keyword describes the scenario, or test case
  • The Given keyword defines the precondition of the test, that is, the initial state for that scenario to be tested
  • The When keyword defines the action (or actions) that exercise the application under test for a given scenario
  • The Then keyword defines the expected result, that is, what should happen after the previous action (or actions) are executed
  • The And keyword is an alias for Given, When, or Then, and can be used when the precondition, action, or expected result has more than one step

Commands overview 📖

Below are the Gherkin keywords used in this lesson's exercise.

Feature

Defines the high-level feature or capability being described and tested.

Syntax:

Feature: <description>

Example:

Feature: Login

Scenario

Describes a single test case, representing one specific behavior of the feature.

Syntax:

Scenario: <description>

Example:

Scenario: successful login

Given

Defines the initial state or precondition for the scenario to be tested.

Syntax:

Given <step description>

Example:

Given I access the Login form

When

Defines the action or event that exercises the feature under test.

Syntax:

When <step description>

Example:

When I submit it with valid credentials

Then

Defines the expected outcome after the action described in the When step.

Syntax:

Then <step description>

Example:

Then I see the logged-in features available

And

An alias for Given, When, or Then. Use it to add a second (or third) step to the same part of the scenario, avoiding repetition of the same keyword.

Syntax:

And <step description>

Example:

Feature: Login

  Scenario: successful login
    Given I access the Login form
    And the form is empty
    When I fill in my credentials
    And I click the submit button
    Then I see the logged-in features available
    And the login form is no longer visible

Exercise 🎯

  1. Inside the cypress/e2e/ folder, create a folder called engagesphere/
  2. Inside the cypress/e2e/engagesphere/ folder, create a folder called cookies/
  3. Inside the cypress/e2e/engagesphere/cookies/ folder, create a file called cookies.feature
  4. Finally, in Gherkin language, write a test for the scenario where the EngageSphere application's cookie consent banner is accepted, and another for when it's declined.
From the Cypress App, run the tests to ensure your implementation works.

If the Cypress App isn't open, use the npm run cy:open command to open it.

When you run the tests, you should see an error indicating that the step implementation is missing. Don't worry about it. That's because we haven't implemented it yet. We'll do it in the next lesson.

Show the world what you learned 🌎

To show your professional network what you learned in this lesson, post the following on LinkedIn.

I am taking the course "End-to-End Testing with Cypress and Cucumber: The Definitive Guide 🥒" by @Walmyr Lima e Silva Filho at the @Talking About Testing School, where I learned about the Gherkin language for writing automated tests with Cypress and Cucumber. #TalkingAboutTesting #TATSchool #Cypress #Cucumber

👨‍🏫 Remember to tag me in your post. Here is my LinkedIn profile.

Quiz

Question 1 of 2
Score: 0

What is the purpose of the `Given` keyword in a Gherkin scenario?

Coding Challenge

Write a Cookie Consent Feature File

Write a Gherkin feature file that describes the scenario where a user accepts the cookie consent banner on the EngageSphere app.

Score 100% on the quiz to continue