Cucumber tutorials LEARNOVITA

Cucumber Testing | A Concise Tutorial for Beginners

Last updated on 10th Aug 2022, Blog, Tutorials

About author

Naren sharma (Test Engineer - Automation and Cucumber Testing )

Naren Sharma is a testing framework that supports behavior-driven development (BDD), allowing users to define application operations in plain text. He is expertise in RSpec BDD, TestNG, BDD, Agile testing, RFT, Selenium TDD, and Gherkin.

(5.0) | 19547 Ratings 2168

What is cucumber?

Cucumber is a testing application that promotes Behavior Driven Development (BDD). It provides a method for writing tests that anyone, regardless of technical knowledge, can comprehend. Before developers write code, users (business analysts, product owners) prepare scenarios or acceptance tests that describe the behavior of the system from the customer’s perspective for assessment and approval by the product owners. The Ruby programming language is used by the Cucumber framework.

cucumber testing

How does BDD function in Cucumber Automation?

Assume you’ve been tasked with developing a Funds Transfer module for a Net Banking application.There are several approaches to test it in the Cucumber Testing Framework.

    1. 1.Fund Transfer should occur if there is sufficient balance in the source account.
    2. 2.Fund transfer should occur if the destination account details are correct.
    3. 3.Fund Transfer should occur if the transaction password / rsa code / security authentication given by the user is correct.
    4. 4.Fund Transfer should occur even if it is a bank holiday.
    5. 5.Fund transfer shall occur on a future date specified by the account holder.

As we consider additional features such as transfer amount X for an interval Y days/months, the Test Scenario becomes more comprehensive and complex.

Cucumber Software Benefits :

Cucumber Software Benefits
  • It is useful for including business stakeholders who cannot readily comprehend code
  • User experience is the main focus of the Cucumber Testing tool.
  • The style of writing tests allows for easier code reuse in the tests.
  • Setup and execution are quick and simple.
  • The Cucumber test tool is an effective testing tool.

More Benefits :

  • Cucumber facilitates closer communication, assisting teams in keeping the business purpose in mind. Cucumber serves as a bridge between business and technical language, which we may achieve by writing test cases in plain English prose.
  • Non-Programmer Participation: Test scripts can be written without any coding skills and allow non-programmers, such as manual testers, to participate.
  • Cucumber is really simple to set up and utilize. It is simple to set up with IDEs such as Eclipse and IntelliJ. For enhanced support, install the IDE-specific Cucumber plugins.
  • Cucumber supports code reusability due to its basic test script architecture, Hooks, Background, and Data Table.
  • Cucumber is a tool that is completely free to use. Cucumber and related libraries can be downloaded and used via the repository website.
  • Cucumber supports numerous languages, including Java,.Net C#, Ruby, PHP, Python, and others.
  • Cucumber Scripts are written in the Gherkin language and support several languages. Keywords and stages can be written in a variety of languages. Cucumber makes it extremely simple to test a multilingual website.
  • Cucumber can be readily integrated with other frameworks such as TestNG, JUnit, NUnit, Selenium, and others.
  • Cucumber includes built-in support for Data-Driven Testing via Data Tables and Scenario Outline Examples.
  • Cucumber’s reporting features include HTML, JSON, and XML. Other reporting libraries, such as Extent and Allure, can also be integrated.
  • Cucumber is a well-known tool for community support. On the Internet, there are hundreds of tutorials and discussion boards.

Cucumber vs Selenium:

    CucumberSelenium
    It is a Behavior Driven Development tool used to develop test cases for the behavior of software’s functionality. It is an automated testing tool.
    Cucumber is a free or open-source BDD (Behavior Driven Development) tool. Selenium is also a free or open-source testing tool.
    Cucumber is a BDD supported tool. Selenium is a both Functional and Performance (Selenium Grid) testing tool.
    Cucumber framework supports many languages, such as Java, Scala, Groovy, etc. beyond Ruby. Selenium also supports many languages, such as Java, .Net, etc.
    Cucumber includes both testers and developers to write automation steps. Like Cucumber, Selenium also includes both testers and developers to write automation steps.
    Cucumber is used to test only web applications. Like Cucumber, Selenium is also used to test only web applications.
    Cucumber testing is less reliable as compared to Selenium and QTP. The process of Selenium makes testing more reliable and dependable.
    Cucumber works very fast in Plugin. Selenium works slower in Plugin than Cucumber.

Step-by-Step Instructions for Using Cucumber:

  • Locate the Feature File: During execution, Cucumber looks for features files containing tagged scenarios written in Gherkin. Scenario, Scenario Outlines, Steps, Tags, Examples, and so forth are all included in the features file. In Cucumber, each test case is referred to as a scenario. A feature file is a collection of this Scenario. A Search Feature file, for example, is made up of numerous search test cases.
  • Search for the Linked Step Definition File: Each Scenario has its own mapping step definition. A step definition is written in a computer language such as Java, Ruby, or Python. Step definitions provide the code step that is translated
  • to a single or many situations. This code is linked to other pieces of code and automation libraries such as Selenium.
  • Scenario Pass or Fail: Cucumber advances to the next step in the Scenario if the code in the step specification executes without error. Cucumber recognises the Scenario as passed if it reaches the end of the Scenario without any of the stages failing. In other words, if any Step in the Scenario fails, Cucumber marks the Scenario as failed and proceeds to the next Scenario.
  • Write the following results to reports and the console: Cucumber outputs the results of the scenarios in the console or the Report you have enabled, showing you exactly what is working and what isn’t.

What are Cucumber Tags?

Cucumber Tags

Assume you have numerous different feature files that cover all of the application’s capabilities. Now, there may be times in the project when you would like to run only SmokeTests, End2EndTests, or RegressionTests. One technique is to begin by generating new feature files with the type’s name, such as SmokeTests.features or End2EndTests.feature, and then copy-paste your current tests into them. However, this would render the project filthy and necessitate extra care in the future. So, how should such circumstances be handled in terms of execution?

Cucumber has already offered a means to organise your scenario execution by utilising tags in the feature file for this purpose. Each situation can be defined by a relevant tag. Later, in the runner file, we may specify which exact tag (and hence which scenario(s)) Cucumber should execute. The tag begins with “@”. Following “@,” you can add any relevant text to specify your tag, such as @SmokeTests, directly above the scenarios you want to label. Then, to target these tagged cases, simply enter tags = “@SmokeTests” in the CucumberOptions.

Tagging is applicable not just to Scenarios but also to Features. This means you can tag your feature files as well. Any tag on a Feature will be inherited by Scenario, Scenario Outline, or Scenario.

What are Cucumber Hooks?

Hooks are blocks of code that run before or after each scenario in Cucumber. You can define them using the methods @Before and @After anywhere in your project or step definition layers. Cucumber Hooks enables us to better manage the code workflow and decrease code redundancy. We can call it an unseen phase that allows us to run our scenarios or tests.

Why are Cucumber Hooks used?

You must have faced circumstances in the testing world when you needed to conduct preparatory tasks before testing any test case. This requirement could be anything from:

  • Launching a webdriver
  • Creating Database Connections
  • Configuring test data
  • Configuring test data
  • Navigating to a specific website
  • or doing anything else prior to the test

Similarly, there are always after steps of the tests, such as:

  • removing the webdriver
  • DB connection closure
  • Delete the test data
  • Delete Browser Cookies
  • Logging out of the programme
  • Reports and logs can be printed.
  • Taking screenshots of errors
  • or anything else that occurs after the test

Cucumber hooks are the ideal tool for dealing with these kind of issues. Cucumber, unlike TestNG Annotations, only offers two hooks (Before & After), which work at the beginning and end of the test scenario. As the name implies, the @before hook is executed before any other test scenario, and the @after hook is executed after the scenario has been executed.

Cucumber Automation Framework:

End 2 End Selenium Test: The first step in the Selenium Cucumber Framework journey is to select one End to End Scenario to automate and begin constructing framework components on top of that. We’ll teach you how to gather the various parts and components, as well as design and execute one End 2 End test of our Demo Application, in this article.

Convert Selenium Test to Cucumber: In this chapter, we will run a test using the Cucumber BDD Framework. However, in order to accomplish this, we must convert Selenium tests into Cucumber BDD Style tests. Cucumber understands Gherkin language, reads Feature files, and executes code defined in Step Definition files using Cucumber Options supplied in TestRunner.

Selenium Page Factory Page Object Pattern: This chapter will teach us more about the Page Object Model Framework, often known as the Page Object Design Pattern or Page Objects. The key benefit of Page Object Model is that if the UI or any HTML object for any page changes, the test does not need to be fixed. Only the code within the page objects will be affected, but this has no effect on the test.

Object Manager for Pages: This chapter is based on Page Objects once more. However, in this chapter, we will create a Page Object Manager. The Page Object Manager’s responsibility is to generate the page’s object while also ensuring that the same object is not created repeatedly. However, a single object should be used for all step definition files.

Config File Reader: This chapter explains how to read configurations from a property file in the Selenium Cucumber Framework or any other framework. It is risky to store hard coded variables in a project, and it also violates coding conventions. And we’ve been using a lot of hard coded data in our code thus far. With the help of the properties file, we will be eliminating these hard coded values one by one.

Manager of File Readers: The File Reader Manager as Singleton Design Pattern is discussed in this chapter. It is sometimes necessary to have only one instance of a class. These are accessed by various objects throughout a software system, necessitating a centralized point of access. In our situation, ConfigReaderFile should be accessible worldwide. But we’ll have a lot more file readers later on in our Selenium Cucumber Framework series. Therefore, a File Reader Manager is preferred above all other File Readers. Furthermore, it is preferable to classify the manager as a singleton.

WebDriver Administrator: Until now, we’ve created the driver within the Step file and specifically told our script to launch Chrome Driver. This means that if we need to switch from Chrome to Firefox later, we must modify it in each test script. That sounds like bhrrrhhhhhhh. Our test is also handling the logic of generating WebDriver. This chapter will concentrate on developing WebDriver Manager, which will handle driver creation for the test.

Sharing Test Context with PicoContainer: Scenario in Cucumber is a collection of stages that are carried out sequentially. Each step in the scenario may have a condition that is required by another step in the scenario. Cucumber supports multiple Dependency Injection (DI) Containers; it simply instructs a DI container to instantiate and wire up your step definition classes. PicoContainer is one of the DI containers that is supported, and it aids in the exchange of context between phases.

Hooks before and after: Hooks are blocks of code that run before or after each scenario in Cucumber. You can define them using the methods @Before and @After anywhere in your project or step definition layers. Cucumber Hooks enables us to better manage the code workflow and decrease code redundancy. We can call it an unseen phase that allows us to run our scenarios or tests.

Reader for JSON Data: The most basic definition of data-driven testing is that data from outside of your functional tests is loaded and used to enhance your automated test cases. You can change the test to accept variables and enter those variables into the data fields to ensure that the application works as expected. Data driven can be done using Excel, XML, JSON, and other formats, but in this series of Cucumber Automation Framework, we will pass data to tests using JSON.

Conclusion:

Cucumber is a freeware (open-source) testing tool used to do software code acceptance testing. It promotes Behavior-Driven Development in order to eliminate failures in the software testing process. A non-technical individual can use the cucumber tool to perform software testing. The Gherkin language used in the cucumber tool allows us to create test cases more quickly.

Are you looking training with Right Jobs?

Contact Us

Popular Courses