cucumber tutorial

32
Introduction to Cucumber Before we learn about Cucumber , lets understand BDD - What is Behaviors Driven Development? Consider you are assigned to create Funds Transfer module in a Net Banking application. There are multiple ways to test it 1. Fund Transfer should take place if there is enough balance in source account 2. Fund Transfer should take place if the destination a/c details are correct 3. Fund Transfer should take place if transaction password / rsa code / security authentication for the transaction entered by user us correct 4. Fund Transfer should take place even if it's a Bank Holiday 5. Fund Transfer should take place on a future date as set by the account holder The test scenario become more elaborate and complex as we consider additional features like transfer amount X for an interval Y days/months , stop schedule transfer when the total amount reaches Z , and so on The general tendency of developers is to develop features and write test code later. As, evident in above case, test case development for this case is complex and developer will put off testing till release , at which point he will do quick but ineffective testing. To overcome this issue (Behavior Driven Development) BDD was conceived. It makes the entire testing process easy for a developer In BDD, whatever you write must go into Given-When-Then steps. Lets consider the same example above in BDD Given that a fund transfer module in net banking application has been developed And I am accessing it with proper authentication

Upload: saddam-hussain

Post on 12-Jul-2016

48 views

Category:

Documents


1 download

DESCRIPTION

Cucumber tutorial

TRANSCRIPT

Page 1: Cucumber Tutorial

Introduction to CucumberBefore we learn about Cucumber , lets understand BDD -

What is Behaviors Driven Development?

Consider you are assigned to create Funds Transfer module in a Net Banking application.

There are multiple ways to test it

1. Fund Transfer should take place if there is enough balance in source account2. Fund Transfer should take place if the destination a/c details are correct3. Fund Transfer should take place if transaction password / rsa code / security

authentication for the transaction entered by user us correct4. Fund Transfer should take place even if it's a Bank Holiday5. Fund Transfer should take place on a future date as set by the account holder

The test scenario become more elaborate and complex as we consider additional features like transfer amount X for an interval Y days/months , stop schedule transfer when the total amount reaches Z , and so on

The general tendency of developers is to develop features and write test code later. As, evident in above case, test case development for this case is complex and developer will put off testing till release , at which point he will do quick but ineffective testing.

To overcome this issue (Behavior Driven Development) BDD was conceived. It makes the entire testing process easy for a developer

In BDD, whatever you write must go into Given-When-Then steps. Lets consider the same example above in BDD

Given that a fund transfer module in net banking application has been developed And I am accessing it with proper authentication

When I shall transfer with enough balance in my source accountOr I shall transfer on a Bank HolidayOr I shall transfer on a future dateAnd destination a/c details are correctAnd transaction password/rsa code / security authentication for the transaction is correctAnd press or click send button

Page 2: Cucumber Tutorial

Then amount must be transferredAnd the event will be logged in log file

Isn't it easy to write and read and understand? It covers all possible test cases for the fund transfer module and can be easily modified to accommodate more. Also, it more like writing documentation for the fund transfer module.

What is Cucumber?

Cucumber tools also support Behavior Driven Development (BDD).It offers a way to write tests that anybody can understand, regardless of their technical knowledge.

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

Cucumber use Ruby programming language.

What are the benefits?

1. It is helpful to involve business stakeholders who can't easily read code2. Cucumber focuses on end-user experience3. Style of writing tests allow for easier reuse of code in the tests4. Quick and easy set up and execution5. Efficient tool for testing

Advantages of Cucumber over other tools?

Cucumber HP ALM (QTP) Selenium

It is free QTP is expensive It is free

Page 3: Cucumber Tutorial

It's a behavior driven development tool

It's a Functional Automation Tool

It's a Functional and Performance ( Selenium Grid) test tool

Plugin in cucumber works faster Plugin are slower compare to Cucumber and Selenium

Plugins are slower than cucumber

Cucumber supports other language as well beyond Ruby like Java, Scala, Groovy etc.

QTP supports only VB script

Selenium supports Java, .Net and many other languages

Writing automation steps are joint effort of testers and developer

In QTP only tester writes automation steps

Like Cucumber, writing automation steps are joint effort of testers and developer

Cucumber supports only web environment

Support web, desktop and any client server application

Supports only web environment

Cucumber InstallationCucumber installation could be tiresome but its relatively easy.

Here is a roadmap of components that need to be installed to make Cucumber work

Page 4: Cucumber Tutorial

PART A) Install Ruby:Step 1) Got to http://rubyinstaller.org/downloads/

Select a package as per your OS Version

If you have 32bit version OS than click on Ruby2.0.0-p451 or have a 64bit OS then click on Ruby2.0.0-p451(x64)

Once download is complete, start the installation. Select Your Preferred language For Installation

Step 2) Accept license and click on next

Page 6: Cucumber Tutorial

Step 5) Once installation is complete After install ruby Lets Run Ruby!

Step 6) You will see Ruby Command prompt same as windows cmd.

Part B Install Ruby Development KitStep 1) Download Development kit here . Development kit should be same version as that of Ruby you downloaded.

Page 7: Cucumber Tutorial

Step 2) The development kit is in .7zip format file and when you execute it ask for extract file. Just choose any directory you want extract and extract it

Step 3) Now in extracted folder you see dk.rb file. it is development kit installation file, we need it when we install development kit in ruby.

Page 8: Cucumber Tutorial

Step 4) Go back to Ruby cmd prompt. And locate development kit directory in cmd which you extracted for development kit.

Step 5) Now type command "ruby (your development kit path) dk.rb init" to initialize process of development kit as shown in image.

Page 9: Cucumber Tutorial

Step 6) Next, we have to install development kit. Type command in cmd "ruby (your development kit path)dk.rb install" for installation.

Step 7) Devkit installation completed:

Page 10: Cucumber Tutorial

PART C) Install CucumberStep 1) Type in Ruby cmd "gem install cucumber". This command will download and install Cucumber at command line itself

After few seconds cucumber installation procedure has been start

Page 11: Cucumber Tutorial

Step 2) To verify cucumber is installed successfully or not just type "cucumber –version"

PART D) Install IDETo write Cucumber Test Scripts, you need IDE

There are two popular GUI based script editor for Windows which can be used for Cucumber - .

1. Ruby mine: You can download from http://www.jetbrains.com/ruby/download/

2. Komodo edit 1.8 You can download from http://www.activestate.com/komodo-ide/downloads

I suggest as a beginner, use komodo edit as it is easy to use.

Page 12: Cucumber Tutorial

Step 1) Open Downloaded Setup file and Click On next

Step 2) Accept license agreement

Page 13: Cucumber Tutorial

Step 3) Select directory for installation and click on next

Step 4) Click on next to install

Step 5) Installation process starts

Page 14: Cucumber Tutorial

Step 6) Komodo install successfully

PART E) Installation For AnsiCon:In command prompt

A "Pass" Cucumber test case is shown in Green A "Fail" Cucumber test case is shown in RedTo show colorized text in cmd prompt you have to install Ansicon tool. You can download

Installation Steps

Step 1) Download Ansicon here

Page 16: Cucumber Tutorial

PART F) Installation Watir

Watir is used for Web Automation in CucumberTo install Watir , in ruby cmd type "gem install watir"

PART G) rspec Installation

It is used to run additional libraries of cucumber for test run.To install rspec, in ruby cmd type "gem install rspec"

Page 17: Cucumber Tutorial

PART H) colorize Installation

It helps to colorize error syntaxTo install rspec, in ruby cmd type "gem install colorize"

Finally you are done and all set J

Cucumber BasicsFor every cucumber project there is a single directory at the root of the project named "features". This is where all of your cucumber features will reside. In this directory you will find additional directories, which is step_definition and support directories

What is "Feature File"?Feature File consist of following components -

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).

Page 18: Cucumber Tutorial

Given: It specifies the context of the text to be executed. By using datatables "Given", step can also be parameterized.

When: "When" specifies the test action that has to performed Then: The expected outcome of the test can be represented by "Then"Example:

Feature: Visit career guide page in career.guru99.com

Scenario : Visit career.guru99.com

Given: I am on career.guru99.com

When: I click on career guide menu

Then: I should see career guide page

What is "Step Definition"Step definition maps the test case steps in the feature files    (introduced by Given/When/Then) to code, which executes and checks the outcomes from the system under test. For a step definition to be executed, it must match the given compoent in a feature. Step definition is defined in ruby files under "features/step_definitions/*_steps.rb".

Example for Step Definition: Here we will above example of browsing career.guru99.com do We will use features like "When, Then, Given "

Step 1:

Page 19: Cucumber Tutorial

Given (/^ I am on career.guru99.com$/) do

Browser.goto "http://career.guru99.com" -This will visit career.guru99 on browser

end

Step 2:

When (/^ click on career guide menu$/) do

Browser.text (:name, " career guide" ).click – This will click "career guide menu"

end

Step 3:

Then (/^ I should see career guide page$/) do

Browser.goto "http://career.guru99.com/category/career-guide/" - It will visit "career guide page"

end

Summary:

You need 2 Files – Features and Step Definition to execute a Cucmber test scenario

Features file contain high level description of the test scenario in simple language

Steps Definition file contains the actual code to execute the test scenario in the Features file.

Your First Cucumber ScriptTest Scenario: Verify output when Email id is not entered

Test Steps:

1. Open Browser2. Go To http://demo.guru99.com/3. Do not enter Email id4. Click Submit

Expected Result: Error is shown

Page 20: Cucumber Tutorial

 

 

Step 1) Open komodo Editor via windows start menu

You will See Komodo Dashboard as below

Page 21: Cucumber Tutorial

 

Step 2) Now we have to create a

1. new feature file in feature folder2. step definition file in step _definitions folder3. config file in support folder

Create a New file From the File > New > New File

Page 22: Cucumber Tutorial

Step 3) Save File in "yourfolder/features/" with name "yourfilename.feature"

Step 4) To execute our scenario, save the following commands in the Feature File

Page 23: Cucumber Tutorial

Step 5) Now lets Run our First feature file.!

Click on "Start Command Prompt With ruby"

 

Step 6) Type "cucumber yourfolder/your feature file path and press enter

Page 24: Cucumber Tutorial

It will look like this!

You see the error because you have to write step definitions file for feature file

Step 7) Lets create step definition file for our Feature File!

Create a new file in komodo editor

Page 25: Cucumber Tutorial

Step 8) Save File As below in "yourfolder/features/step_definititons" with name test_step.rb

Step 9) Write the following code into the step file

Above code will call the gems we have installed.

Next add Step Definitions For Scenario as below:

Page 27: Cucumber Tutorial

Step 11) Lets code the next scenario

Test Scenario: Verify output when Email id is not entered

Test Steps:

1. Open Browser2. Go To http://demo.guru99.com/3. Do enter Email id4. Click Submit

Expected Result: Error is shown

let's write feature and scenarios as below for this testing

Page 28: Cucumber Tutorial

And write step definitions as below for this scenario:

Run feature file from ruby cmd

Page 29: Cucumber Tutorial

 

Sample code of feature file:

Feature: guru99 Demopage Login

In order to Login in Demopage we have to enter login details

Scenario: Register On Guru99 Demopage without email

Given I am on the Guru99 homepage

When enter blank details for Register

Then error email shown

Scenario: Register On Guru99 Demopage with valid email

Given I am on the Guru99 homepage

When enter details for Register

Then login details shown

Sample of of step definition file for this feature file:

?12

require 'watir-webdriver'

Page 30: Cucumber Tutorial

34567891011121314151617181920212223242526272829303132333435363738394041424344454647

require 'colorize'

browser = Watir::Browser.new

Given (/^I am on the Guru99 homepage$/)do

browser.goto "http://demo.guru99.com"

end

When (/^enter blank details for Register$/)do

browser.text_field(:name,"emailid").set(" ")

browser.button(:name,"btnLogin").click

end

Then (/^error email shown$/)do

puts " Email is Required".red

browser.close

end

When (/^enter details for Register$/)do

browser = Watir::Browser.new

browser.goto "http://demo.guru99.com"

browser.text_field(:name,"emailid").set("[email protected]")

browser.button(:name,"btnLogin").click

end

Then (/^login details shown$/)do

puts " Sucessfully register"

browser.close

end