Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cucmber Material

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10
At a glance
Powered by AI
Some of the key takeaways from the document are that Cucumber supports data driven testing using Scenario Outlines and Examples. It also discusses different ways to parameterize tests using external files like Excel, JSON, XML. Tagged hooks in Cucumber allow performing different tasks before and after specific scenarios.

The different ways to parameterize tests in Cucumber discussed in the document are: Scenario Outline, external files like Excel, JSON, XML, and Data Tables. Scenario Outline uses the Example keyword while Data Tables can handle data without Examples.

The main differences between Scenario Outline and Data Tables in Cucumber are: Scenario Outline works for the whole test while Data Tables work for a single step. Scenario Outline uses the Example keyword to define test data while Data Tables do not use any keyword. Scenario Outline automatically runs the test for each data set while separate code is needed to handle Data Table data.

Cucmber:

Data Driven Testing in Cucumber:


Cucumber inherently supports Data Driven Testing using Scenario Outline. There are different
ways to use the data insertion with in the Cucumber and outside the Cucumber with external files.

Data Driven Testing in Cucumber


 Parameterization without Example Keyword
Data Driven Testing in Cucumber using Scenario Outline
 Parameterization with Example Keyword
 Parameterization using Tables
Data Driven Testing in Cucumber using External Files
 Parameterization using Excel Files
 Parameterization using Json
 Parameterization using XML

Scenario Outline – This is used to run the same scenario for 2 or more different set of test
data. E.g. In our scenario, if you want to register another user you can data drive the same scenario
twice.
Examples – All scenario outlines have to be followed with the Examples section. This contains the
data that has to be passed on to the scenario.
@When(“^User enters \”(.*)\” and \”(.*)\”$”)
The same can be achieved by using the below code as well:
@When(“^User enters \”([^\”]*)\” and \”([^\”]*)\”$”)
With the help of the above statements, Cucumber will understand that the associated Test_Step is
expecting some parameters.

Data Tables in Cucumber


DataTables are also used to handle large amount of data. They are quite powerful but not the most

intuitive as you either need to deal with a list of maps or a map of lists.

Difference between Scenario Outline & Data Table


Scenario Outline:
 This uses Example keyword to define the test data for the Scenario
 This works for the whole test
 Cucumber automatically run the complete test the number of times equal to the number of data in
the Test Set
Test Data:
 No keyword is used to define the test data
 This works only for the single step, below which it is defined
 A separate code is need to understand the test data and then it can be run single or multiple times
but again just for the single step, not for the complete test

Feature File:
We will pass the test data using data table and handle it with using Raw() method.
@When("^User enters Credentials to LogIn$")
public void user_enters_testuser__and_Test(DataTable usercredentials) throws Throwable {

//Write the code to handle Data Table


List<List<String>> data = usercredentials.raw();

//This is to get the first data of the set (First Row + First Column)
driver.findElement(By.id("log")).sendKeys(data.get(0).get(0));

//This is to get the first data of the set (First Row + Second Column)
driver.findElement(By.id("pwd")).sendKeys(data.get(0).get(1));

driver.findElement(By.id("login")).click();
}

Step Definition file @"C:\Users\acer\Desktop\Folders\Testing\Selenium\Selenium By Naveen Automation\Naveen


Source Code\CucumberSeleniumFramework-master\src\main\java\stepDefinitions\DealStepDefnition.java"

Maps in Data Tables


Maps in Data Tables can be used if different ways. Headers can also be defined for
the data tables. A same step can be executed multiple times with different set of test data
using Maps.
Maps in Data Tables with Multiple Test Data

In this test we will pass Username and Password two times to the test step. So our test should
enter Username & Password once, click on LogIn button and repeat the same steps again.

What are Cucumber Tags?


Let’s say you have got many different feature files which cover all the different functionality of the
application. Now there can be certain situation in the project where you like to execute just
a SmokeTests or End2EndTests or may be RegressionTests. One approach is that you start
creating new feature files with the name of the type
like SmokeTests.features or End2EndTests.feature and copy paste your existing tests in the
same. But this would make the project filthy and would require more maintenance in future. So how
to manage execution in such cases?
For this, Cucumber has already provided a way to organize your scenario execution by using tags in
feature file. We can define each scenario with a useful tag. Later, in the runner file, we can decide
which specific tag (and so as the scenario(s)) we want Cucumber to execute. Tag starts with “@”.
After “@” you can have any relevant text to define your tag like @SmokeTests just above the
scenarios you like to mark. Then to target these tagged scenarios just specify the tags names in
the CucumberOptions as tags = {“@SmokeTests”}.
Tagging not just specifically works with Scenarios, it also works with Features. Means you can also
tag your features files. Any tag that exists on a Feature will be inherited by Scenario,
Scenario Outline or Examples.
Things to Note:
 Few scenarios are part of Smoke Test, Regression Test and End2End Test.
 Few scenarios are part of two or more Test Types. For example the first test is considered as
Smoke as well as Regression.
 Few scenarios are not at all tagged
 Last scenario of Payment Declined, it is a single scenario but has five different test data. So this
will be considered as five different scenarios.
Logically ANDing and ORing Tags
Requirements are complicated, it will not always simple like executing a single tag. It can be
complicated like executing scenarios which are tagged either as @SmokeTest or @RegressionTest. It
can also be like executing scenarios which are tagged both as @SmokeTest and @RegressionTest.
Cucumber tagging gives us the capability to choose what we want with the help
of ANDing and ORing.

Execute all tests tagged as @SmokeTest OR @RegressionTest


Tags which are comma separated are ORed.

Execute all tests tagged as @SmokeTest AND @RegressionTest


Tags which are passed in separate quotes are ANDed

How to Ignore Cucumber Tests


This is again a good feature of Cucumber Tags that you can even skip tests in the group execution.
Special Character ~ is used to skip the tags. This also works both for Scenarios and Features. And
this can also works in conjunction with AND or OR.
Execute all tests of the feature tagged as @FunctionalTests but skip scenarios tagged as
@SmokeTest

What are Hooks in Cucumber?


Cucumber supports hooks, which are blocks of code that run before or aftereach scenario. You can
define them anywhere in your project or step definition layers, using the
methods @Before and @After. Cucumber Hooks allows us to better manage the code workflow
and helps us to reduce the code redundancy. We can say that it is an unseen step, which allows us to
perform our scenarios or tests.

Why Cucumber Hooks?


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
Annotaions, 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.

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;

Tagged Hook in Cucumber.


Now we know that if we need to do anything before of after the test, we can use @Before & @After
hooks. But this scenario works till the time our prerequisites are same for all the scenarios. For
example till the time prerequisite for any test is to start the browser, hooks can solve our purpose.
But what if we have different perquisites for different scenarios. And we need to have different hooks
for different scenarios.
Again, Cucumbers has given feature of Tagged Hooks to solve the above situation where we need to
perform different tasks before and after scenarios.
Tagged Hooks in Cucumber
Lets again start with doing an simple exercise to get the concept straight. Just keep three different
scenarios in the feature file with the same Given, When & Then steps.
1)-First step is to annotate required scenarios using @ + AnyName at the top of the Scenario. For
this example, i just annotate each scenario with the sequence order of it, like @First, @Second &
@Third.

2) Create a Step definition file and just print the execution order of the steps in the console.

Hooks can be used like @Before(“@TagName”). Create before and after hooks for every scenario.
I have also added normal before and after hooks,
Note: We learned that @Before & @After hooks runs before & after every Scenario.
But @Before(“@First”) will run only before the first scenario and like wise other tagged hooks.
Again, these tags names can be anything and no need to be first, second and third.

4) Run the feature file and observe the output.

Common Tagged Hooks for Multiple Scenarios


We can have common tagged hooks for multiple scenarios as well. In the below example, I just
combined the @Before(“First”) and @Before(“Third”) by @Before(“@First, @Third”). So in this
way we do not need have two different hooks logic.
Execution Order of Hooks
Order hooks to run in a particular sequence is easy to do. As we already know the way to specify
hooks in cucumber like putting an annotation just above the scenario. Ordering also works the same
way but the only difference is that it required an extra parameter. This extra parameter decides the
order of execution of the certain hook.
For example @Before, and if you want to specify the order it will become @Before(value = 1).

How to set the Order or Priority of Cucumber Hooks?


The very important thing to note here is:
 @Before(order = int) : This runs in increment order, means value 0 would run first and 1 would
be after 0.
 @After(order = int) : This runs in decrements order, means apposite of @Before. Value 1 would
run first and 0 would be after 1.
So, as per the logic above the Hooks file will looks like below.

You might also like