FyreeW
FyreeW

Reputation: 415

Is it possible to get data from Cucumber scenario, parse it using a ruby script?

I am trying figure out how to parse a feature file, and get the name and description of every scenario in the feature file to put into a CSV file. For example if I had a cucumber scenario like so

Feature: Feature Info

Scenario: Name 1
Description 1
    Rest of the cucumber script

Scenario: Name 2
Description 2
    Rest of the cucumber script

I would be able to take out the Name data and Description, so I could that into a cell in CSV. The cells would end up something like:

First Column - Scenario Name Header: Name 1, Name 2
Second Column - Scenario Description Header: Description 1, Description 2

I don't have any code yet, since I am not sure where to start really. Any help would be great.

Upvotes: 1

Views: 1260

Answers (1)

Stephen Harper
Stephen Harper

Reputation: 51

Yes, a cucumber feature file is just a text file. You can open it and read it line by line in Ruby as with any other ASCII/UTF8 character file. You can use the Ruby match instruction to select and classify lines and extract the desired information. See http://ruby-doc.org/core-2.2.3/Regexp.html

Since you are building a unit record out of multiple lines you will need to hold state until you are finished processing each group.

 File.open( feature_file ).each do |line|
   # do matching here
   # build unit record
   # pass to output
   # rinse and repeat
 end

Upvotes: 2

Related Questions