Karate-config.js, Is it possible to run java method after every karate scenario? - karate

I found in the karate docs that the java method can be run like this:
* def JavaDemo = Java.type('com.app.DBUtils').prepareData(arg1, arg2)
I created karate-config.js file where I stored environment variables. Now I need to run some java methods after every scenario, but just for some environments. So I have there some conditions.
But I didn't find a way to run a java method from karate-config.js after every scenario. Is it possible?

Yes if you wrap it in JS or a Feature: https://github.com/intuit/karate#hooks
var fun = function(){ var MyClass = Java.type('com.myco.MyClass'); MyClass.doWork() }
karate.configure('afterScenario', fun);

Related

How to Feature file keywords are wired to java Code in Karate?

I have recently started using Karate frame work for my API tests cases. It was great. But I was wondering how the feature files are getting parsed ? How to wiring is done in Karate ?
* def handler = function(msg){ return msg.startsWith('hello') }
* def socket = karate.webSocket(demoBaseUrl + '/websocket', handler)
* socket.send('Billie')
* def result = socket.listen(5000)
* match result == 'hello Billie !'
In the above code.. "Karate.websocket" is calling which method in framework ?
Karate uses a JVM-based JavaScript engine that makes calling any Java code on the class-path very easy.
To answer your question, the webSocket() method is in the ScenarioBridge class. This class is injected into the feature file with the name karate at run-time.

karate| env specific config file

karate framework| I am trying to create multiple karate-config.js file for different env like 'test', 'stage' can somebody help me with an example how I can call env specific config values from different karate config file.
I refer this https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/karate-config-contract.js
but doesn't clarify what exactly need to do for calling different config.
This part of the karate documentation explains how to check the karate.env property in your karate-config.js in order to set configurations and variables depending on your environment.
An other way to archive different configurations per environment is explained here: environment-specific-config
All these approaches solves the issue, to configure different urls for example in your test cases.
Hence, there is no need to call or check for the environment in your feature file in order to get different configuration values. It's done by karate. Just refer to the variables you assigned in karate-config.js.
You just do something like:
Background:
* url baseUrls.userSystem
Where your karate-config.js could look like:
function fn() {
if (!env) {
env = 'local';
}
var config = {
baseUrls : {
userSystem : "http://localhost"
}
}
if (env === 'dev') {
config.baseUrls.userSystem = "http://usersystem.default.svc"
}
return config
}
The approach above demonstrate how to use just one karate-config.js for all enviroments. One file for all.
If you want to create a karate-config-<env>.js per enviroment, follow the environment-specific-config documentation.
You will find here https://github.com/intuit/karate/tree/master/karate-demo/src/test/java a default karate-config.js that will be evaluated for every environment. The karate-config-contract.js will only be evaluated after the karate-config.js file has been evaluated if and only if the karate.env property is contract.
Please read the karate documentation. Peter did a great job to document nearly every little feature karate offers.

Create and run codeception tests from PHP

I know that Codeception is designed for command line usage. But as it is completely based on PHP, I am pretty sure there must be a way to dynamically/temporarily create a test by PHP.
In my case I am getting acceptance test steps from a database and need to run the tests dynamically with Codeception. I would prefer a way to test it without always having to generate and delete temporary test folders and running the codeception commands on the commandline.
The problem is that Codeception dynamically generates a bunch of config files and scripts when creating a cest. I couldn't make it work by using the Codeception classes.
Does anyone have an idea what's the best way to achieve this?
I think that the best approach would be to implement custom test loader as documented at https://codeception.com/docs/07-AdvancedUsage#Formats
You still have to use placeholder file in each suite to kickoff the loader, but the tests can be loaded from database.
Copy of documentation:
In addition to the standard test formats (Cept, Cest, Unit, Gherkin)
you can implement your own format classes to customise your test
execution. Specify these in your suite configuration:
formats:
- \My\Namespace\MyFormat
Then define a class which implements the LoaderInterface
namespace My\Namespace;
class MyFormat implements \Codeception\Test\Loader\LoaderInterface
{
protected $tests;
protected $settings;
public function __construct($settings = [])
{
//These are the suite settings
$this->settings = $settings;
}
public function loadTests($filename)
{
//Load file and create tests
}
public function getTests()
{
return $this->tests;
}
public function getPattern()
{
return '~Myformat\.php$~';
}
}
Look at existing Loader classes for inspiration: https://github.com/Codeception/Codeception/tree/4.0/src/Codeception/Test/Loader

How to parameterise the cucumber options tags from jenkins in karate

I have below java class which runs with cucumberOptions
#CucumberOptions(tags = {"#userManagement"})
public class IC_API_Tests_Runner {
runner code here
}
From jenkins I am passing below command ti run the tests
clean test "-Dkarate.env=$WhereToRun" "-Dbvt.tags=#userManagement"
I am able to fetch the value of 'bvt.tags' using below command
bvtTags = karate.properties['bvt.tags'];
Now I need to pass the 'bvtTags' value to the CucumberOptions.
I tried
#CucumberOptions(tags = {"bvtTags"})
public class IC_API_Tests_Runner {
runner code here
}
But 'bvtTags' value is not substituted in the CucumberOptions. But I am able to print the value of 'bvtTags' with print statement in karate code.
Any help will be great help
No you can't do dynamic changing of the #CucumberOptions like that.
Use the API for dynamically choosing tests, see this example: DemoTestSelected.java.
Then do something like this (please change for your environment):
String tags = System.getProperty("bvt.tags");
List<String> tags = Arrays.asList(tags);
EDIT: actually you don't need to do any of this. (I guess that you will never read the docs :)
Please refer: https://github.com/intuit/karate#command-line
-Dkarate.options="--tags #userManagement"

How can I target a test in the _before() hook

I have a Codeception cest file which has a number of tests in it.
In some of the tests, there are initializations which I would like to so in the _before() hook. These initializations are specific to those tests only and to no other test in the cest file.
How can I go about this?
The pseudocode would be something like
public _before($event)
{
if ($event->test_being_run == 'testThatFeature')
{
$init = something(here);
}
}
Through investigation, I have realized that the $event variable passed into the _before() hook is an instance of the generated AcceptanceTester; as opposed to \Codeception\Event\TestCase. So I cannot use the hopeful $event->getTest()->getTestFullName().
Codeception injects parameters based on type hinting.
If you want to get \Codeception\Event\TestCase, your code must look like this:
public _before(\Codeception\Event\TestCase $event)
All information that I found about receiving testcase in _before method, was about _before method of modules and extensions, it does not apply to tests.
If you want to run specific code for one test, just run it in the test code.