Cucumber

Here I have mentioned 3 types of reports

Method 01 :

Add below line into your TestRunner, java file

@CucumberOptions(

..........
plugin = {"html:taregt/test-report"}

.........

)

It will generate index.html file in the mentioned location

Right-click on index.html file and open with -> system editor

It will open with browser as below


Green colour steps are passed and failed one is displayed with yellow colour.

Method 02 :

Add below line into your TestRunner, java file

@CucumberOptions(

..........
plugin = {"junit:target/junit-xml-report.xml"}

.........

)

Run test with JUnit testing method.

Below file will be created





Result open with -> JUnit viewer





Method 03 :

Add below line into your TestRunner, java file

@CucumberOptions(

..........
plugin = {"pretty:target/json-report.json"}

.........

)

Run test with TestNG

Below file will be created json-report.json


Open with JSON viewer

Cucumber QA Pulse

1. What is cucumber?

  1. Cucumber is a testing framework which supports the behavior-driven approach.
  2. Cucumber is a software testing tool that allows users to write test cases that are easy to understand
  3. It’s important because it supports BDD, which allows a variety of professionals, such as quality assurance specialists, software developers, customer support teams and business analysts, to understand software applications without needing extensive technical knowledge
  4. It helps testers and developers evaluate the functionality of different software programs and automate tests that customers can understand
  5. This helps reduce the total testing time, allowing companies to release software to customers and determine and fix any issues with it much more quickly

2. What is a feature file?

It includes scenarios which can be executed and written in a gherkin language.

Feature: login to the bank system
Scenario: Successfully login
Given I am in the bank login page
When I enter username and password
And I click on Submit button
Then I can login successfully

3. What is the background keyword in cucumber?

It is used to define a set of steps common in all scenarios. The purpose is to redundancy.

Feature: Login functionality

Background:
Given the user is on the login page
And the user has entered a valid username
And the user has entered a valid password

Scenario: Successful login
When the user clicks the login button
Then the user should be redirected to the dashboard

Scenario: Failed login
When the user clicks the login button
Then an error message should be displayed

4. What is the step definition?

It is the implementation of scenarios given in the feature file. it is correct step definition matching with corresponding each step in the feature file

public class MyStepdefs {
@Given("I am in the bank login page")
public void iAmInTheBankLoginPage() {
// Code to navigate to the bank login page
System.out.println("Navigating to the bank login page");
}

@When("I enter username and password")
public void iEnterUsernameAndPassword() {
// Code to enter username and password
System.out.println("Entering username and password");
}

@And("I click on Submit button")
public void iClickOnSubmitButton() {
// Code to click on the Submit button
System.out.println("Clicking on Submit button");
}

5. How do you run only specific test cases in Cucumber?

Use tags in the feature file. Ex: @smoke tag on top of the relevant feature.


@smoke
@regression
Scenario: Successfully login
Given I am in the bank login page
When I enter username and password
And I click on Submit button
Then I can login successfully

6. How to achieve data-driven testing in cucumber?

Use scenario outlines in feature files

Scenario Outline: Successfully login
Given I am in the bank login page
When I enter <username> and <password>
And I click on Submit button
Then I can login successfully

Examples:
|username|password|
|victor |abc123 |
|kiyots |gbc234 |

7. What is the purpose of cucumber options annotation in runner class?

It is used to define different configurations such as different feature file locations, step definition locations different tags to use etc…

8. What are the keywords used in Cucumber

  1. Feature
  2. Scenario
  3. Scenario Outline
  4. Given
  5. When
  6. Then
  7. But
  8. And

9. How to group scripts in cucumber?

  1. Use tags inside the .feature file
  2. @smoke , @regression,@functional
  3. Can use these tags before the Feature keyword
  4. Can use these tags before each scenario

10. What is included in the TestRunner class in Cucumber?

@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = "com.example.stepdefinitions",
tags = "@smoke",
plugin = {"pretty", "html:target/cucumber-reports"}
)

public class TestRunner {
// This class doesn't need to have any code in it.
// It serves as the entry point for Cucumber test execution.
}

11. Describe the Gherkin syntax and the principle of BDD

  1. The Gherkin syntax is a language that allows you to describe behaviour without being overly technical
  2. Most of the lines within a Gherkin document begin with a keyword, which you can follow up with your desired text
  3. Common keywords include ‘Feature,’ ‘Rule,’ ‘Example,’ ‘Background,’ ‘Scenario Outline,’ and ‘Examples.’
  4. BDD is an Agile software development methodology that involves creating software based on the unique requirements of its users
  5. It also allows for cross-functional collaboration among software developers, testers and professionals in positions that have more direct interaction with customers

12. Explain what some of the keywords mean in Cucumber

  1. Many keywords in Cucumber have different meanings and functionalities. 
  2. The purpose of ‘Feature’ is to provide a thorough description of a software feature. 
  3. ‘Scenario Outline’ allows you to test a scenario multiple times. 
  4. This can be especially useful because it allows you to consider all different types of users when developing a sign-in feature. 
  5. ‘Background’ enables you to define a single step or set of steps that are the same for all software tests.
  6.  It executes the steps before performing the actual tests

13. Describe some of the primary benefits of using Cucumber

  1. From my own experience working with Cucumber, I’ve discovered that it supports a variety of programming languages, which can be especially useful when collaborating with different developers.
  2.  Another major benefit is that it functions as an end-to-end testing framework, which means that it enables all different types of tests.
  3.  It also makes it easy to reuse code due to its simplistic architecture, which helps to reduce costs and make developing different applications quicker and easier

14. List the files and annotations required in the Cucumber framework

  1. There are five primary annotations in Cucumber.
  2.  ‘Given’ determines the test’s specifications, 
  3. ‘When’ establishes the test’s starting point,
  4.  ‘Then’ specifies the preferred result of the test,
  5.  ‘And’ provides the logical condition for the test
  6.  ‘But’ outlines the relationship between the two available propositions.
  7.  In Cucumber, a feature file contains the descriptions of your tests and stores your test cases,
  8.  step definition file translates the test case steps from the feature files into code.
  9.  A test runner file enables you to execute multiple tests at once and helps connect the feature and step definition files.

15. How does the BDD process work?

  1. The BDD process includes three primary phases. 
  2. First is the discovery phase, which involves the creation of acceptance criteria.
  3.  This phase is often a collaborative process that involves professionals from different backgrounds working together to determine additional criteria to include.
  4.  The next step in the process is the formulation phase, which involves creating the acceptance tests.
  5.  The initial acceptance criteria from the discovery tend to be simplistic and vague, and this phase helps to resolve any ambiguities and develop more detailed criteria. 
  6. Finally, the automation phase automates the acceptance tests, enabling them to occur continuously

16. How do you use the Options tag in Cucumber?

  1. The Options tag helps create a link between the feature files and the step definition files, with each feature file connecting to a corresponding step definition file.
  2.  In my previous role as a developer for a software startup, I tested a variety of projects with the help of Options.
  3.  My team often used Options to set a feature file option and a step definition file option. 
  4. The two parameters were features and glue, with the feature option specifying the path to the feature file and glue providing the location of the step definition file

17. Difference between “But” and “And” keywords

  1. The And keyword is used to continue the same type of action or verification as the preceding step.
  2. It’s often used when you want to chain multiple steps of the same type without explicitly specifying the type again.
  3. For example, if you have a sequence of Given, When, or Then steps, you can use And to add more steps of the same type. in cucumber
  4. The But the keyword is used when you want to introduce an exception or contrast to the behaviour defined by the preceding steps.
  5. It’s often used to indicate an unexpected or contrasting outcome compared to the expected behaviour.
  6. It is similar to And but is used when you want to emphasize a change or exception in the flow
Scenario: Example using But
Given the user is on the login page
When the user enters an invalid username
And the user enters a valid password
And the user clicks the login button
Then an error message should be displayed
But the user should not be redirected to the dashboard

18. Explain the “Feature” keyword in Gherkin

  1. It provides a high-level description of software functionality
  2. It also groups related scenarios
  3. Need to use Colon(:) after the word Feature:

19. Explain the “Rule” Keyword in Gherkin

  1. It represents one business rule that should be implemented
  2. It provides additional information on the feature
  3. It is used to group a few scenarios that belong to the same business rule
  4. The rule should contain one or more scenarios that the illustrate particular rule
Feature: Overdue tasks
Let users know when tasks are overdue, even when using other
features of the app

Rule: Users are notified about overdue tasks on first use of the day
Background:
Given I have overdue tasks

20. Explain the “Given” keyword in gherkin

  1. It describes the initial context
  2. It is typically something that happened in the past
  3. The purpose of the Given step is to put the system in a known state
  4. If we create use cases this will be a Precondition
  5. It is okay to have several Given steps (Use And or But keywords)
Scenario: Example using But
Given the user is on the login page

Scenario: Verify page loading
Given Micky and Anne started a game

Scenario: Verify login button
Given I am logged in the Amazon webpage

Scenario: Verify success login
Given Joe has a balance of Euro 45

21. Explain the “When” keyword in gherkin

  1. It describes an event 
  2. It can be an action
  3. This can be a person interacting with a system
  4. There can be more than one “When” step in a scenario using And & But keywords
Scenario: Example using But
Given the user is on the login page
When I enter username and password

Scenario: Verify page loading
Given Micky and Anne started a game
When Micky guess a word

Scenario: Verify login button
Given I am logged in the Amazon webpage
When I select a phone

Scenario: Verify success login
Given Joe has a balance of Euro 45
When Joe buy a mango

22. Explain the “Then” keyword in gherkin

  1. It describes the expected outcome
  2. It uses an assertion to compare the actual result
  3. There can be more than one Then step in a scenario using And & But keywords
Scenario: Example using But
Given the user is on the login page
When I enter username and password
Then Successfully logged in message appears

Scenario: Verify page loading
Given Micky and Anne started a game
When Micky guess a word
Then Guessed the correct word

Scenario: Verify login button
Given I am logged in the Amazon webpage
When I select a phone
Then Message appears and asked to add the cart

Scenario: Verify success login
Given Joe has a balance of Euro 45
When Joe buy a mango
Then Mango is Euro 45

23. Explain “And” and “But” keywords

  1. It can be used when we have more than one Given or Then
Scenario: Example using And and But
Given One thing
And another thing
When I open my eyes
Then I should see something
But I should not see this thing

24. Explain the “Scenario Outline” keyword

  1. This is used to run the same scenario multiple times
  2. With different combinations of values
Scenario Outline: Eating
Given I started eating <start number> of eggs
When I ate <completed number> of eggs
Then I can see <remaining number> of eggs are remained

Examples:
|start number|completed number|remaining number|
|10|4|6|
|23|3|20|

25. Explain data tables

  1. When we run the script with multiple sets of data
  2. Data tables can be used for any clause not only in the Given clause
  3. The DataTable object contains the tabular data from the data table we defined in our scenario as well as methods for transforming this data into usable information. 
  4. Generally, there are three ways to transform a data table in Cucumber:
  5.  a list of lists
  6. a list of maps 
  7. a table transformer.
Scenario: Correct non-zero number of books found by author
Given I have the following books in the store
| The Devil in the White City | Erik Larson |
| The Lion, the Witch and the Wardrobe | C.S. Lewis |
| In the Garden of Beasts | Erik Larson |
When I search for books by author Erik Larson
Then I find 2 books

https://javadoc.io/doc/io.cucumber/datatable/4.0.0/io/cucumber/datatable/DataTable.html

26. List of Lists


@Given("^I have the following books in the store by list$")
public void haveBooksInTheStoreByList(DataTable table) {

List<List<String>> rows = table.asLists(String.class);

for (List<String> columns : rows) {
store.addBook(new Book(columns.get(0), columns.get(1)));
}
}

27. List of Maps


[
{"title": "The Devil in the White City", "author": "Erik Larson"},
{"title": "The Lion, the Witch and the Wardrobe", "author": "C.S. Lewis"},
{"title": "In the Garden of Beasts", "author": "Erik Larson"}
]
@Given("^I have the following books in the store by map$")
public void haveBooksInTheStoreByMap(DataTable table) {

List<Map<String, String>> rows = table.asMaps(String.class, String.class);

for (Map<String, String> columns : rows) {
store.addBook(new Book(columns.get("title"), columns.get("author")));
}
}

28. Create a step definition file with multiple sets of data

public class stepDef {

@Given("The user is on the search page")
public static void theUserIsOnTheSearchPage(){
System.out.println("Hi");
}

@When("User search {product}")
public static void userSearchProduct(String product){
System.out.println("Product is " + product);
}

@Then("Search result should contain the products")
public static void searchResultShouldContainProducts(){
System.out.println("Products");
}

29. Where we keep our runner class?

src/test/java/runners: This directory is often used to store Cucumber test runner classes.


Comments