Executing a Testsuite from PowerShell

Table of Contents

Use the following PowerShell-Skript to execute a testsuite and to receive the testresults.

To run the script it’s important to create an Api Client first. See this article to get to know how it’s done.

# Enter the base-url of the BiG EVAL API and instance
$bigevalUrl = "https://localhost:1703/"
$baseUrl = $bigevalUrl+"api/v1/default/"
Write-Host $baseUrl;

# Enter the ID of the suite to execute (get it from the URL when the suite is open in the UI)
$suiteId = 1

# Enter the ID of the test to check later
$testId = 8

# Enter the Client ID of the API Client.
$clientId = "e01b7ba2dd930ea97da79b0005341aaa"
 
# Enter the Client Secret of the API Client.
$clientSecret = "5d1b205aae2d4fec90d424469abcd8ac"



# Acquire OAuth2 Access-Token
[string]$accessToken = $null
try {
    $tokenEndpoint = $bigevalUrl+"connect/token"
    $authBody = "grant_type=client_credentials&scope=bigeval-api-v1&client_id=" + $clientId + "&client_secret=" + $clientSecret
    $authResponse = Invoke-RestMethod -Uri $tokenEndpoint -Method POST -Body $authBody
    $accessToken = $authResponse.access_token
    $accessTokenExpiresAt = (Get-Date).AddSeconds($authResponse.expires_in)
    Write-Host "OAuth Token Request successful"
    Write-Host " Token expires at:" $accessTokenExpiresAt
} catch {
    Write-Host "OAuth Token Request failed"
    Write-Host " StatusCode:" $_.Exception.Response.StatusCode.value__
    Write-Host " StatusDescription:" $_.Exception.Response.StatusDescription
}



# Execute the suite. This creates a new run
$executeResult = Invoke-RestMethod -Uri ($baseUrl + "suites/" + $suiteId + "/execute") -Headers @{"Authorization"="Bearer $accessToken"}
Write-Host "Suite started"

# Poll the status of the run until it finishes
do
{
    # Wait a specific amount of time until polling
    Start-Sleep -Milliseconds 5000

    # Fetch the status and testresults
    $currentResponse = Invoke-RestMethod -Uri ($baseUrl + "statistics/testresultsscoped?runScope=3&runScopeIdentifier=" + $suiteId + "&skip=0&take=1") -Headers @{"Authorization"="Bearer $accessToken"}
        
    $currentResult = $currentResponse.items[0]
    
    # Write the run-status to the console
    Write-Host $currentResult.status
} while($currentResult.status -ne "FINISHED")

# Write some statistics to the console
Write-Host ("Run-ID:           " + $currentResult.runId)
Write-Host ("Tests run:        " + $currentResult.totalCount)
Write-Host ("Tests succeeded:  " + $currentResult.succeededCount)
Write-Host ("Tests failed:     " + $currentResult.failedCount)
Write-Host ("Tests excepted:   " + $currentResult.exceptionsCount)
Write-Host ("Success-Rate:     " + ($currentResult.succeededCount / $currentResult.totalCount * 100) + "%")


# Get testresults of a specific test
$runId = $currentResult.runId
$runResults = Invoke-RestMethod -Uri ($baseUrl + "runs/" + $runId + "/testresults?search=%23" + $testId + "&status=SUCCESS,FAILED,EXECUTING,NOTEVALUATED,DIFFERENTDIMENSIONALITY,CANCELED,NOTENOUGHPROBES,EXCEPTION&sortBy=&skip=0&take=10") -Headers @{"Authorization"="Bearer $accessToken"}

# Write all test-results (could be multiple) to the console
$runResults.items | ForEach-Object {
    Write-Host 
    Write-Host ("Test-ID:          " + $_.testId)
    Write-Host ("Test-Name:        " + $_.testName)
    Write-Host ("Test-Status:      " + $_.status)
}

# Return exit-code depending on the test-result
if($currentResult.failedCount -eq 0)
{
    exit 0
}
else 
{
    exit -1
}