As you may already know, Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.


We can use the Jenkins third-party plugin in Testsigma to automate a Test Execution when a successful build is triggered in Jenkins server. This is done by using the curl linux utility to send POST requests to Testsigma API.


Steps to add the execution step in Jenkins pipeline


1. Go to the specific Execution details page in Testsigma tool for the Execution that you want to include in your Jenkins Pipeline. The CURL request format for starting a Test Execution can be found on the Execution details page under the CI Integration Details as shown below:



2. Replace the <Username> and <Password> part with your Testsigma login credentials to get the CURL request format for the corresponding execution.


3. Once you get the request format, you simply need to add a script in your Jenkins Tool to execute the CURL request which is triggered by a successful build.



Jenkins Script format


Simple Method


Add a step in the Jenkins pipeline to run the following bash script which starts the Test Execution remotely.


curl -X POST -H 'Content-type: application/json' -u <Username>:<Password> http://app.testsigma.com/rest/execution/229/run


Advanced Method


If you want to start the Execution and then check the execution status in regular intervals you can make use of a more advanced script such as the once given below:



#https://github.com/trentm/json
#https://gist.github.com/maxcnunes/9f77afdc32df354883df

get_status(){

    RUN_RESPONSE=$(curl -u <YOUR_USERNAME>:<YOUR_PASSWORD> --silent --write-out "HTTPSTATUS:%{http_code}" -X GET $URL/$HTTP_BODY/status)
    # 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://')

    # print the body
    echo "Run Status..."
    echo "$RUN_BODY"

    EXECUTION_STATUS=$(echo $RUN_BODY | json status)

    echo "Execution Status $EXECUTION_STATUS"
    echo $RUN_BODY | json status_text

    if [ $EXECUTION_STATUS -eq 2 ]; then
      sleep 120
      get_status
    else
      echo "Automated Tests Execution completed..."
    fi

}


echo "Start executing automated tests ... "

URL='<YOUR_EXECUTION_URL>'
#example: URL='http://app.testsigma.com/rest/execution/49/run'


# store the whole response with the status at the and
HTTP_RESPONSE=$(curl -u <YOUR_USERNAME>:<YOUR_PASSWORD> --silent --write-out "HTTPSTATUS:%{http_code}" -X POST $URL)

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

# example using the status
if [ ! $HTTP_STATUS -eq 200  ]; then

  echo "Failed to executed automated tests!!"
  echo "Error [HTTP status: $HTTP_STATUS]"
  exit 1 #Exit with a failure.
else
  #Poll to check the execution status
  poll=true
  while $poll
  do

    get_status
    #sleep 120 #Wait for 120 seconds to check execution status again.
    poll=false #Mark poll to false to stop polling and exit
  done

fi


echo "Completed executing automated tests!!!"



You need to replace the <YOUR_USERNAME>, <YOUR_PASSWORD> and <YOUR_EXECUTION_URL> with your Testsigma account username, password and Execution URL obtained from the corresponding Execution page respectively.



That's all we need to automate Test Execution when a successful build is triggered using the Jenkins CI server.


Happy Automation Testing!