Saturday, 13 December 2014

Ruby and Cucumber Basic example- Basic input, table and data

Feature: Cucumber basics using Ruby

@portalScenario: Sum example
   Given I add "4+8"
   And Sum should be "6"

  @test  Scenario Outline: Sum example for multiple values
   Given I add for <vals>
   And Sum should be <answer> for <vals>

Examples:   
|vals |answer|   
|"2+4"|"6"  |   
|"4+4"|"9"  |   
|"4+5"|"9"  |   
|"4+0"|"199"|

@test   
Scenario: Testing for multiple data at each step
Given I validate product feed for the details
    |username     |vickyvenu81@gmal.com  |      
    |profile       |venu                 |      
    |emp_id         |739                 |      
    |project        |bankofamerica       |     
And print pass or fail
    |col1       |col2  |     
    |data1   |data2 |     
    |data3   |data4 |
     

++++++++++++++++Step Definitions+++++++++++++++++++++++++++
Given(/^I add "([^"]*)"$/) do |arg|
  @sum= eval(arg)
end
And(/^Sum should be "([^"]*)"$/) do |arg|
  puts @sum  if @sum.to_i != 6    puts "fail"  else    puts "pass"  endend
Given(/^I add for "([^"]*)"$/) do |vals|
  @sum= eval(vals)
end
And(/^Sum should be "([^"]*)" for "([^"]*)"$/) do |answer, vals|
  puts 'sum = '+ @sum.to_s
  if @sum!=answer.to_i
    puts 'error in evaluating sum and sum should be: '+ @sum.to_s
  endend

Given(/^I validate product feed for the details$/) do |table|
  # table is a table.hashes.keys # => [:username, :password, :profile, :logout, :items_count]  puts '+++++++++++++++++++++++++++++++++++++++++++'  data = table.rows_hash
  puts data['emp_id']   #Here we are accesing row wise.. |property| value|  this will print 739end
And(/^print pass or fail$/) do |table|
  puts "table.class:-  #{table.class}"  puts 'table inspection:'  puts table.inspect

  table.hashes.each do |hash|
    puts "col1 data should be printed: #{hash['col1']}"    puts "col2 data should be printed: #{hash['col2']}"  end
  puts 'table.raw example: '  data= table.raw
  puts data[0]

end

+++++++++++++++++++++Output++++++++++++++++++++++++++++++++
C:\Ruby193\bin\ruby.exe -EUTF-8 -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:\Ruby193\bin/cucumber C:/RnD/features/gmail.feature --format Teamcity::Cucumber::Formatter --expand --color -r features
Testing started at 20:17 ...
Tag: @@portal

12

fail
Tag: @@test

Given I add for <vals>                          # features/step_definitions/gmailtest.rb:14

And Sum should be <answer> for <vals>           # features/step_definitions/gmailtest.rb:18

sum = 6

sum = 8

error in evaluating sum and sum should be: 8

sum = 9

sum = 4

error in evaluating sum and sum should be: 4
Tag: @@test

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

739

table.class:-  Cucumber::Ast::Table

table inspection:


  |     col1  |     col2  |
  |     data1 |     data2 |
  |     data3 |     data4 |

col1 data should be printed: data1

col2 data should be printed: data2

col1 data should be printed: data3

col2 data should be printed: data4

table.raw example: 

["col1", "col2"]
6 scenarios (6 passed)
12 steps (12 passed)
0m0.181s

Process finished with exit code 0

+++++++++++++++++++++++Explanation++++++++++++++++++++++++

# http://www.ruby-doc.org/gems/docs/d/davidtrogers-cucumber-0.6.2/Cucumber/Ast/Table.html# Step Definitions that match a plain text Step with a multiline argument table will receive it as an instance of Table.# A Table object holds the data of a table parsed from a feature file and lets you access and manipulate the data in different ways.#                                                                                                                                  Given I have:# | a | b |# | c | d |# This will store [['a', 'b'], ['c', 'd']] in the data variable.## hashes()# Converts this table into an Array of Hash where the keys of each Hash are the headers in the table.# For example, a Table built from the following plain text:## | a | b | sum |# | 2 | 3 | 5   |# | 7 | 9 | 16  |# Gets converted into the following:## [{'a' => '2', 'b' => '3', 'sum' => '5'}, {'a' => '7', 'b' => '9', 'sum' => '16'}]## raw: Gets the raw data of this table. For example, a Table built from the following plain text:# rows_hash: Converts this table into a Hash where the first column is used as keys and the second column is used as values## | a | 2 |# | b | 3 |# Gets converted into the following:# {'a' => '2', 'b' => '3'}

No comments:

Post a Comment