Powershell Script

1. Generate the API key from Configurations > API Keys

2. Get the Test Plan ID from Test Plan details page

3. Replace those values in the below script(TESTSIGMA_API_KEY, TESTSIGMA_TEST_PLAN_ID) and paste it in Jenkins Pipeline as a new Stage


The Script triggers the executions and also waits until the Timeout(MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT) for the execution to complete.

###################################################################################
#TESTSIGMA_API_KEY -> API key generated in Testsigma App under Configurations > API Keys

#TESTSIGMA_TEST_PLAN_ID -> Testsigma Testplan ID. You can get this ID from Testsigma App in Test Plans > TEST_PLAN_NAME > CI/CD Integration Tab

#MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT -> Maximum time the script will wait for Test Plan execution to complete.
The sctript will exit if the script execution exceeeds this mentioned Maximum time.
However, the Test Plan will continue to run in Testsigma. You can check test results by logging to Testsigma later once you get the Execution passed or failed email/Slack/Teams notification.

#REPORT_FILE_PATH -> File path to save report Ex: <DIR_PATH>/report.xml, ./report.xml, C:\report.xml

#$RUN_TIME_PARAMS -> Here, you can pass Runtime parameters such as Deployment URL to the Test Plan
@{key1="$env:buildURL";key2="value2"}
####################################################################################



<####### START USER INPUTS ######>

$TESTSIGMA_API_KEY="eyJhbGciOiJIUzUxMiJSYG50bdaDnenFtcqYOxsEbgw16EY0dFTshN"

$TESTSIGMA_TEST_PLAN_ID="3058"

$REPORT_FILE_PATH="./junit-report.xml"

$MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT=180

$RUN_TIME_PARAMS=@{}

<####### END USER INPUTS ########>

<#### Please do not change the values below this if you are not sure what you are doing. ####>

$TESTSIGMA_TEST_PLAN_REST_URL="https://app.testsigma.com/api/v1/execution_results"
$TESTSIGMA_JUNIT_REPORT_URL="https://app.testsigma.com/api/v1/reports/junit"

$POLL_INTERVAL_FOR_RUN_STATUS=5
$NO_OF_POLLS=($MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT/$POLL_INTERVAL_FOR_RUN_STATUS)
$SLEEP_TIME=($POLL_INTERVAL_FOR_RUN_STATUS * 60)
$global:LOG_CONTENT=""
$global:APP_URL=""
$global:EXECUTION_STATUS=-1
$RUN_ID=""
$global:IS_TEST_RUN_COMPLETED=-1
$PSDefaultParameterValues['Invoke-RestMethod:SkipHeaderValidation'] = $true
$PSDefaultParameterValues['Invoke-WebRequest:SkipHeaderValidation'] = $true
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}" -f $TESTSIGMA_API_KEY)))
function get_status{
    $global:RUN_RESPONSE=Invoke-RestMethod  $status_URL -Method GET -Headers @{Authorization=("Bearer {0}" -f $TESTSIGMA_API_KEY);'Accept'='application/json'} -ContentType "application/json"

    $global:EXECUTION_STATUS=$RUN_RESPONSE.status
    $global:APP_URL=$RUN_RESPONSE.app_url
    Write-Host "Execution Status: $EXECUTION_STATUS"

}
function checkTestPlanRunStatus{
  $global:IS_TEST_RUN_COMPLETED=0
  for($i=0; $i -le $NO_OF_POLLS;$i++){
    get_status
    Write-Host "Execution Status before going for wait: $EXECUTION_STATUS ,Status_message:"($RUN_RESPONSE.message)
    if ($EXECUTION_STATUS -eq "STATUS_IN_PROGRESS"){
      Write-Host "Sleep/Wait for $SLEEP_TIME seconds before next poll....."
      sleep $SLEEP_TIME
    }else{
      $global:IS_TEST_RUN_COMPLETED=1
      Write-Host "Automated Tests Execution completed...`nTotal script execution time:$(($i)*$SLEEP_TIME/60) minutes"
      break
    }
  }
}

function saveFinalResponseToAFile{
  if ($IS_TEST_RUN_COMPLETED -eq 0){
      $global:LOG_CONTENT="Wait time exceeded specified maximum time(MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT). Please visit below URL for Test Plan Run status.$APP_URL"
     Write-Host "LogContent:$LOG_CONTENT nResponse content:"($RUN_RESPONSE | ConvertTo-Json -Compress)

	 }
	 else{
	 Write-Host "Fetching reports:$TESTSIGMA_JUNIT_REPORT_URL/$RUN_ID"
	 $REPORT_DATA=Invoke-RestMethod  $TESTSIGMA_JUNIT_REPORT_URL/$RUN_ID -Method GET -Headers @{Authorization=("Bearer {0}" -f $TESTSIGMA_API_KEY);'Accept'='application/xml'} -ContentType "application/json"
	 Write-Host "report data: $REPORT_DATA"

	# Add-Content -Path $REPORT_FILE_PATH -Value ($REPORT_DATA)
	$REPORT_DATA.OuterXml | Out-File $REPORT_FILE_PATH
		}
    Write-Host "Reports File::$REPORT_FILE_PATH"

}

Write-Host "No of polls: $NO_OF_POLLS"
Write-Host "Polling Interval:$SLEEP_TIME"
Write-Host "Junit report file path: $REPORT_FILE_PATH"

$REQUEST_BODY_TABLE=@{executionId="$TESTSIGMA_TEST_PLAN_ID"}
$REQUEST_BODY_TABLE.Add("runtimeData",$RUN_TIME_PARAMS)

$REQUEST_BODY = $REQUEST_BODY_TABLE | ConvertTo-Json -Compress
Write-Host "Json payload" $REQUEST_BODY
try{
$TRIGGER_RESPONSE=Invoke-RestMethod -Method POST -Headers @{Authorization=("Bearer {0}" -f $TESTSIGMA_API_KEY);'Accept'='application/json'} -ContentType 'application/json' -Body $REQUEST_BODY -uri $TESTSIGMA_TEST_PLAN_REST_URL
}catch{

 Write-Host "Code:" $_.Exception.Response.StatusCode.value__
 Write-Host "Description:" $_.Exception.Response.StatusDescription
 Write-Host "Error encountered in executing a test plan. Please check if the test plan is already in running state."
 exit 1
}

$RUN_ID=$TRIGGER_RESPONSE.id
Write-Host "Execution triggered RunID: $RUN_ID"
$status_URL = "$TESTSIGMA_TEST_PLAN_REST_URL/$RUN_ID"
Write-Host  $status_URL

checkTestPlanRunStatus
saveFinalResponseToAFile




Unix Shell Script(Bash Script)

Replace the Username, Password, & Testsigma Plan URL in the below script and paste it in Jenkins Pipeline as a new Stage


The Script triggers the executions and also waits until the Timeout(MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT) for the execution to complete.

#!/bin/bash

#********START USER_INPUTS ************************************************
#UserID/E-mail with which you log-in to testsigma(https://app.testsigma.com/)
TESTSIGMA_USER_NAME=<Testsigma username>

#Testsigma user password
TESTSIGMA_PASSWORD=<Testsigma password>

# Testsigma URL to trigger a Test Plan. You can get this URL from Testsigma's Test Plan page. 
# For example, http://app.testsigma.com/rest/execution/49/run
TESTSIGMA_TEST_PLAN_RUN_URL=https://app.testsigma.com/rest/execution/<execution_id>/run

# Maximum time in minutes the script will wait for TEST Plan execution to complete. 
# The script will exit if the Maximum time is exceeded but the Test Plan will continue to run in Testsigma. 
# In that case, you can check test results later by logging to Testsigma.
MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT=180

# The JSON report data will be copied to a file under this directory. FileName: testsigma.json
JSON_OUTPUT_REPORTS_DIR=~/Documents/ts_reports
#********END USER_INPUTS*****************************************************

##########GLOBAL variables#########
# Time in minutes the script will wait between the polls Testsigma to check if the execution is complete.
POLL_INTERVAL_FOR_RUN_STATUS=3
NO_OF_POLLS=$((MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT/POLL_INTERVAL_FOR_RUN_STATUS))
SLEEP_TIME=$((POLL_INTERVAL_FOR_RUN_STATUS * 60))

# The generated Test Reports file name
JSON_FILE_NAME=testsigma.json
REPORT_FILE_NAME=$JSON_OUTPUT_REPORTS_DIR/$JSON_FILE_NAME
LOG_CONTENT=""
####################################

get_status(){

    RUN_RESPONSE=$(curl -u $TESTSIGMA_USER_NAME:$TESTSIGMA_PASSWORD --silent --write-out "HTTPSTATUS:%{http_code}" -X GET $TESTSIGMA_TEST_PLAN_RUN_URL/$HTTP_BODY/status)

    echo "Status_Response_Complete: $RUN_RESPONSE "
    # extract the body
    RUN_BODY=$(echo $RUN_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
    # extract the status
    RUN_STATUS=$(echo $RUN_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
    echo "Response_Status: $RUN_STATUS"
    # print the body
    echo "Run Status..."
    echo "$RUN_BODY"
    EXECUTION_STATUS=$(echo $RUN_BODY | getJsonValue status)

    echo "Execution Status: $EXECUTION_STATUS"


}

function checkTestPlanRunStatus(){
  IS_TEST_RUN_COMPLETED=0
  for((i=0; i<= NO_OF_POLLS;i++))
  do
    get_status
    if [ $EXECUTION_STATUS -eq 2 ]
     then
      echo "Sleep/Wait for $SLEEP_TIME seconds before next poll....."
      sleep $SLEEP_TIME

    else
      IS_TEST_RUN_COMPLETED=1
      echo "Automated Tests Execution completed...\n total script execution time:$(((i+1)*SLEEP_TIME/60)) minutes"
      break
    fi
  done


}

function saveFinalResponseToAFile(){
    if [ $IS_TEST_RUN_COMPLETED -eq 0 ]
     then
      LOG_CONTENT="Wait time exceeded specified maximum time(MAX_WAIT_TIME_FOR_SCRIPT_TO_EXIT). Please log-in to Testsigma to check Test Plan run status.
      You can visit the URL specified in \"app_url\" JSON param For landing in Test Plan run page directly.
      Ex: \"app_url\":\"https://dev.testsigma.com/#/projects/31/applications/53/version/72/report/executions/197/runs/819/environments\""

    fi
    echo "Reports dir::$REPORT_FILE_NAME"
    echo "$LOG_CONTENT \n $RUN_BODY"
    echo "$RUN_BODY" >> $REPORT_FILE_NAME
}

function getJsonValue() {
    json_key=$1
    awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/\042'$json_key'\042/){print $(i+1)}}}' | tr -d '"'
}

echo "************Testsigma:Start executing automated tests ... ************"

echo "NO of polls $NO_OF_POLLS"

# store the whole response with the status at the and
HTTP_RESPONSE=$(curl -u $TESTSIGMA_USER_NAME:$TESTSIGMA_PASSWORD --silent --write-out "HTTPSTATUS:%{http_code}" -X POST $TESTSIGMA_TEST_PLAN_RUN_URL)

# extract the body
echo "HTTP_RESPONSE:"
echo
echo $HTTP_RESPONSE
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')

# extract the status
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')

# print the body
echo "Run_ID:$HTTP_BODY"

# example using the status
if [ ! $HTTP_STATUS -eq 200  ]; then
  echo "Failed to execute automated tests!!"
  echo "Error [HTTP status: $HTTP_STATUS]"
  exit 1 #Exit with a failure.
else
  checkTestPlanRunStatus
  saveFinalResponseToAFile
fi

echo "Final response: $RUN_BODY"
echo "************Testsigma:Completed executing automated tests ... ************"


Feel free to contact Support if you need more clarification.

Welcome to Testsigma!