How to convert or parse a Gherkin Feature FIle into JSON object using Python I have a Cucumber Gherkin file that needs to be converted into JSON object. I am using Python dictionary and json library and file object.
from pytest_bdd import scenario
Can you please clarify why this is giving an error?
CodePudding user response:
import json
from pytest_bdd import scenario
"""
data = {} # fix same keys by just putting vals
data["Feature"] = "A site where you can publish your articles"
data["Scenarios"] = {
"Scenario": "Publishing the article",
"Given": "I'm an author user",
"GivenAnds": ["I have an article", "go here"],
"When": "I go to article",
"WhenAnds":["I press publish button", "go here"],
"Then": "I should not see the error message",
"ThenAnds":["article should be published", "go here"]
}"""
def program():
data = {}
file = open("myfeature.feature", "r ")
feature = ""
data["Feature"] = None
data["Scenarios"] = {
"Given": None,
"GivenAnds": [],
"When": None,
"WhenAnds":[],
"Then": None,
"ThenAnds":[]
}
file_lines = [] # each element is a line ni file
with open("myfeature.feature", 'r ') as file:
for line in file.readlines():
file_lines.append(line)
cur_statement = None
for i, line in enumerate(file_lines):
if "Feature: " in line:
feature = line.split("Feature: ", 1)[1]
data["Feature"] = feature
if "Scenario" in line:
scenario = line.split("Scenario: ", 1)[1]
data["Scenarios"]["Scenario"] = scenario
if "Given " in line:
given = line.split("Given ", 1)[1]
data["Scenarios"]["Given"] = given
cur_statement = "Given "
if "When " in line:
when = line.split("When ", 1)[1]
data["Scenarios"]["When"] = when
cur_statement = "When "
if "Then " in line:
then = line.split("Then ", 1)[1]
data["Scenarios"]["Then"] = then
cur_statement = "Then "
if "And " in line:
if cur_statement == "Given ":
g_and = line.split("And ", 1)[1]
data["Scenarios"]["GivenAnds"].append(g_and)
if cur_statement == "When ":
w_and = line.split("And ", 1)[1]
data["Scenarios"]["WhenAnds"].append(w_and)
if cur_statement == "Then ":
t_and = line.split("And ", 1)[1]
data["Scenarios"]["ThenAnds"].append(t_and)
print("\n")
print(data)
print("\n")
json_data = json.dumps(data, indent=4)
print(json_data)
program()
CodePudding user response:
import json
data = {}
data["Feature"] = "A site where you can publish your articles"
data["Scenarios"] = {
"Publishing the article":{
"Given": "I'm an author user",
"GivenAnds": ["I have an article", "ok go"],
"When": "I go to the article page",
"WhenAnds": ["I press the publish button", "ok go"],
"Then": "I should not see the error message",
"ThenAnds": ["article should be published", "ok go"]
}
}
class Scenario:
def __init__(self, name):
self.name = name
self.given = None
self.when = None
self.then = None
self.given_ands = []
self.then_ands = []
self.when_ands = []
def program():
data = {"Feature": None, "Scenarios": {}}
file = open("myfeature.feature", "r ")
feature = ""
file_lines = [] # each element is a line ni file
with open("myfeature.feature", 'r ') as file:
for line in file.readlines():
file_lines.append(line)
scenario_objs = []
cur_scenario = None
cur_statement = None
for i, line in enumerate(file_lines):
if "Feature: " in line:
feature = line.split("Feature: ", 1)[1]
if "Scenario" in line:
name = line.split("Scenario: ", 1)[1]
s1 = Scenario(name)
cur_scenario = s1
scenario_objs.append(s1)
if "Given " in line:
given = line.split("Given ", 1)[1]
cur_scenario.given = given
cur_statement = "Given "
if "When " in line:
when = line.split("When ", 1)[1]
cur_scenario.when = when
cur_statement = "When "
if "Then " in line:
then = line.split("Then ", 1)[1]
cur_scenario.then = then
cur_statement = "Then "
if "And " in line:
if cur_statement == "Given ":
g_and = line.split("And ", 1)[1]
cur_scenario.given_ands.append(g_and)
if cur_statement == "When ":
w_and = line.split("And ", 1)[1]
cur_scenario.when_ands.append(w_and)
if cur_statement == "Then ":
t_and = line.split("And ", 1)[1]
cur_scenario.then_ands.append(t_and)
for s in scenario_objs:
data["Feature"] = feature
data["Scenarios"][s.name] = {}
data["Scenarios"][s.name]["Given"] = s.given
data["Scenarios"][s.name]["When"] = s.when
data["Scenarios"][s.name]["Then"] = s.then
data["Scenarios"][s.name]["GivenAnds"] = s.given_ands
data["Scenarios"][s.name]["ThenAnds"] = s.then_ands
data["Scenarios"][s.name]["WhenAnds"] = s.when_ands
print("\n")
print(data)
print("\n")
json_data = json.dumps(data, indent=4)
print(json_data)
program()