Autoplay



Transcripted Summary

In the chapter, we will start using the BDD, or behavior-driven development with Cucumber and Appium.

We will cover :

  • Why Cucumber is important

  • How we can prepare our project for BDD and Cucumber with Appium

  • How we can create the Cucumber feature files

  • How we can implement the cucumber step definitions using Appium


# Why use Cucumber in our tests?

Cucumber was created to drive the behavior-driven development, or BDD, process, such that the customer can describe the requirements as a series of examples called scenarios in plain texts.

Then, using Gherkin language in a Given-When-Then format, the customer will be able to transfer it to the feature files.

In Cucumber, these files are called feature files, which are reviewed by a scrum team to get a clear understanding of the requirements, before starting the actual development.

Once development is underway, the developers and QA will start writing step** definitions**, which are essential snippets of code that bind the scenarios from the feature files to the test code, which execute actions against the application under tests.

In our case, it's a mobile application.

In this demo, we will prepare our project for BDD, or behavior-driven development, with Appium by installing the required plugins in our IntelliJ IDE and by adding the required dependencies in our pom.xml file.

So this is our project - I created a new project with Appium BDD and this is a similar structure from the previous one, with the data-driven.


Here we have apps, data, PageObjects, and the test cases.

I just to add our required dependencies for Cucumber and for BDD, which are the cucumber-java, cucumber-core, gherkin, and cucumber-testing.


<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>6.10.2</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>6.10.2</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>gherkin</artifactId>
    <version>17.0.2</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>6.10.2</version>
</dependency>

We can get these from the MVN repository - we can search for it and under io.cucumber, you can find all the dependencies here.

For example, if you are using Spring or if you are using Guice, you can find your required one.

In our case, we are using testng because we added cucumber for testng.

One more thing that we need to add is the plug-ins in our IntelliJ.

From the preferences, we can search for Plugins, and here we can search for Cucumber for Java, which is already installed, but in your case, if it's not installed, you need to install it and restart your IntelliJ to take effect.

The second one is for Gherkins to be able to write feature files with auto-complete syntax.

So here it's already installed also, but in your case, if it's not installed, we need to click install and restart your IDE.


# Create the Cucumber Feature Files

Now we can start creating our first feature.

After we prepared our IntelliJ IDE by installing the plug-ins and adding our required dependencies and our pom.xml file, now it's time to create our feature file.

Under the test folder, we will create a resources directory, like the one that is inside the main folder - so right-click on the test folder, and then click "Directory".

Then we will add resources and inside this one, we will create a new directory features.

Then, right-click on features and we will create a new file and the file will be CreateTask.feature.

Then we will take the BDD template and now we can start writing our feature.

I can start with writing "Feature" and I have auto-complete because I have already installed the Gherkin and the Cucumber plugin on IntelliJ.

I will add the feature name - for example, "Create New Task".

Then I will add the "Scenario", and here I have "Scenario" or "Scenario Outline" if I am using the data table or scenario template, but in this case, I will use "Scenario" and for example, I will add "The user can add new task" and then add "Given Click Add new Task".

We will try to simulate our test script and then try to convert it to a feature file.

Then we will add a new "Given Enter TaskName", and "Given Enter TaskDesc" and then "Given Click Save".

Then the final one will be "Then Task added successfully".


Feature: Create New Task
  Scenario: The user can add new task
    Given Click Add new Task
    Given Enter TaskName
    Given Enter TaskDesc
    When Click Save
    Then Task added successfully

So, this is a simple scenario.

The feature is to create a new task, and we need to click the add new task button.

Then enter the task name and the description, click save, or maybe we can add "When Click Save", then the task is added successfully.

This is a simple feature file.

Now we will create the step definition for this feature file.

In the next video, we will add the step definitions and the runner that's required for running our BDD test cases using Appium.



Resources



Quiz

The quiz for this chapter can be found in Chapter 9.2

© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR