Commit 4b5f0d46 authored by Kamlesh Todai's avatar Kamlesh Todai
Browse files

python script file to create Excel file, summarizing the html reports...

python script file to create Excel file, summarizing the html reports generated by running the Postman collections using newman -htmlextra report generator.
parent 70f0c4fd
Pipeline #8765 failed with stages
in 11 seconds
#!/usr/bin/env python3
# importing the libraries
import os
import re
import xlsxwriter
from bs4 import BeautifulSoup
import requests
import datetime
#
# This script processes all the html files that are generated by using the Postman Newman (htmlextra)
# program to run the postman collections for "OSDU Platform Validation project."
# it expects the report files to follow the assumptions and rules mentioned in file
# OSDU Platform Validation CI_CD_Notes.md of "Platform Validation project"
# At present it is assuming 11 collections being run each of the four platforms
# AWS, AZURE, GCP, IBM
# In future work will be done where it should be able handle n number of collections instead of fix 11
# At present this restriction is because of the way the writing of the excel file is implemented,
# specially the writing of the last summary part, after all the collections are processed.
#=======================================================================================================
# Method to process html report file and summarize the results in Excel file
def processFile(fnm, platform, rowNo, colOffset, envFileNamesPopulated):
# print("fileName: " + fnm)
# print("Platform: " + platform)
# print("Row: ", rowNo)
# print("Total Rows: ", totRows)
# print("Column offset", colOffset)
# return()
#
html = open(fnm, 'r').read() # local html
bs = BeautifulSoup(html, features="html.parser")
#
mydivs = bs.findAll("div", {"class": "col-sm-12 mb-3"})
txt = mydivs[0].get_text()
fileInfo = txt.split("\n")
# Get file and environment information
collectionName = ""
enviornmentName = ""
count = 0
pattern = re.compile("Collection:")
# Parse and extract all the necessary values
for elm in fileInfo:
if pattern.search(elm):
if (count == 0):
collectionstr = elm.split(":")
collectionName = collectionstr[1]
pattern = re.compile("Environment:")
count += 1
elif (count == 1):
environmentstr = elm.split(":")
enviornmentName = environmentstr[1]
#
print(collectionName)
print(enviornmentName)
# Save/Collect environment file name
if (envFileNamesPopulated == False):
if (colOffset == 0):
env_file_names.append(enviornmentName)
elif (colOffset == 6):
env_file_names.append(enviornmentName)
elif (colOffset == 10):
env_file_names.append(enviornmentName)
elif(colOffset == 14):
env_file_names.append(enviornmentName)
else:
print("Something wrong while populating env file names")
#
#Get the summary of Total requests pass/fail, Total assertions pass/fail, Total tests skipped
mytab = bs.findAll("table", {"class": "table table-striped table-bordered"})
txt2 = mytab[0].get_text()
summaryInfo = txt2.split()
# Initialize the values
totalRequestsPassed = 0
totalRequestsFailed = 0
totalAssertionPassed = 0
totalAssertionFailed = 0
skippedTests = 0
count = 0
# Parse and extract all the necessary values
for info in summaryInfo:
if (count == 5):
totalRequestsPassed = info
elif (count == 6):
totalRequestsFailed = info
elif (count == 16):
totalAssertionPassed = info
elif (count == 17):
totalAssertionFailed = info
elif (count == 20):
skippedTests = info
count += 1
#
passRate = (100 * float(totalAssertionPassed ) / (float(totalAssertionPassed) + float(totalAssertionFailed)) )
# print("totalRequestsPassed: " + totalRequestsPassed)
# print("totalRequestsFailed: " + totalRequestsFailed)
# print("totalAssertionPassed: " + totalAssertionPassed)
# print("totalAssertionFailed: " + totalAssertionFailed)
# print("skippedTests: " + skippedTests)
# print(passRate)
records.append((collectionName, totalRequestsPassed, totalAssertionPassed, totalAssertionFailed, passRate ))
curRow = len(records) - 1
# print(records[curRow])
for col_num, data in enumerate(records[curRow]):
l_colNum = col_num
if (col_num > 1) and (colOffset > 5):
l_colNum = col_num + colOffset-2
if (l_colNum == 0):
worksheet.write(rowNo, l_colNum, data, collection_cell_format)
elif (l_colNum == 1):
worksheet.write(rowNo, l_colNum, int(data), request_cell_format)
elif ( (l_colNum == 4) or (l_colNum == 8) or (l_colNum == 12) or (l_colNum == 16 ) ):
if (passRate >= 100.0):
worksheet.write(rowNo, l_colNum, data, greenPct_cell_format)
elif (passRate < 60.0):
worksheet.write(rowNo, l_colNum, data, redPct_cell_format)
else:
worksheet.write(rowNo, l_colNum, data, pct_cell_format)
else:
worksheet.write(rowNo, l_colNum, int(data), cell_format)
#Extract run Date Info
if (len(collection_run_date) == 0):
mydivs3 = bs.findAll("div", {"class": "col-md-9 col-lg-12 main"})
txt3 = mydivs3[0].get_text()
dateInfo = txt3.split("\n")
collection_run_date.append(dateInfo[2])
#
#=======================================================================================================
# Start of Main
print("Starting to summarize all the html reports")
records = []
env_file_names = []
env_file_names_populated = False
collection_run_date = []
# Define the headers for the excel file
records.append(("","","","AWS","","","","AZURE","","","","GCP","","","","IBM",""))
records.append(("Collection Name", "#of Requests", "Pass", "Fail", "PassRate","","Pass", "Fail", "PassRate","","Pass", "Fail", "PassRate","","Pass", "Fail", "PassRate"))
#Create and initialize the excel file
workbook = xlsxwriter.Workbook('NewmanHtmlReportsSummary.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column(0, 0, 40)
worksheet.set_column(1, 1, 12)
cell_format = workbook.add_format({'align': 'center', 'bold': True})
summ_cell_format = workbook.add_format({'align': 'left', 'bold': True})
summ_right_cell_format = workbook.add_format({'align': 'right', 'bold': True})
request_cell_format = workbook.add_format({'align': 'center', 'bg_color': '#D8E4BC'})
collection_cell_format = workbook.add_format({'align': 'center', 'bold': True})
collection_cell_format.set_font_color('blue')
#
total_cell_format = workbook.add_format({'align': 'center', 'bold': True, 'bg_color': 'orange', 'font_color': 'blue'})
#
pct_cell_format = workbook.add_format({'align': 'center', 'bold': True})
pct_cell_format.set_num_format('0.00')
#
greenPct_cell_format = workbook.add_format({'align': 'center', 'bold': True})
greenPct_cell_format.set_font_color('green')
greenPct_cell_format.set_num_format('0.00')
#
redPct_cell_format = workbook.add_format({'align': 'center', 'bold': True})
redPct_cell_format.set_font_color('red')
redPct_cell_format.set_num_format('0.00')
#
head_cell_format = workbook.add_format({'align': 'center', 'bold': True, 'bg_color': '#78B0DE'})
head_cell_format.set_font_size(14)
head2_cell_format = workbook.add_format({'align': 'center', 'bold': True, 'bg_color': '#BBD7EF', 'font_color': 'black'})
head2_cell_format.set_font_size(12)
head2_green_cell_format = workbook.add_format({'align': 'center', 'bold': True, 'bg_color': '#BBD7EF', 'font_color': 'green'})
head2_green_cell_format.set_font_size(12)
head2_red_cell_format = workbook.add_format({'align': 'center', 'bold': True, 'bg_color': '#BBD7EF', 'font_color': 'red'})
head2_red_cell_format.set_font_size(12)
#Write the header records to excel file
rowNum = 0
col_offset = 0
for row_num in records:
for col_num, data in enumerate(records[rowNum]):
if (rowNum == 0):
worksheet.write(rowNum, col_num, data, head_cell_format)
else:
# print("rowNum: ", rowNum, " col_num: ", col_num)
if (rowNum == 1):
if ( (col_num == 2) or (col_num == 6) or (col_num == 10) or (col_num == 14) ):
worksheet.write(rowNum, col_num, data, head2_green_cell_format)
elif ((col_num == 3) or (col_num == 7) or (col_num == 11) or (col_num == 15)):
worksheet.write(rowNum, col_num, data, head2_red_cell_format)
else:
worksheet.write(rowNum, col_num, data, head2_cell_format)
rowNum += 1
#process all the reports to summarize the results
curPlatform = ""
localRowNo = rowNum
collName = []
for file in os.listdir("."):
if (file.endswith(".html")): # process only html files
fileParts = file.split(".")
if (fileParts[0] == "index"):
print("Skip - " + file) # skip processing of index.html file
continue
else:
# print(file)
if (fileParts[0] != curPlatform):
#
localRowNo = rowNum
curPlatform = fileParts[0]
#
if (fileParts[0] == "aws"):
col_offset = 0
elif (fileParts[0] == "azure"):
col_offset = 6
elif (fileParts[0] == "gcp"):
col_offset = 10
elif (fileParts[0] == "ibm"):
col_offset = 14
else:
print("Invalid platform: " + fileParts[0])
continue
env_file_names_populated = False
else:
env_file_names_populated = True
if (fileParts[0] == "aws"):
collName.append(fileParts[1])
processFile(file, fileParts[0], localRowNo, col_offset, env_file_names_populated) # process html report file
localRowNo += 1
print("")
#
print("Total number of collections: ", len(collName) )
print("Total number of reports summarized: ", (len(records)-2) )
print("Total number of platforms", ((len(records) - 2)/len(collName)) )
fill_cell_format = workbook.add_format({'bg_color': '#FFE1E5'})
total_row_num = len(collName) + 2
colList = [5,9,13]
for i in range(2, total_row_num):
for j in range(len(colList)):
worksheet.write(i, colList[j], "", fill_cell_format)
#
total_cols = 17
for j in range(total_cols):
if (j == 0):
worksheet.write(13, j, "Total", total_cell_format)
elif ( (j == 5) or (j == 9) or (j == 13) ):
worksheet.write(total_row_num, j, "", total_cell_format)
# continue
elif (j == 1):
worksheet.write_formula('B14', '{=SUM(B3:B13)}', total_cell_format)
elif (j == 2):
worksheet.write_formula('C14', '{=SUM(C3:C13)}', total_cell_format)
elif (j == 3):
worksheet.write_formula('D14', '{=SUM(D3:D13)}', total_cell_format)
elif (j == 4):
worksheet.write_formula('E14', '{=SUM(E3:E13)}', total_cell_format)
elif (j == 6):
worksheet.write_formula('G14', '{=SUM(G3:G13)}', total_cell_format)
elif (j == 7):
worksheet.write_formula('H14', '{=SUM(H3:H13)}', total_cell_format)
elif (j == 8):
worksheet.write_formula('I14', '{=SUM(I3:I13)}', total_cell_format)
elif (j == 10):
worksheet.write_formula('K14', '{=SUM(K3:K13)}', total_cell_format)
elif (j == 11):
worksheet.write_formula('L14', '{=SUM(L3:L13)}', total_cell_format)
elif (j == 12):
worksheet.write_formula('M14', '{=SUM(M3:M13)}', total_cell_format)
elif (j == 14):
worksheet.write_formula('O14', '{=SUM(O3:O13)}', total_cell_format)
elif (j == 15):
worksheet.write_formula('P14', '{=SUM(P3:P13)}', total_cell_format)
elif (j == 16):
worksheet.write_formula('Q14', '{=SUM(Q3:Q13)}', total_cell_format)
#
date_time_obj = datetime.datetime.strptime(collection_run_date[0], '%A, %d %B %Y %H:%M:%S')
str1 = "Collection run date: " + date_time_obj.strftime('%m/%d/%Y, %H:%M:%S')
worksheet.write(15, 0, str1, summ_cell_format)
# str1 = "Summary report run date:\t " + (datetime.datetime.now().strftime('%A, %d %B %Y %H:%M:%S'))
str1 = "Summary report date: " + (datetime.datetime.now().strftime('%m/%d/%Y, %H:%M:%S'))
worksheet.write(16, 0, str1, summ_cell_format)
#
worksheet.write(18, 0, "Environment Files:", summ_cell_format)
numOfEnvs = len(env_file_names)
for i in range(numOfEnvs):
worksheet.write((19+i), 0, env_file_names[i], cell_format)
#
workbook.close()
print(env_file_names)
print(collection_run_date)
print("Summary Report Date: ", datetime.datetime.now())
print("Done")
exit()
#
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment