-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection-runner.ps1
105 lines (84 loc) · 3.57 KB
/
collection-runner.ps1
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
param (
[string]$apiKey,
[string]$collectionId,
[string]$environmentId,
[string]$workspaceId,
[string]$systemName,
[string]$notificationUrl,
[string]$outputDirectory,
[boolean]$addSuffix
)
# Define the Postman API endpoints
$collectionUrl = "https://api.getpostman.com/collections/$collectionId"
$environmentUrl = "https://api.getpostman.com/environments/$environmentId"
$globalVariablesUrl = "https://api.getpostman.com/workspaces/$workspaceId/global-variables?apikey=$apiKey"
# Define the output file names
$collectionFile = "collection.json"
$environmentFile = "environment.json"
$globalVariablesFile = "globals.json"
$junitFilename = "JUnitReport.xml"
$jsonSummaryFilename = "Summary.json"
[long]$epochTime = Get-Date -UFormat %s
$epochTime = $epochTime * 1000
Write-Output $epochTime
if($addSuffix -eq $true)
{
$currentTime = Get-Date
[string]$fileSufix = $currentTime.ToString("-yyyyMMdd_HHmm")
$junitFilename = "JUnitReport-" + $systemName + $fileSufix + ".xml"
$jsonSummaryFilename = "Summary-" + $systemName + $fileSufix + ".json"
}
if(-not [string]::IsNullOrEmpty($outputDirectory))
{
$collectionFile = Join-Path -Path $outputDirectory -ChildPath $collectionFile
$environmentFile = Join-Path -Path $outputDirectory -ChildPath $environmentFile
$globalVariablesFile = Join-Path -Path $outputDirectory -ChildPath $globalVariablesFile
$junitFilename = Join-Path -Path $outputDirectory -ChildPath $junitFilename
$jsonSummaryFilename = Join-Path -Path $outputDirectory -ChildPath $jsonSummaryFilename
}
# Download the collection
Invoke-RestMethod -Uri $collectionUrl -Headers @{ "X-Api-Key" = $apiKey } | ConvertTo-Json -Depth 100 | Out-File $collectionFile
# Download the environment
Invoke-RestMethod -Uri $environmentUrl -Headers @{ "X-Api-Key" = $apiKey } | ConvertTo-Json -Depth 100 | Out-File $environmentFile
# Download the environment
Invoke-RestMethod -Uri $globalVariablesUrl | ConvertTo-Json -Depth 100 | Out-File $globalVariablesFile
# Run Newman with the specified parameters
newman run $collectionFile -e $environmentFile -g $globalVariablesFile -r junit --reporter-junit-export $junitFilename -k
# Print a success message
Write-Host "Collection and environment downloaded successfully. Collection saved as $collectionFile, environment saved as $environmentFile."
Write-Host "Newman run completed. JUnit report exported as $junitFilename"
# READING THE JUNITREPORT.XML
[xml]$xml = Get-Content -Path $junitFilename
$totalTests = 0
$failedTests = 0
$failedTestList = ""
# Iterate through the test cases
foreach ($testsuite in $xml.testsuites.testsuite) {
$totalTests += [int]$testsuite.tests
foreach ($testcase in $testsuite.testcase) {
if ($testcase.failure) {
$failedTests++
$failedTestList += $testcase.name + "<br/>"
}
}
}
# Create a summary object
$summary = @{
TotalTests = $totalTests
FailedTests = $failedTests
FailedTestList = $failedTestList
System = $systemName
Datetime = $epochTime
}
# Convert the summary object to JSON and save it to a file
$summary | ConvertTo-Json | Set-Content -Path $jsonSummaryFilename
Write-Output "Summary saved to $jsonSummaryFilename"
# SEND NOTIFICATION
if ($failedTests -ge 1 -and -not [string]::IsNullOrEmpty($notificationUrl)) {
$jsonContent = Get-Content -Path $jsonSummaryFilename -Raw
Invoke-RestMethod -Uri $notificationUrl -Method Post -Body $jsonContent -ContentType "application/json"
Write-Output "Result Summary sent to notificationUrl"
}
else {
Write-Output "Skipped Sending Notification: No failed tests or notificationUrl not provided"
}