-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
334 lines (283 loc) · 13.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
73
74
75
76
77
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from flask import Flask, make_response, send_file, session, request, redirect
import os
import json
import api.api as api
import api.prjinit as prjinit
import api.diff as diff
import shutil
app = Flask(__name__)
PRJ_ROOT = './static/projects/'
@app.route("/")
def index():
return send_file("templates/index.html")
@app.route("/create_project", methods=['POST'])
def create_project():
request_data = json.loads(request.data)
config_data = request_data['config']
devices_data = request_data['devices']
resp_code = prjinit.create_project(
config_data, devices_data, PRJ_ROOT)
if resp_code == 0:
return api.create_json_response("success", 0, message="Project Created.")
else:
return api.create_json_response("failure", -1, message="Project cannot be created.")
@app.route("/projects", methods=['GET'])
def get_projects():
projects = []
for filename in os.listdir(PRJ_ROOT):
if os.path.isdir(PRJ_ROOT + filename):
projects.append(filename)
return api.create_json_response("success", 0, data=projects)
@app.route("/testcases/<project_name>", methods=['GET'])
def getTestCases(project_name):
if os.path.isdir(PRJ_ROOT + project_name) is False:
return api.create_json_response("failure", -1, message="Project does not exist.")
config_data = api.readInputFile(
api.config_file_path(PRJ_ROOT, project_name))
devices_data = api.readInputFile(
api.devices_file_path(PRJ_ROOT, project_name))
response_data = {
'projectName': config_data['projectName'],
'testcases': None
}
testcases = []
for subject in config_data['subject']:
page_name = subject['pageName']
page_url = subject['pageUrl']
testcase = {
'pageName': page_name,
'pageUrl': page_url,
'images': []
}
for device in subject['devices']:
image = {
'device': device,
'source': None,
'screenshot': None,
'output': {
'contourOnSubject': None,
'contourOnSource': None,
'diff': None
}
}
source_file_path = api.source_file_path(
PRJ_ROOT, project_name, page_name, device)
screenshot_file_path = api.screenshot_file_path(
PRJ_ROOT, project_name, page_name, device)
contourOnSource_file_path = api.contourOnSource_file_path(
PRJ_ROOT, project_name, page_name, device)
contourOnSubject_file_path = api.contourOnSubject_file_path(
PRJ_ROOT, project_name, page_name, device)
diff_file_path = api.diff_file_path(
PRJ_ROOT, project_name, page_name, device)
if(os.path.isfile(source_file_path)):
image['source'] = api.source_file_path(
PRJ_ROOT, project_name, page_name, device, True)
if(os.path.isfile(screenshot_file_path)):
image['screenshot'] = api.screenshot_file_path(
PRJ_ROOT, project_name, page_name, device, True)
if(os.path.isfile(contourOnSource_file_path)):
image['output']['contourOnSource'] = api.contourOnSource_file_path(
PRJ_ROOT, project_name, page_name, device, True)
if(os.path.isfile(contourOnSubject_file_path)):
image['output']['contourOnSubject'] = api.contourOnSubject_file_path(
PRJ_ROOT, project_name, page_name, device, True)
if(os.path.isfile(diff_file_path)):
image['output']['diff'] = api.diff_file_path(
PRJ_ROOT, project_name, page_name, device, True)
testcase['images'].append(image)
testcases.append(testcase)
response_data['testcases'] = testcases
return api.create_json_response("success", 0, data=response_data)
@app.route("/upload/<project_name>", methods=['POST'])
def upload(project_name):
if os.path.isdir(PRJ_ROOT + project_name) is False:
return api.create_json_response("failure", -1, message="Project does not exist.")
img_type = request.form.get('type')
img_page_name = request.form.get('pageName')
img_device = request.form.get('device')
file_path = PRJ_ROOT + project_name + '/input/' + \
img_type + '/' + img_page_name + '/' + \
img_device + '.png'
uploaded_File = request.files['file']
d = os.path.dirname(file_path)
if not os.path.exists(d):
os.makedirs(d)
uploaded_File.save(file_path)
if os.path.isfile(file_path):
output = {
'path': None
}
if img_type == 'source':
output['path'] = api.source_file_path(
PRJ_ROOT, project_name, img_page_name, img_device, True)
else:
output['path'] = api.screenshot_file_path(
PRJ_ROOT, project_name, img_page_name, img_device, True)
return api.create_json_response("success", 0, data=output)
else:
return api.create_json_response("failure", -1, message="File cannot be uploaded.")
@app.route("/compare/<project_name>/<page_name>/<device>", methods=['GET'])
def compare(project_name, page_name, device):
if os.path.isdir(PRJ_ROOT + project_name) is False:
return api.create_json_response("failure", -1, message="Project does not exist.")
elif os.path.isfile(api.source_file_path(PRJ_ROOT, project_name, page_name, device)) is False:
return api.create_json_response("failure", -1, message="Source image does not exist")
elif os.path.isfile(api.screenshot_file_path(PRJ_ROOT, project_name, page_name, device)) is False:
return api.create_json_response("failure", -1, message="Screenshot image does not exist")
diff_img, source_img, subject_img = diff.compare(api.source_file_path(
PRJ_ROOT, project_name, page_name, device), api.screenshot_file_path(PRJ_ROOT, project_name, page_name, device))
diff.write(diff_img, api.diff_file_path(
PRJ_ROOT, project_name, page_name, device))
diff.write(source_img, api.contourOnSource_file_path(
PRJ_ROOT, project_name, page_name, device))
diff.write(subject_img, api.contourOnSubject_file_path(
PRJ_ROOT, project_name, page_name, device))
error_code = 0
error_message = ""
if os.path.isfile(api.diff_file_path(
PRJ_ROOT, project_name, page_name, device)) is False:
error_code = -1
error_message = error_message + "Difference file could not be created."
if os.path.isfile(api.contourOnSource_file_path(
PRJ_ROOT, project_name, page_name, device)) is False:
error_code = -1
error_message = error_message + \
"Countour on source file could not be created."
if os.path.isfile(api.contourOnSource_file_path(
PRJ_ROOT, project_name, page_name, device)) is False:
error_code = -1
error_message = error_message + \
"Countour on subject file could not be created."
if error_code is -1:
return api.create_json_response("failure", -1, message=error_message)
else:
output = {
'contourOnSubject': None,
'contourOnSource': None,
'diff': None
}
output['contourOnSource'] = api.contourOnSource_file_path(
PRJ_ROOT, project_name, page_name, device, True)
output['contourOnSubject'] = api.contourOnSubject_file_path(
PRJ_ROOT, project_name, page_name, device, True)
output['diff'] = api.diff_file_path(
PRJ_ROOT, project_name, page_name, device, True)
return api.create_json_response("success", 0, data=output)
@app.route("/add_test_case/<project_name>", methods=['POST'])
def add_test_case(project_name):
if os.path.isdir(PRJ_ROOT + project_name) is False:
return api.create_json_response("failure", -1, message="Project does not exist.")
request_data = json.loads(request.data)
config_data = api.readInputFile(api.config_file_path(PRJ_ROOT, project_name))
new_page = {
'pageName':request_data['pageName'],
'pageUrl':request_data['pageUrl'],
'devices':None
}
config_data['subject'].append(new_page)
f = open(api.config_file_path(PRJ_ROOT, project_name),'w')
f.write(json.dumps(config_data))
f.close()
return api.create_json_response("success", 0, message="Test case added.")
@app.route("/delete_test_case/<project_name>", methods=['POST'])
def delete_test_case(project_name):
if os.path.isdir(PRJ_ROOT + project_name) is False:
return api.create_json_response("failure", -1, message="Project does not exist.")
request_data = json.loads(request.data)
page_name = request_data['pageName']
config_data = api.readInputFile(api.config_file_path(PRJ_ROOT, project_name))
index=0
print page_name
for page in config_data['subject']:
if page['pageName'] == page_name:
config_data['subject'].pop(index)
print page['pageName']
break
index=index+1
f = open(api.config_file_path(PRJ_ROOT, project_name),'w')
f.write(json.dumps(config_data))
f.close()
source_folder_path = PRJ_ROOT + project_name + '/input/source/' + page_name
if os.path.isdir(source_folder_path):
shutil.rmtree(source_folder_path)
screenshot_folder_path = PRJ_ROOT + project_name + '/input/screenshots/' + page_name
if os.path.isdir(screenshot_folder_path):
shutil.rmtree(screenshot_folder_path)
output_folder_path = PRJ_ROOT + project_name + '/output/' + page_name
if os.path.isdir(output_folder_path):
shutil.rmtree(output_folder_path)
return api.create_json_response("success", 0, message="Test case deleted.")
@app.route("/delete_test_device/<project_name>", methods=['POST'])
def delete_test_device(project_name):
if os.path.isdir(PRJ_ROOT + project_name) is False:
return api.create_json_response("failure", -1, message="Project does not exist.")
request_data = json.loads(request.data)
page_name = request_data['pageName']
device_name = request_data['device']
config_data = api.readInputFile(api.config_file_path(PRJ_ROOT, project_name))
for page in config_data['subject']:
if page['pageName'] == page_name:
index = 0
for device in page['devices']:
if device == device_name:
page['devices'].pop(index)
break
index = index+1
f = open(api.config_file_path(PRJ_ROOT, project_name),'w')
f.write(json.dumps(config_data))
f.close()
source_file_path = api.source_file_path(PRJ_ROOT, project_name, page_name, device_name)
if os.path.isfile(source_file_path):
os.remove(source_file_path)
screenshot_file_path = api.screenshot_file_path(PRJ_ROOT, project_name, page_name, device_name)
if os.path.isfile(screenshot_file_path):
os.remove(screenshot_file_path)
output_folder_path = PRJ_ROOT + project_name + '/output/' + page_name + '/' + device_name
if os.path.isdir(output_folder_path):
shutil.rmtree(output_folder_path)
return api.create_json_response("success", 0, message="Test device deleted.")
@app.route("/add_valid_device/<project_name>", methods=['POST'])
def add_valid_device(project_name):
if os.path.isdir(PRJ_ROOT + project_name) is False:
return api.create_json_response("failure", -1, message="Project does not exist.")
request_data = json.loads(request.data)
new_device={
'deviceName':request_data['newDevice'],
'width':request_data['width'],
'height':request_data['height']
}
devices_data = api.readInputFile(api.devices_file_path(PRJ_ROOT, project_name))
devices_data[request_data['newDevice']] = new_device
f = open(api.devices_file_path(PRJ_ROOT, project_name),'w')
f.write(json.dumps(devices_data))
f.close()
return api.create_json_response("success", 0, message="Device added to valid devices list.")
@app.route("/add_test_device/<project_name>", methods=['POST'])
def add_test_device(project_name):
if os.path.isdir(PRJ_ROOT + project_name) is False:
return api.create_json_response("failure", -1, message="Project does not exist.")
request_data = json.loads(request.data)
device_to_add = request_data['device']
page_name = request_data['pageName']
devices_data = api.readInputFile(api.devices_file_path(PRJ_ROOT, project_name))
if devices_data['device_to_add'] is None:
return api.create_json_response("failure", -1, message="Not a valid device.")
config_data = api.readInputFile(api.config_file_path(PRJ_ROOT, project_name))
for page in config_data['subject']:
if page['pageName'] == page_name:
if device_to_add not in page['devices']:
page['devices'].append(device_to_add)
break
else:
return api.create_json_response("failure", -1, message="Testcase already contains this device.")
f = open(api.config_file_path(PRJ_ROOT, project_name),'w')
f.write(json.dumps(config_data))
f.close()
return api.create_json_response("success", 0, message="Device added to test case.")
@app.after_request
def add_header(response):
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
return response
if __name__ == '__main__':
app.run(host='0.0.0.0')