Search code examples
javatestingautomated-testskarate

Karate dropping the driver after invoking a Java function that spins up a new Driver object


In my attempt to keep two browser sessions open, I've resorted to scripting a Java function to use the Karate Java Driver object and spin up the secondary session. That is working as expected, however when the call is finished and the next step in the feature file is being executed I get "driver not defined" errors.

Java method:

public static void completeBuyerBssInNewSession(Map<String, Object> params) {
    
        Driver driver = Driver.start("chrome");
        driver.setUrl((String) params.get("arg1"));
    }

Here is the call being made:

* def newSession =
  """
  function(args) {
     var BuyerNewSession = Java.type('utils.util.BuyerNewSessionBss');
     return BuyerNewSession.completeBuyerBssInNewSession(args);
  }
  * def result = callonce newSession {'arg1': #(arg1), 'arg2': #(arg2)}

Error received:

org.graalvm.polyglot.PolyglotException: ReferenceError: "driver" is not defined

Solution

  • Refer this answer for details (see the links also): https://stackoverflow.com/a/60387907/143475

    Karate does auto-kill the browser on a Scenario exit. We are thinking of improving this for the future, so feel free to open an issue to discuss.

    For now maybe you have to use 2 instances of your Java helper.

    EDIT: also make sure you have read this part of the docs, this can be a reason why you don't see the driver in some cases: https://github.com/karatelabs/karate/tree/master/karate-core#javascript-function-reuse