Skip to content

Instantly share code, notes, and snippets.

@mimholt
Created June 30, 2021 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mimholt/5fd75e96a1c0d4ea56692bd36b24d812 to your computer and use it in GitHub Desktop.
Save mimholt/5fd75e96a1c0d4ea56692bd36b24d812 to your computer and use it in GitHub Desktop.
Teamcity Powershell script to Run Data driven tests
################################
##### CUSTOMIZE BELOW HERE #####
$API_KEY = '<YOUR API KEY>'
$GI_SUITE = '<SUITE ID FROM GHOST INSPECTOR>'
$NGROK_TOKEN = '<NGROK TOKEN>'
$GI_DATA_SOURCE ='<DATASOURCE ID FROM GI>'
$HOST_AND_PORT = '<URL AND PORT TO LAUNCH like localhost:443'
Remove-Item * -Include ngrok.*
# Download and unzip ngrok
$url = "https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-windows-386.zip"
(New-Object System.Net.WebClient).DownloadFile($url, "$(Get-Location)\ngrok.zip")
# Unzip the file to specified location
$shell_app = New-Object -com shell.application
$zip_file = $shell_app.namespace("$(Get-Location)\ngrok.zip")
$destination = $shell_app.namespace("$(Get-Location)")
$destination.Copyhere($zip_file.items())
$ngrok_job_init = Start-Job `
-ScriptBlock {Invoke-Expression -command $args[0] } `
-ArgumentList "$((Get-Location).Path)\ngrok authtoken $NGROK_TOKEN"
# Intialize ngrok and open tunnel to our application
$ngrok_job = Start-Job `
-ScriptBlock {Invoke-Expression -command $args[0] } `
-ArgumentList "$((Get-Location).Path)\ngrok http -host-header=rewrite $HOST_AND_PORT"
# give ngrok a second to register URLs
Start-Sleep -Seconds 5
# Grab the ngrok HTTPS url to send to Ghost Inspector API
$START_URL = (Invoke-RestMethod -Uri "http://localhost:4040/api/tunnels").tunnels[1].public_url
Write-Output "Using start URL: $START_URL"
# Execute Ghost Inspector suite via API and grab the result ID
$EXECUTE_URL = "https://api.ghostinspector.com/v1/suites/$GI_SUITE/execute/?apiKey=$API_KEY&startUrl=$START_URL&immediate=1&dataSource=$GI_DATA_SOURCE"
Write-Output "Executing suite: $EXECUTE_URL"
$RESULT = (Invoke-RestMethod -Uri "$EXECUTE_URL").data
$PASSING_STATUS = 1
# for the suite result, sleep for a few seconds if it hasn't changed
ForEach ($TEST_RESULT in $RESULT)
{
# Set up initial state
$STATUS = $null
$RESULT_ID = $TEST_RESULT._id
while ($STATUS -eq $null) {
Write-Output "Fetching status: https://api.ghostinspector.com/v1/suite-results/$RESULT_ID/?apiKey=$API_KEY"
$STATUS = (Invoke-RestMethod -Uri "https://api.ghostinspector.com/v1/suite-results/$RESULT_ID/?apiKey=$API_KEY").data.passing
Write-Output "Test result ID: $RESULT_ID - status: $STATUS"
Start-Sleep -Seconds 5
}
If ($STATUS -ne 1) {
$PASSING_STATUS = 0
}
}
# status has been updated, check results for "passing"
if ($PASSING_STATUS) {
Write-Output "Suite passed! \o/"
$PASSING = 0
} else {
Write-Output "Suite failed! :("
$PASSING = 1
}
# clean up our jobs
Stop-Job $ngrok_job
Remove-Job $ngrok_job
Stop-Job $ngrok_job_init
Remove-Job $ngrok_job_init
Remove-Item * -Include ngrok.*
# exit with passing status
exit $PASSING
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment