1

I'd like to use the 'faker' library to generate fake data in JSON file as below.

In karate-config.js, I do the following:

var faker = require('faker');

In sample.json:

{
    'firstName': '#(faker.name.firstName)'
    'city'     : '#(faker.address.city)'
}

But I'm getting error like 'unable to find 'require' keyword in 'karate-config.js'

Please help on this.

creimers
  • 4,975
  • 4
  • 33
  • 55
Mr. Venkat
  • 101
  • 1
  • 8

4 Answers4

4

First add the below dependency in your pom.xml

<dependency>
        <groupId>com.github.javafaker</groupId>
        <artifactId>javafaker</artifactId>
        <version>1.0.2</version>
</dependency>

For the latest version of the dependency click here

Use the below code in karate-config.js:

config.faker = Java.type('com.github.javafaker.Faker');

In the feature file use the below code:

* def fakerObj =  new faker()
* def fName = fakerObj.name().firstName()
* def lName = fakerObj.name().lastName()
* def mailId = fName+'.'+lName+'@test.com'

You could use the same in JSON body as follows:

"emailAddress":"#(mailId)",
"firstName":"#(fName)",
"lastName":"#(lName)",
   "address":{
          line1:"#(fakerObj.address().streetAddress())"}

Please click here for the class and methods of faker package

3

Karate does not support NPM or the require keyword. For complex custom logic, the recommendation is to use Java interop.

My suggestion is you should find a Java library that does what "faker" does and integrate it.

EDIT: for those looking for quick ways to integrate with other code, I recommend looking at the CLI option. You can call any OS process via the command-line, and if it returns data (string or JSON ideally) via the system out / err, you can capture it in Karate. So this means, if you can run a node program locally, you can use it from Karate.

Refer: https://stackoverflow.com/a/62911366/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • thanks for your response and suggestion, tried below and working fine. in karate-config.js: var faker = Java.type('.FakerClass'); ...... config.faker = faker; in sample.json: > { 'name': '#(faker.address.city)' } – Mr. Venkat Aug 31 '18 at 12:47
  • Are there plans for npm support in karate? – Pavel Biryukov Jul 21 '21 at 10:30
  • 1
    @PavelBiryukov it is not a priority for us. we use Graal as the JS engine and it technically can run Node.JS - so if the community tries to experiment, who knows :) – Peter Thomas Jul 21 '21 at 11:21
2

As far as I know, karate only supports java based dependencies. So try to find a Java equivalent for your JS library.

yek
  • 127
  • 1
  • 7
1

thanks for your response and suggestion, tried below and working fine.

in karate-config.js:

var faker = Java.type('.FakerClass');

......

config.faker = faker;

in sample.json:

{ 'name': '#(faker.address.city)' }

Community
  • 1
  • 1
Mr. Venkat
  • 101
  • 1
  • 8
  • How did you make use of faker 'npm' library inside karate project ? How did you manage to install faker dependency into your karate project ? – Vinod Baradwaj Jan 22 '20 at 06:59