Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting `org.graalvm.polyglot.PolyglotException: ReferenceError:` - after upgrading to karate 1.1.0.RC3 from karate 0.9.5 [duplicate]

Tags:

karate

Description : Getting reference error message after upgrading to karate 1.1.0.RC3

Steps: Execute the test.feature file

test.feature

  Scenario: Get custom request
    * def testUtils = call read('classpath:features/utils.feature')
    * def getRequest = testUtils.customRequest()
    * print getRequest 

Utils.feature

Feature: common utilities

  Scenario: 
    * def randomAlphabetic = function (count) { return org.apache.commons.lang3.RandomStringUtils.randomAlphabetic(count) }

    * def customRequest =
    """
     function() {
     var name = randomAlphabetic(2)
     return {
         name:name,
         city:'Bangalore'
     }
  }
    """

Error message :

org.graalvm.polyglot.PolyglotException: ReferenceError: "randomAlphabetic" is not defined
- <js>.:anonymous(Unnamed:2)

Git repo for reference - https://github.com/naveenkrao/karate-sample-project

like image 533
naveenkrao Avatar asked Oct 20 '25 09:10

naveenkrao


1 Answers

There are some limits to re-use of JS functions in the new versions. There are multiple work-arounds. Here is one below, to move the re-usable function within the JS block itself:

* def customRequest =
"""
 function() {
 var randomAlphabetic = function (count) { return java.lang.System.currentTimeMillis() + '' };
 var name = randomAlphabetic(2);
 return {
     name:name,
     city:'Bangalore'
 }
}
"""

Or if you make the randomAlphabetic function globally available e.g. via karate-config.js it may also work.

This is a consequence of the JS engine change which we tried hard to address. You are most welcome to contribute code if this is a problem for you. We think not, there are many ways to create re-usable functions, follow some rules - keep them simple, prefer Java for complex logic, and don't mix or compose JS or Java too much.

You are also welcome to contribute documentation if you feel that it needs to be updated.

like image 64
Peter Thomas Avatar answered Oct 22 '25 06:10

Peter Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!