Академический Документы
Профессиональный Документы
Культура Документы
MOHAN SEERANGAN
25/04/18
Contents
Cucumber Introduction:..................................................................................................................................... 4
Benefits of TDD .................................................................................................................................................. 4
Drawbacks of TDD .............................................................................................................................................. 4
Best BDD Tools and Testing Frameworks .......................................................................................................... 5
Features of Cucumber........................................................................................................................................ 5
Cucumber KEYWORDS ....................................................................................................................................... 6
Cucumber Design ............................................................................................................................................... 6
What is "Feature File"? ...................................................................................................................................... 6
What is "Step Definition" ................................................................................................................................... 6
Cucumber - Annotations .................................................................................................................................... 7
Create cucumber feature file ............................................................................................................................. 7
Steps to Configure Cucumber Eclipse Plugin ................................................................................................... 10
Structure that will be followed for Cucumber ................................................................................................. 12
Some important points about Cucumber feature file ..................................................................................... 12
What’s next? .................................................................................................................................................... 13
Create Cucumber Test Runner class ................................................................................................................ 13
What is Cucumber Test Runner Class? ............................................................................................................ 13
Add Cucumber Test Runner class to Eclipse project ........................................................................................ 13
Add code to test runner class .......................................................................................................................... 14
Understanding the annotations in the test runner class ................................................................................. 14
How to create Cucumber Step Definition class ............................................................................................... 15
What is Cucumber step definition class? ......................................................................................................... 15
Identify the step definition methods to be added........................................................................................... 15
Create new step definition class ...................................................................................................................... 16
Add step definition methods to the class ........................................................................................................ 16
Run cucumber test runner class once again .................................................................................................... 18
Add Selenium code in Step Definition class ..................................................................................................... 19
Which approach to be used to write selenium code? ..................................................................................... 19
Create new java class to add Cucumber Selenium code ................................................................................. 19
What are Hooks in Cucumber? ........................................................................................................................ 20
Why Cucumber Hooks?.................................................................................................................................... 20
Cucumber Introduction:
A cucumber is a tool based on Behavior Driven Development (BDD) framework which is used to
write acceptance tests for the web application. It allows automation of functional validation in easily
readable and understandable format (like plain English) to Business Analysts, Developers, Testers, etc.
Cucumber feature files can serve as a good document for all. There are many other tools like JBehave which
also support BDD framework. Initially, Cucumber was implemented in Ruby and then extended to Java
framework. Both the tools support native JUnit.(it only support JUnit and it doesn't support TestNG)
Behavior Driven Development is an extension of Test Driven Development and it is used to test the system
rather than testing the particular piece of code. We will discuss more the BDD and style of writing BDD
tests.
Cucumber can be used along with Selenium, Watir, and Capybara etc. Cucumber supports many other
languages like Perl, PHP, Python, Net etc. In this tutorial, we will concentrate on Cucumber with Java as a
language.
The concept is to “get something working now and perfect it later.” After each test, refactoring is done and
then the same or a similar test is performed again. The process is iterated as many times as necessary until
each unit is functioning according to the desired specifications.
Benefits of TDD
Unit test proves that the code actually works
Can drive the design of the program
Refactoring allow to improve the design of the code
Low Level regression test suite
Test first reduce the cost of the bugs
Drawbacks of TDD
Developer can consider it as a waste of time
Features of Cucumber
1. First point and the most important is the price. And, yes! It is free to use
2. Cucumber helps in writing the tests which are easy to understand by anyone irrespective of the
Technical knowledge they possess
3. The stakeholders, Business Owners, Testers and Developers work on the problem statement to
derive the best behavior set
4. The behavior sets are then changed into Acceptance Test conditions using Cucumber
5. Cucumber as a tool maintains both the requirement statement and the Test conditions in the same
place
6. The tracking of the implementation is easy using Cucumber. The behavior set that is implemented
and working are displayed as working in Cucumber. The ones those are not are displayed
accordingly
7. Cucumber typically uses Ruby as the programming language. However, other languages such as
JAVA, Groovy etc. can also be used
8. Both the Testing team and development team are involved in writing/developing test conditions
9. Just like Selenium, Cucumber is restricted only for Web-based automation
Cucumber KEYWORDS
Feature
Background Then Scenario Outline
Scenario And Examples
Given But
When *
Cucumber Design
Feature: A feature would describe the current test script which has to be executed.
Scenario: Scenario describes the steps and expected outcome for a particular test case.
Scenario Outline: Same scenario can be executed for multiple sets of data using scenario outline.
The data is provided by a tabular structure separated by (I I).
Background: Often times when writing multiple scenarios within a singular feature file you see repeated test
steps. Initial test steps that are common across all scenarios and scenario outlines can be pulled out into a
Background test step. These steps get executed before every scenario. This can greatly reduce the number of
test steps in the feature file, and increase readability.
Given: It specifies the context of the text to be executed. By using data tables "Given", step can
also be parameterized. (Pre-Condition)
When: "When" specifies the test action that has to performed (Test Condition)
Then & But: The expected outcome of the test can be represented by "Then"(Expected Results)
When −It defines the trigger point for any test scenario execution.
Example − WHEN I enter "<username>"
Then −Then holds the expected result for the test to be executed.
Example − THEN login should be successful.
And −It provides the logical AND condition between any two statements. AND can be used in
conjunction with GIVEN, WHEN and THEN statement.
Example − WHEN I enter my "<username>" AND I enter my "<password>"
But −It signifies logical OR condition between any two statements. OR can be used in conjunction
with GIVEN, WHEN and THEN statement.
Example − THEN login should be successful. BUT home page should not be missing.
Scenario −Details about the scenario under the test needs to be captured after the keyword
“Scenario:”
Example −Scenario:
GIVEN I am a Facebook user
WHEN I enter my
AND I enter my
THEN login should be successful.
BUT home page should not be missing.
Scenario Outline − (To be covered later)
Examples − (To be covered later)
Background −Background generally has the instruction on what to setup before each scenario runs.
However, it gets executed after “Before” hook (to be covered later). So this is ideal to be used for
code when we want to set up the web-browser or we want to establish the database connectivity.
Example −Background:
Go to Face book home page.
3. You can now see that resources folder is added to your eclipse project. Now, right click
on resources folder and select New > Package
4. Enter package name as features and click on Finish button. This is the package inside which you will
add cucumber feature files
6. From the popup window, expand General folder and then select File option. Then click on Next
button
You can now see that the cucumber feature file is added to the features package.
1. Launch the Eclipse IDE and from Help menu, click "Eclipse Marketplace" and type Cucumber
natural and finally search it
Apart from this, you can also change the order of the keywords. For example, you can use ‘and’ or ‘then’
before ‘given’ also. The only reason we use all given, when, then keywords is due to readability. And the
readability of a scenario is much better when you use these keywords in proper order.
What’s next?
You have now successfully created a feature file in your eclipse project. Cucumber understands
that you have provided a feature file, but it doesn’t know what to do with it. Cucumber won’t be able to
run this feature file unless you provide a runner class. We will work on creating a test runner class and link
it to this feature file. This will enable you to run the cucumber feature file.
The complete code After you add the code, the test runner class should look something like this –
Before proceeding further, let us have a quick look at the annotations that you have used in the test runner
class. For cucumber test runner class (including the annotations) is given below. You can copy paste the
entire code to your runner class.
Below this message, cucumber lists down all the step definition methods that you have to implement.
4. Add System.out.println() method in each step definition method. The complete code will look like
this
Let us now re-run the test runner class and check that the step definition methods that you have added
work fine.
For better readability, you can split this in multiple lines as well The test runner class should look as shown
in the below image
We will end the article at this point, as you have completed creating a basic cucumber step definition
class. Even though this class is kind of empty, as it just prints out some statements on the console. But from
cucumber setup point of view, this is a big step.
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.
Output
Note: Scenario Hooks execute before and after every scenario. In the above example, executed two times
for two scenarios.
Note: Again, in cucumber every example is considered as a separate scenario. So the output is same as the
second example above.
Exercise -1
Single scenario in feature file with single test definition:
Bussiness_Scenarios_01.feature
Feature: Example Scenarios
# Scenario-1
Scenario: Login for leaftaps positive
a. TestRunner_01.java
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/main/java/feature/Bussiness_Scenarios_01.feature",
glue = "cucumber_TestCases_01")
public class TestRunner_01 {
b. CucumberTestCase_01.java
package cucumber_TestCases_01;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
@And("set timeout_sc1")
public void Timeout() {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
Exercise -2
Multiple scenarios in one feature file with test definitions (with background for common steps):
a. Bussiness_Scenarios_02.feature
Feature: Example Scenarios
# Scenario-4
Background: User is login leaftaps
Given Open the browser_sc4
And Launch the url_sc4
And set timeout_sc4
And Maximize the browser_sc4
b. TestRunner_04.java
package cucumber_TestCases_04;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
c. CucumberTestCase_04.java
package cucumber_TestCases_04;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
@And("set timeout_sc4")
public void Timeout() {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
Exercise -3
Command line in feature file:
a. Bussiness_Scenarios_03.feature
Feature: Example Scenarios
# Scenario-5
# Given Open the browser_sc1
# And Launch the url_sc1
# And set timeout_sc1
# And Maximize the browser_sc1
b. TestRunner_05.java
package cucumber_TestCases_05;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/main/java/feature/Bussiness_Scenarios_03.feature",
glue = "cucumber_TestCases_05")
public class TestRunner_05 {
c. CucumberTestCase_05.java
package cucumber_TestCases_05;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
@Before
public void beforeScenario() {
System.out.println("This will run before the Scenario");
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://leaftaps.com/opentaps/control/main");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@After
public void afterScenario() {
System.out.println("This will run after the Scenario");
driver.quit();
}
Exercise -4
Single scenarios with same steps repeated in feature file with different test data (one step definition):
a. Bussiness_Scenarios_04.feature
Feature: Example Scenarios
# Scenario-6
Scenario: Login for leaftaps01
And Enter User Name as DemoSalesManager
And Enter Password as crmsfa
When Click on Login button
Then Verify login is sucessful
b. TestRunner_06.java
package cucumber_TestCases_06;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/main/java/feature/Bussiness_Scenarios_04.feature",
glue = "cucumber_TestCases_06")
public class TestRunner_06 {
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
@Before
public void beforeScenario(Scenario sc) throws Exception{
System.out.println("Starting executig scenario " + sc.getName());
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://leaftaps.com/opentaps/control/main");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@After
public void afterScenario(Scenario sc) {
System.out.println("Completing scenario " + sc.getName());
Exercise -5
Multiple Test data provided in feature file (with examples):
a. Bussiness_Scenarios_05.feature
Feature: Example Scenarios
# Scenario-7
Scenario: Login for leaftaps01
And Enter User Name_sc7 as <uName> #added variable name and mention
examples multiple data
And Enter Password_sc7 as <password>
When Click on Login button_sc7
Then Verify login is sucessful_sc7
Examples:
|uName|password|
|DemoSalesManager|crmsfa|
|DemoCSR|crmsfa|
b. TestRunner_07.java
package cucumber_TestCases_07;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/main/java/feature/Bussiness_Scenarios_05.feature",
glue = "cucumber_TestCases_07")
public class TestRunner_07 {
}
c. CucumberTestCase_07.java
package cucumber_TestCases_07;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
@Before
public void beforeScenario(Scenario sc) throws Exception{
System.out.println("Starting executig scenario " + sc.getName());
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://leaftaps.com/opentaps/control/main");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@After
public void afterScenario(Scenario sc) {
System.out.println("Completing scenario " + sc.getName());
Exercise -6
Groping in feature and step definition files with different test data:
a. Bussiness_Scenarios_06.feature
Feature: Example Scenarios
@Regression
Scenario Outline: Login for leaftaps02
And Enter User Name_sc8 as <uName> #added variable name and mention
examples multiple data
And Enter Password_sc8 as <password>
When Click on Login button_sc8
Then Verify login is sucessful_sc8
Examples:
|uName|password|
|DemoSalesManager|crmsfa|
|DemoCSR|crmsfa|
b. TestRunner_08.java
package cucumber_TestCases_08;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions( features =
"src/main/java/feature/Bussiness_Scenarios_06.feature",
glue = "cucumber_TestCases_08",
tags= {"@Smoke"}
) //Grouping with Feature file Examples- @Sanity,
@Smoke, @Regression etc.
public class TestRunner_08 {
c. CucumberTestCase_08.java
package cucumber_TestCases_08;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
@Before
public void beforeScenario(Scenario sc) throws Exception{
System.out.println("Starting executig scenario " + sc.getName());
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://leaftaps.com/opentaps/control/main");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@After
public void afterScenario(Scenario sc) {
System.out.println("Completing scenario " + sc.getName());
Exercise -7
Multiple Grouping in single scenario in feature file with step definition: (Tags: any one condition execute)
a. Bussiness_Scenarios_07.feature
Feature: Example Scenarios
# Scenario-9
@Smoke @Regression
Scenario: Login for leaftaps01
@Sanity @Regression
Scenario Outline: Login for leaftaps02
And Enter User Name_sc9 as <uName> #added variable name and mention
examples multiple data
And Enter Password_sc9 as <password>
When Click on Login button_sc9
Then Verify login is sucessful_sc9
Examples:
|uName|password|
|DemoSalesManager|crmsfa|
|DemoCSR|crmsfa|
b. TestRunner_09.java
package cucumber_TestCases_09;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions( features =
"src/main/java/feature/Bussiness_Scenarios_07.feature",
glue = "cucumber_TestCases_09",
tags= {"@Smoke, @Regression"}, monochrome=true
) //Any one of the Condition is meet and it will
execute
public class TestRunner_09 {
c. CucumberTestCase_09.java
package cucumber_TestCases_09;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://leaftaps.com/opentaps/control/main");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@After
public void afterScenario(Scenario sc) {
System.out.println("Completing scenario " + sc.getName());
Exercise -8
Multiple Grouping in single scenario in feature file with step definition: (Tags: common group to execute)
a. Bussiness_Scenarios_08.feature
Feature: Example Scenarios
# Scenario-10
@Smoke @Regression
Scenario: Login for leaftaps01
@Sanity @Regression
Scenario Outline: Login for leaftaps02
And Enter User Name_sc10 as <uName> #added variable name and mention
examples multiple data
And Enter Password_sc10 as <password>
When Click on Login button_sc10
Then Verify login is sucessful_sc10
Examples:
|uName|password|
|DemoSalesManager|crmsfa|
|DemoCSR|crmsfa|
b. TestRunner_10.java
package cucumber_TestCases_10;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions( features =
"src/main/java/feature/Bussiness_Scenarios_08.feature",
glue = "cucumber_TestCases_10",
tags= {"@Smoke", "@Regression"},
monochrome=true
) //Both Conditions are meet and it will execute
public class TestRunner_10 {
c. CucumberTestCase_10.java
package cucumber_TestCases_10;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
@Before
public void beforeScenario(Scenario sc) throws Exception{
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://leaftaps.com/opentaps/control/main");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@After
public void afterScenario(Scenario sc) {
System.out.println("Completing scenario " + sc.getName());
Exercise -9
Multiple Grouping in single scenario in feature file with step definition: (Tags : ~@S)
a. Bussiness_Scenarios_09.feature
Feature: Example Scenarios
# Scenario-10
@Smoke @Regression
Scenario: Login for leaftaps01
@Sanity @Regression
And Enter User Name_sc11 as <uName> #added variable name and mention
examples multiple data
And Enter Password_sc11 as <password>
When Click on Login button_sc11
Then Verify login is sucessful_sc11
Examples:
|uName|password|
|DemoSalesManager|crmsfa|
|DemoCSR|crmsfa|
b. TestRunner_11.java
package cucumber_TestCases_11;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions( features =
"src/main/java/feature/Bussiness_Scenarios_08.feature",
glue = "cucumber_TestCases_10",
tags= {"~@S.*", "@Regression"}, monochrome=true
) // Start from 'S' groups and checks conditions
and it will execute
public class TestRunner_11 {
c. CucumberTestCase_11.java
package cucumber_TestCases_11;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeDriver;
@Before
public void beforeScenario(Scenario sc) throws Exception{
System.out.println("Starting executig scenario " + sc.getName());
System.setProperty("webdriver.chrome.driver",
"./drivers/chromedriver.exe");
@After
public void afterScenario(Scenario sc) {
System.out.println("Completing scenario " + sc.getName());