Cucumber in Selenium



  • In BDD, users (business analysts, product owners) first write scenarios or acceptance tests that describe the behavior of the system from the customer’s perspective, for review and sign-off by the product owners before developers write their code.

  • This is to bridge the gap between the test cases written by QA and BRD/FD written by Business analyst. Earlier test cases were written on the basis of big document called BRD.

  • Some requirements can be skipped and it becomes difficult to trace requirements and test cases i.e. which test case is for which requirement.

  • Cucumber has made it very easy to track all requirements and test cases.

  • Cucumber is best suited in agile environment where there is always a user story to start with.

What is a user story?

  • User stories are short, simple descriptions of a feature told from the perspective of the person who desires the new capability, usually a user or customer of the system. They typically follow a simple template:
    Template: As a < type of user >, I want < some goal > so that < some reason >.

For Example:

  • As a user when I login into Facebook, home page should appear.

  • As a user when I login into Facebook, home page should appear.

  • As such, they strongly shift the focus from writing about features to discussing them. In fact, these discussions are more important than the text they have written.

  • Now above story will be used in cucumber that will be called as a feature.

  1. It is helpful to involve business stakeholders who can’t easily read code.

  2. Cucumber focuses on end-user experience.

  3. Style of writing tests makes it easier to reuse the code in the tests.

  4. Quick and easy set up as well as execution.

  • One need to know the below given description of what a feature is in order to understand cucumber. Example of feature is as shown below:

  • Feature: Login Functionality Feature

  • Scenario: Login Functionality

  • Given user navigates to SOFTWARETETINGHELP.COM

  • When user logs in using Username as “USER”

  • And password as “password”

  • Then login should be successful

  • And Home page should be displayed

  • Explanation for the above terms:

    1. Given: As mentioned above, given specifies the pre-conditions. It is basically a known state.

    2. When: This is used when some action is to be performed. As in above example we have seen when the user tries to log in using username and password, it becomes an action.

    3. Then: The expected outcome or result should be placed here. For Instance: when ‘verify the login’ is successful it will navigate to ‘successful’ page.

    4. Background: Whenever any step is required to perform in each scenario then those steps needs to be placed in Background. For Instance: If user needs to clear database before each scenario then those steps can be put in background.

    5. And: And is used to combine two or more same types of actions.

    Example of Background:

    Background:
    Given user logged in as database’s administrator.
    And all the junk values are cleared.

First, we will setup the project in eclipse by following the steps as mentioned below:

  1. Eclipse plugin for cucumber.

    Natural is a collection of Eclipse plugins to enable rich smart editing of acceptance tests using natural language definition files. It currently supports Cucumber and JBehave syntax.
    natural eclipse plugin

    natural eclipse plugin

    natural eclipse plugin

  2. Make maven project to get dependencies in project.

    1. Select maven project in eclipse.

      create new maven project

    2. Get quick start as archetype.

      create new maven project

    3. Name it as cucumber-maven.

      create new maven project

    4. We will be asked about version number and properties configuration but we can go with the defaults. The following structure with pom.xml, App.java and AppTest.java (which follows Maven’s Standard Directory Layout) will be created.

    5. It will have the project structure as shown in below image:

      maven project structure

    6. Now that we have a bare-bones Maven project we can start doing Behavior Driven Development by creating a failing acceptance test. We will delete the by default generated App.java and AppTest.java files.

    7. It takes a moment to update pom.xml to add required dependencies and configure the compiler as below below:


    In the above pom.xml file:

    • dependencies tag - We use Cucumber and JUnit so Maven will download the required jars.

    • plugin tag – It is a java compiler configuration.

    Now project structure will look as shown below:

    cucumber maven project structure

    If we see an error as shown below go to problems window and see what problems we have in our project:

    problems window in cucumber project

  • So follow the steps as mentioned below and update maven project due to which errors will be removed. Create ‘feature’ file as shown below in folder ‘resources/ cucumber’ in ‘test’ folder under ‘src’ and save it as “calculator.feature”.


Below is the explanation of the above created ‘feature’ file:

    Line 1 – Let Cucumber know that this is a feature file.

    Line 2-4 – It is a User story which is not executed but we add it for clarity. Line 6 – A new scenario is started.

    Line 7-8 – They are steps wherein the scenario is executed.

  • Now our project structure will be something like this as shown in below image:

  • In order to run the feature, we will create a runner class ‘RunCalculatorTest’ which contains ‘Cucumber’ execution parameters.


In above program line number:

  1. We want Cucumber to run this test and

  2. Passing the Cucumber options i.e. output formatting and location of the feature file.


Skip below steps if no errors are encountered:

If any compilation issues occurs, go ahead with trouble shooting as shown below. Hover the mouse on the error and fix it.

fix project setup in cucumber project

Now our structure will look as shown below:

cucumber maven project structure

  • Run as >> Maven clean >> maven test [perform this repeatedly if the build fails. Here ‘clean’ is used for deleting all compiled code].

  • Now the below shown data will appear in the console.


  • We will get quite a lot of outputs but the most important thing is that Cucumber tells us about missing step definitions and provides skeletons for the implementation.

  • This proves to be a very useful feature as it gives us information about the missing code and helps with writing regular expressions. So let’s put these steps in ‘CalculatorSteps’:

Create a ‘calculator’ class for which we want to do the testing.

Now our project structure will look something like this.

cucumber maven project structure

Now we will be writing actual testing code in CalculatorSteps.java. We will modify the code which we have already created.


Troubleshooting:

  • If compile time error occurs, then clean the project:
    Project >> clean

  • Now when we run the tests,we should get successful results.

  • Now we can see output in console which will be scattered.

  • Console output problems:

  • If our console is not configured to display colored output from Cucumber we may see something that resembles a failed ascii-art like the one shown below.


  • Just modify ‘RunCalculatorTest’ formatting, so we can fix it by telling the runner to give the monochrome output as shown below:


  • Now we will see the out as shown below:


What internally happens here?

When you run maven’s test goal:

  1. It tries to locate tests in the project.

  2. Using the default naming convention it finds ‘RunCalculatorTest’. This test is configured to run with Cucumber.

  3. It will pick up the configuration options including output (format and monochrome), glue code (step implementations) and location of the feature file.

  4. The feature file (written in a language called Gherkin) describes scenarios and their steps.

  5. Scenarios are executed by mapping steps to methods annotated with ‘Given’, ‘When’ and ‘Then’ annotations, instantiating their classes and executing these methods.

  • Scenario and step execution can result in:

    1. error – When there is an error or an exception during execution.

    2. failure – When one of the assertions fails (AssertionError).

    3. pending – When a step throws PendingException.

    4. skip – When a step is not found or there is an exception in a previous step.

    5. pass – When method execution becomes successful.

  • Now try failing test cases just by changing the feature file as shown below.

  • Instead of 5 I write 55 so that we will be able to see how many steps will pass and how many will fail.


Now after running this we will get the following output in console as shown below:

Results:

    Failed tests: Then the result should be 44(Scenario: Add two numbers): expected:<44> but was:<5>

    Scenario: Add two numbers: expected:<44> but was:<5>

    Tests run: 4, Failures: 2, Errors: 0, Skipped: 0

Now we will see some live examples with multiple feature files.

Example of login page of website. In this example we will retain one feature file of calculator as well and add one more feature file. So that we will know how to run multiple feature files.

Directory structure will be as below with addition of new files.

cucumber maven directory structure

In above directory I have added highlighted files and renamed RunALLTest.java as we want to use that class for running all feature files.

    File: jbkfilesystem.feature-

    Description: In this file we will be login with invalid credentials and test error message.


  • Run as >> Maven clean >> maven test [perform this repeatedly if the build fails. Here ‘Clean’ is used for deleting all compiled code].

  • Now from console we can copy methods for our test steps. It will look like below.

    File: JbksiteTestSteps.java

    Description: it will have steps and we will write selenium code in this file also asserts.

    If you can see from feature file we are sending dynamic parameters username as kiran and kiran as a password so you can change this whenever needed in feature file.


Meaning of below line:

@Given ("^website is opened\\. - \"(.*?)\"$")

  • This is website name which we are sending from feature file similarly you can observe for username and password.

Meaning of below line:

public void website_is_opened(String websiteName)

  • In this case we are receiving website name from feature file. In our case. https://javabykiran.com/javabykiranfiles/
    Now we need to have runner class.
    File: RunAllTest.java
    Description: we want to add here list of all feature file or package name where all feature files exists.

Below highlighted is how to give multiple feature files path. You can even mention folder name as well, below we have given folder name as cucumber as you can see in directory structure above:

give multiple feature files path in cucumber

Now you can run a test. Go to maven test.

Calculator test will run same as above and website test case will also start running.

So in total we ran 2 features file.

In console we can see results.

Let’s say we want to test table then we can pass data from feature file as below. We will see feature file and test steps class.

Directory structure will be as below:

cucumber maven directory structure



This is how we can pass table data as well.

It’s very simple, only we need to tag some of scenarios as below.

cucumber tags example

And RunAllTest.java look like.

cucumber tags example

Observe output only smoketest tagging going to run.

If u want to exclude this tag just negate it. Like tags = {"[email protected]"}

In the world of testing, you must have encountered the situations where you need to perform the prerequisite steps before testing any test scenario.

This prerequisite can be anything from:

  • Starting a webdriver

  • Setting up DB connections

  • Setting up test data

  • Setting up browser cookies

  • Navigating to certain page

  • or anything before the test

In the same way there are always after steps as well of the tests like:

  • Killing the webdriver

  • Closing DB connections

  • Clearing the test data

  • Clearing browser cookies

  • Logging out from the application

  • Printing reports or logs

  • Taking screenshots on error

  • or anything after the test

To handle these kind of situations, cucumber hooks are the best choice to use. Unlike TestNG An- notaions, cucumber supports only two hooks (Before & After) which works at the start and the end of the test scenario. As the name suggests, @before hook gets executed well before any other test scenario, and @after hook gets executed after executing the scenario.

How to implement Hooks in Cucumber Test?

Let’s do some easy and small example of Cucumber Hooks just to understand the concept. I will bring the intelligent usage of Hooks in my later tutorial series of Designing Framework with Cucumber.

Test Hooks with Single Scenario

  • Feature File


  • Step Definitions


Note: There is no logic used in the step definitions. Just printing the step summary log.

  • Hooks


    Things to note:

  • An important thing to note about the after hook is that even in case of test fail, after hook will execute for sure.

  • Method name can be anything, need not to be beforeScenario() or afterScenario(). can also be named as setUp() and tearDown().

  • Make sure that the package import statement should be import cucumber.api.java.After; & import cucumber.api.java.Before;

Often people make mistake and import Junit Annotations, so be careful with this.

Output:cucumber hooks example output

We will try understanding some basic questions on Cucumber.

Q.1) What are the differences between Jbehave and Cucumber?

    - Although Cucumber and Jbehave are meant for the same purpose, acceptance tests are completely different frameworks.
  • Jbehave is Java based and Cucumber is Ruby based

  • Jbehave are based on stories while Cucumber is based on features


Q.2) What language is used for expressing scenario in feature file?

    - Gherkin language is used to express scenario in feature files and ruby files containing unobtrusive automation for the steps in scenarios.


Q.3) What are 2 files required to execute a Cucumber Test Scenario?

    - The 2 files required to execute a Cucumber test scenario are:
  1. Features

  2. Step Definition


Q.4) What is Feature File? What Does Feature File consist of?

    - Feature file in cucumber consist of parameters or conditions required for executing code, they are:
  1. Feature

  2. Scenario

  3. Scenario Outline

  4. Given

  5. When

  6. Then


Q.5) What is BDD Framework? What are benefits of BDD in Selenium?

    - BDD is becoming widely accepted practice in agile software development, and Cucumber- JVM is a mainstream tool used to implement this practice in Java. Cucumber-JVM is based on Cucumber framework, widely used in Ruby on Rails world as well as in Java and .Net.

    Cucumber-JVM allows developers, QA, and non-technical or business participants to write features and scenarios in a plain text file using Gherkin language with minimal restrictions about grammar in a typical Given, When, and Then structure.

    The feature file is then supported by a step definition file, which implements automated steps to execute the scenarios written in a feature file. Apart from testing APIs with Cucumber-JVM, we can also test UI level tests by combining Selenium WebDriver.


Q.6) What is Profile in Cucumber?

    - We can create Cucumber profiles to run specific features and step definitions.

    We can use following command to execute a cucumber profile,
    cucumber features -p <profile_name>
    Ex: cucumber features -p regression


Q.7) What are Before, After, Beforestep and Afterstep Hooks?

    - Before: execute before the feature file execution

    After: executes after the feature file execution

    BeforeStep: executes before the each step execution

    AfterStep: executes after the each step execution


Q.8) What are Cucumber Tags? Why we use the Tags?

    - Cucumber tags used to filter the scenarios. We can tag the scenarios and we can execute the scenarios based on tags, We can add tags to scenarios with @ We can use following command to execute a cucumber tagged scenarios.
    cucumber features -t @<tag_name>
    Ex: cucumber features -t @test


Q.9) What is Cucumber Dry Run?

    - Cucumber dry run is used to compile cucumber feature files and stepDefinitions. If there is any compilations errors it will show when we use dry run.
    Ex: Cucumber features –dry-run