Forum Discussion

hrn83au's avatar
hrn83au
Occasional Contributor
10 days ago

call a rest api when a functional test fails

hi,

I need something like this. when an error is received in a step of the function test, I want it to call a rest api. the reason for this is that this rest api will send sms to my cell phone. I could not find how to do it. can you help.

regards...

 

2 Replies

  • hrn83au's avatar
    hrn83au
    Occasional Contributor

    hi,

    We wrote the following groovy script using the TestRunListener.afterrun event, but this script works rest abi whether the test automation is successful or not. What can I do to make it work only when it fails.

     

    import java.io.BufferedReader
    import java.io.InputStreamReader
    import java.net.HttpURLConnection
    import java.net.URL

    def restApiUrl = "URL"

    def url = new URL(restApiUrl)
    def connection = url.openConnection() as HttpURLConnection
    connection.requestMethod = "GET"

    if (connection.responseCode == HttpURLConnection.HTTP_OK) {
        def response = new StringBuffer()
        def reader = new BufferedReader(new InputStreamReader(connection.inputStream))
        String line
        while ((line = reader.readLine()) != null) {
            response.append(line)
        }
        reader.close()
        
     
        println("REST API Yanıtı: " + response.toString())
    } else {
      
        println("REST API çağrısı başarısız: " + connection.responseCode + ", " + connection.responseMessage)
    }connection.disconnect()

  • nmrao's avatar
    nmrao
    Champion Level 3

    hrn83au 

    Simple. Make the following change in the above groovy script.

    • Read the previous test step response, use context.response
    • Add the condition to determine if the test fails.
    • If fails, then call the above code i.e., call your above code conditionally.