Sandeep P
Sandeep P

Reputation: 109

How to configure karate afterScenario in karate-config.js file

I am using Karate '* configure afterScenario = ' in feature file and getting results successfully and able to have DB calls as well. How to implement same in Karate-config.js?

Note: I have a Java class call as well.

Any implementation like this?

Code using in feature file

Background:
* def inFluxDb = Java.type('aPITests.InfluxDBCall');
* def dbCDB = new inFluxDb();
* configure afterScenario = 
"""
function(){
  var info = karate.info; 
  karate.log('after', info.scenarioType + ':', info.scenarioName);
  karate.log('after', info.scenarioType + ':', info.errorMessage);
  karate.log('after', info.scenarioType + ':', info.featureDir);
  karate.log('after', info.scenarioType + ':', info.featureFileName);
  karate.log('after', info.scenarioType + ':', info.scenarioDescription);
  var featurenameupdate = info.featureFileName.substring(0, info.featureFileName.length-8);
  var status="Fail";
  if(!info.errorMessage){
  status="Pass";
  }
  dbCDB.DBConnection( "http://localhost:8086", "root", "root");
  dbCDB.DBwrite( featurenameupdate, info.scenarioName, info.errorMessage, status );
  dbCDB.connectionClose();
}
"""

Update: Concern after using karate.configure

Scenario: I have two feature files

Feature1: API) contains @smoke @test tags

Feature2: UI) contains @dropdown @Angular

My * def configure afterScenariois declared into Feature2 file While executing only @smoke test, my karate.configure('afterScenario', 'UI.feature'); is not loading.

is this as expected or else anything need to be updated from my end?

Upvotes: 1

Views: 2764

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Use the karate.configure() API to move any feature configure step into the JS config.

karate.configure('afterScenario', read('some.feature'));

I don't recommend "bloating" your karate-config.js unless absolutely necessary, because it is called for every Scenario or Examples row. Consider using a ExecutionHook instead.

Upvotes: 1

Related Questions