简体   繁体   中英

Karate-config.js - is it possible to have methods of a java class made available for use elsewhere?

To save me having to repeat a line of code (example a) in lots of different files - wherever I want to make use of a Java method, is it possible to move that line of code to the Karate-config.js so that I can make use of the methods attached to it? like in (example b)?

example a (line in feature file that I want to move to karate-config):

* def LdapUtils = Java.type('com.package.something.LdapUtils')

example b (in feature file):

* def newUser = LdapUtils.addNewUser()
* def userId = newUser.uid

Yes this is possible. You can find examples in our test-suites, for example this one: https://github.com/karatelabs/karate/tree/v1.2.1.RC1/karate-core/src/test/java/com/intuit/karate/core/parajava

Personally, I discourage too much of this, we've had problems with Java interop + JS, but the good news is it is mostly resolved. If you go down this path, please use 1.3.0.RC1 or RC2: https://github.com/karatelabs/karate/wiki/1.3.0-Upgrade-Guide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM