Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send scenario result in after scenario hook to external endpoint #1381

Closed
stefdaems opened this issue Nov 21, 2020 · 3 comments
Closed

Send scenario result in after scenario hook to external endpoint #1381

stefdaems opened this issue Nov 21, 2020 · 3 comments
Labels

Comments

@stefdaems
Copy link

stefdaems commented Nov 21, 2020

I want to be able to send the scenario result (testName + status) to an external endpoint, right after the execution of the scenario.

My karate-config looks like this:

function fn() {   
  var config = { // base config JSON
    testPusher: function(testResult) {
      var url = 'http://localhost:5000';

      var data = JSON.stringify(testResult);

      fetch(url, {
        headers: { "Content-Type": "application/json; charset=utf-8" },
        method: 'POST',
        body: data
      })
    }
  };
  return config;
}

In de Background part of my feature I've added

Background:
  * configure afterScenario = 
    """
    function(){
      var info = karate.info; 
      karate.log('after', info.scenarioType + ':', info.scenarioName);
      var status="Fail";
      if(!info.errorMessage){
        status="Pass";
      };
      var testResult = {
        testName: info.scenarioName,
        result: status
      }
      testPusher(testResult)
    }
    """

Fetch seems to be unknown to karate, so is there any possibility to add fetch (or node-fetch or axios...) to the modules which can be used.

If I'm totally missing out on the way of doing it, what will be the best approach to send me the result of each scenario in the afterscenario hook?

@ptrthomas
Copy link
Member

@stefdaems answered on stack overflow. also keep in mind that karate is an http client so you don't need fetch and what not. also see this thread for other ideas: #619

@stefdaems
Copy link
Author

Thanks for the quick response @ptrthomas but this still kept me a bit in the dark. The docs referred to aren't shining much light on my question.

Does this indicate I don't need to use the afterscenario any longer and look for the ExecutionHook to be implemented somewhere, somehow?

I expect the need to implement something like Kafka.post(url, body, header)....

@ptrthomas
Copy link
Member

@stefdaems you can use any karate feature in afterScenario, here is an example: https://stackoverflow.com/a/51467470/143475 - and karate can make http calls

there are more examples in the karate-demo

if that doesn't work, then go for hooks

you can't use npm / random js libraries in karate

if you read the other thread, you may find that it may be much more convenient to send all results at the end of your tests rather than complicating things and having brittle moving parts inside your tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants