rubybashshellcucumberaruba

How to capture shell script program output from cucumber/aruba?


I want to capture output which I'm running from the cucumber feature file.

I created one shell script program and placed it in /usr/local/bin/ so it can be accessible from anywhere in system.

abc_qa.sh -

arg=$1
if [[ $arg = 1 ]]
then
    echo $(date)
fi

project structure of cucumber -

aruba -

.

├── features

│ ├── support

│ │ └── env.rb

│ └── use_aruba_cucumber.feature

├── Gemfile

Gemfile -

source 'https://rubygems.org'
gem 'aruba', '~> 0.14.2'

env.rb -

require 'aruba/cucumber'

use_aruba_cucumber.feature -

Feature: Cucumber
 Scenario: First Run
    When I run `bash abc_qa.sh 1`

I want to capture this abc_qa.sh program output in the cucumber itself and compare this date is right or wrong by using any kind of simple test and make this test as a pass.


Solution

  • You can use %x(command) to get the stdout of your command.

    You can then use Time.parse to convert "Sat Nov 5 12:04:18 CET 2016" to 2016-11-05 12:04:18 +0100 as Time object, and compare it to Time.now :

    require 'time'
    time_from_abc_script = Time.parse(%x(bash abc_qa.sh 1))
    puts (Time.now-time_from_abc_script).abs < 5 # No more than 5s difference between 2 times
    

    You could use this boolean in any test file you want.

    For example :

    In features/use_aruba_with_cucumber.feature :

    Feature: Cucumber
     Scenario: First Run
      When I run `bash abc_qa.sh 1`
      Then the output should be the current time
    

    and features/step_definitions/time_steps.rb :

    require 'time'
    
    Then(/^the output should be the current time$/) do
      time_from_script = Time.parse(last_command_started.output)
      expect(time_from_script).to be_within(5).of(Time.now)
    end