In this post we will see how to add an configuration file (karate-config.js) to our sample karate project. This configuration file is similar to the properties file that you would have used for other automation projects. I'll be using the Karate-Zip release version for this post too. Still unaware about it, check out my last post.
When using the config file we need to understand a few basics about it:
- When Karate starts-up it expects a file called karate-config.js in the classpath
- This config file should be a Javascript function.
- The function is expected to return a json object
- All Keys and values in the json object will be made available as script variable
So com.intuit.karate.Main is the entry point for the Karate command line app
And . (period) indicates the current directory
Here we see that the current directory is also added to the classpath.
Karate-config.js, how and where - Steps?
Lets address the where part first.
In the root of your project, create a new file and name it as karate-config.js
Now open the file and add below code
What does the code do? How to use karate-config.js?
We have added a Key called myName with value Kalimoh, we will use this in our API sample feature file and try to print it. This will help check if the configuration file is setup correctly.
So, lets open file users.feature and modify it to add it as shown here
We have added the print statement in the Background section which utilizes the variable created previously.
Now while being in the root of your project, run your test with command karate D:\karate\karate-0.9.6\src\demo\api
Below report is generated and we can see that the correct value got printed in both scenarios.
Now that we know that the configuration is setup correctly, lets try to get the URL used in the Background section of the feature file from the config file.
Anything used in the background would be run for each of the scenarios in the feature file. So we should be able to use the variable from Config file for both scenarios.
Lets modify the karate-configuration.js file and add the URL to it as shown below
Now once again lets modify the users.feature file to use the usersUrl variable (created in the config file) in the Background section and second Scenario as shown below
Run your test with command karate D:\karate\karate-0.9.6\src\demo\api
In the report we see that everything has passed. All 3 steps where URL was required for the test it was setup correctly.
Next Steps:
Try to use the config for the sample UI automation test and let me know how that worked out.
References:
Comments
Post a Comment