Newer
Older
import os
from checks import Checks
class ParseRobotFile:
def __init__(self, filename: str, execdir: str):
self.test_suite = os.path.basename(filename).split('.')[0]
with open(filename, 'r') as file:
# Read the contents of the file
self.file_contents = file.read()
self.variables = dict()
self.execdir = execdir
self.resource_file = str()
self.get_variables_data()
self.get_apiutils_path()
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def get_variables_data(self):
string = self.get_substring(initial_string='*** Variables ***\n', final_string='*** ', include=False)
regex = r"(\$\{.*\})\s*=\s*(.*)\n"
matches = re.finditer(regex, string, re.MULTILINE)
for match in matches:
# Check that we have two groups matched
if len(match.groups()) == 2:
self.variables[match.group(1)] = match.group(2)
else:
print("Error, the variable is not following the format ${thing} = <value>")
def get_expected_status_code(self, keyword: str):
# Check Response Status Code ${expected_status_code} ${response.status_code}
# Check Response Body Containing ProblemDetails Element Containing Type Element set to
# ... ${response.json()}
# ... ${ERROR_TYPE_LD_CONTEXT_NOT_AVAILABLE}
string = self.get_substring(initial_string=keyword, final_string='\n', include=True)
expected_status_code = string.split(' ')[1]
if expected_status_code.isdigit():
return expected_status_code
else:
return self.variables[expected_status_code]
def get_apiutils_path(self):
string = self.get_substring(initial_string='Resource', final_string='*** Variables ***', include=True)
result = [item for item in string.split('\n') if 'ApiUtils.resource' in item and item[0] != '#']
if len(result) == 1:
regex = r"\s*Resource\s*(.*)\s*"
matches = re.finditer(regex, result[0], re.MULTILINE)
for match in matches:
# Check that we have 1 group matched
if len(match.groups()) == 1:
self.resource_file = match.group(1)
else:
print("Error, unexpected format")
self.resource_file = self.resource_file.replace('${EXECDIR}', self.execdir)
def get_substring(self, initial_string: str, final_string: str, include: bool) -> str:
index_start = self.file_contents.find(initial_string)
if include:
string = self.file_contents[index_start:]
else:
string = self.file_contents[index_start+len(initial_string):]
if final_string is not '':
index_end = string.find(final_string)
string = string[:index_end]
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def get_test_cases(self):
index_start = self.file_contents.find('*** Test Cases ***')
string = self.file_contents[index_start+len('*** Test Cases ***')+1:]
print(string)
pattern = f'{self.test_suite}_\d+\s.*'
matches = re.findall(pattern=pattern, string=string)
indexes = list()
self.test_case_names = list()
for match in matches:
name = match.strip()
self.test_case_names.append(name)
indexes.append(string.find(name))
self.test_cases = dict()
for i in range(0, len(indexes)-1):
self.test_cases[self.test_case_names[i]] = string[indexes[i]:indexes[i+1]]
self.test_cases[self.test_case_names[-1]] = string[indexes[-1]:]
print()
def get_checks(self, test_name, apiutils):
data = Checks()
test_content = self.test_cases[test_name]
# Get The lines starting by 'Check'
checks = list()
param = dict()
lines_starting_with_check = re.findall(r'^\s*Check.*', test_content, re.MULTILINE)
for line in lines_starting_with_check:
check, param = self.get_data_check(content=test_content, checks=data, line=line.strip())
result = data.get_checks(checks=check, **param)
checks.append(check)
return checks
def get_data_check(self, content, checks, line):
content = line.split(" ")
aux = len(content)
position_params = checks.args[content[0]]
if aux == 1:
# We are in multiline classification of the Check, need to extract the parameter for the next lines
self.find_attributes_next_line(content=content, name=content[0])
elif aux > 1:
# We are in one line definiton
param = dict()
for i in range(0, len(position_params['position'])):
param_key = position_params['params'][i]
param_position = position_params['position'][i]
param_value = self.variables[content[param_position]]
param[param_key] = param_value
return content[0], param
else:
raise Exception("ERROR, line should contain data")
def find_attributes_next_line(self, content, name):
index_start = content.find(name)
aux = content[index_start+len(name)+1:]