Does anybody use Karate DSL as a test data management tool? - api

i am glad with the use of Karate DSL for API testing. But i was wondering if it is suited for test data generation. Some of our UI tests need some particular data load which I think could be generated by calling the API (Rest and SOAP) through Karate DSL.
Is Karate adequate for this or would you use other specific data generation tool?
Thanks in advance for your help.

I suggest you start looking at the new RC version (0.9.9.RC3) you can use a function to generate data: https://github.com/intuit/karate/tree/develop#json-function-data-source
* def generator = function(i){ if (i == 20) return null; return { name: 'cat' + i, age: i } }

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-config.js, Is it possible to run java method after every karate scenario?

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);

Using mocks in Karate DSL feature file with stanalone run

I have REST service, written in language different from Java.
It have few dependencies from other REST services.
For example service under development and testing is A, other services are respectively B and C.
I want to run system test for A, some tests require B or/and C to be online and perform queries from A.
I wrote b-mock.featue and c-mock.feature to represent that services in mock.
Also I wrote some a-test-smth.feature files to run test against A
Is it possible to add some information into a-test-smth.feature to enable some mocks for concrete test?
Now I should run standalone karate.jar twice, first - for mocking. second - for run tests. That approach works, but, I can't ceck that:
some API calls to A not required B or C
can't emulate service B down or for example slow or incorrect response answer fetching
Thanks.
Are you using Java ? If so then the best approach is to perform the set-up of your test in Java code. You can start 2 mocks for B and c and then start the main test for your service A. And at the end do clean-up if needed.
You can refer this as an example: https://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example
Row 3 shows how you can start a mock and run a Karate test.
If you are not using Java and would like to use only the stand-alone JAR, it is actually possible using Java-interop and quite easy, I just tried it.
EDIT: This API is now built into Karate, so you don't need to write the extra JS code below: https://github.com/intuit/karate/tree/master/karate-netty#within-a-karate-test
(Obsolete)
First create this bit of JavaScript code that is smart enough to start a Karate mock:
function() {
var Mock = Java.type('com.intuit.karate.netty.FeatureServer');
var file = new java.io.File('src/test/java/mock/web/cats-mock.feature');
var server = Mock.start(file, 0, false, null);
return server.port;
}
And this is how it can look in the Background of your main Karate test. You can see how you can do some conditional logic if needed and you have plenty of ways to change things based on your environment.
Background:
* def starter = read('start-mock.js')
* def port = karate.env == 'mock' ? starter() : 8080
* url 'http://localhost:' + port + '/cats'
Does this answer your question ? Let me know and I will add this trick to the documentation !

Need one example deno on how to use karate scripts for peformance testing using gatling from scratch

I am very new to perf testing, i went through the sample project created using karate scripts in gatling, but unable to understand how to do it,
Can anyone please provide explanation on how to use karate for performance testing using gatling by using some public api like below
Scenario: Get State specific information - one state
Given url 'http://services.groupkt.com/state/get/IND/AP'
When method get
Then status 200
* def resp = response.RestResponse.result.name
* print resp
so that we can use it in our project. unable to understand the current demo project available in github karate
You just have to Git-clone and run (using Maven) this simple, stand-alone project: https://github.com/ptrthomas/karate-gatling-demo
Take the help of someone who knows their way around a Maven project if needed.
Once you get this running, you will be able to understand and modify this in no time.
package mock
import com.intuit.karate.gatling.PreDef._
import io.gatling.core.Predef._
import scala.concurrent.duration._
class CatsSimulation extends Simulation {
val protocol = karateProtocol(
"/cats/{id}" -> Nil,
"/cats" -> Nil
)
val create = scenario("create").exec(karateFeature("classpath:mock/cats-create.feature"))
val delete = scenario("delete").exec(karateFeature("classpath:mock/cats-delete.feature"))
setUp(
create.inject(rampUsers(10) over (5 seconds)).protocols(protocol),
delete.inject(rampUsers(5) over (5 seconds)).protocols(protocol)
)
}

Process synchronization for web application automation testing

We are using selenium to do automation on our web application. We have knowledge of CUIT.In CUIT we have process synchronization like WaitForReadyLevel.AllThreads,WaitForReadyLevel.UIThread.I want to know is there any way to achieve similar functionality using selenium?
I need some process synchronization after every step in my script for web application or is there any external APIs to achieve this.
Regards,
Raj.
We are using this process in our automation scripts to check whether process is busy or not.Hope this piece of code gives you a idea for your requirement.
bool isSync = true;
while (isSync)
{
using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time",ProcessName))
{
pcProcess.NextValue();
Thread.Sleep(100);
if (!IsProcessRuning()) return rc;
double pct = pcProcess.NextValue();
if (pct == 0.0)
isSync = false;
}
}
Regards,
Nagasree.