Search code examples
testingchatbotbotium-box

How to send an additional request to endpoint after each test case


I’m currently looking at Botium Box, and I’m wondering if it is possible to send an additional request to our endpoint after each test case? Let me give you some background information about how we set up the HTTP(S)/JSON connector in Botium Box and how we are sending information to our bot:

HTTP(S) endpoint: https://MyChatBotsEndpoint.com/?userinput={{msg.messageText}}

HTTP method: POST

We also send cookies through the header template in the request builder. Like this:

{
    "Cookie": "JSESSIONID={{context.sessionId}}"
}

The response is given back in JSON.

When a test ends (when it is successful but also when it fails), we need to send an additional request to our endpoint. The endpoint URL of that request should look like this:

https://MyChatBotsEndpoint.com/endsession

The header should include the cookie as described before.

Is there a way to achieve this in Botium?


Solution

  • Botium has many extension points to plug in your custom functionality. In this case, I guess the SIMPLEREST_STOP_HOOK is the best choice.

    Write a small javascript file calling your endpoint, and register is with the SIMPLEREST_STOP_HOOK capability in botium.json. The context (session context from the HTTP/JSON connector) is part of the hook arguments.

    in botium.json:

    ...
    "SIMPLEREST_STOP_HOOK": "my-stop-hook.js"
    ...
    

    my-stop-hook.js:

    const request = require('request')
    
    module.exports = ({ context }) => {
      return new Promise((resolve, reject) => { 
        request({
          method: 'GET',
          uri: 'https://MyChatBotsEndpoint.com/endsession',
          headers: {
            Cookie: "JSESSIONID=" + context.sessionId
          }
        }, (err) => {
          if (err) reject(err)
          else resolve()
        })
      })
    }