1

I am currently working on a PoC of the Karate network and for some small test cases (both automated API tests and automated UI tests) it seems to be working very nice. However, I seem to have a problem with the callSingle() method - but only in UI tests. I read in the documentation that Karate closes a browser instance after every scenario, unless the driver is called upon before the scenario has been entered. So I tried doing this via callSingle() and put in in the karate-config.js file, like so (also tried var result = karate.callSingle('driver.feature', config) and the combo without the config as argument):

function fn() {
  var env = karate.env; // get system property 'karate.env'
  karate.log('karate.env system property was:', env);
  if (!env) {
    env = 'dev';
  }
  var config = {
    env: env,
    myVarName: 'someValue'
  }
  if (env == 'dev') {
    // customize
    // e.g. config.foo = 'bar';
  } else if (env == 'e2e') {
    // customize
  }
   
    karate.callSingle('driver.feature', config);
    return config;
}

where the driver.feature file looks like (also tried to put the configuration in the background):

Feature: setup-driver

  Background:


  Scenario:
    Given configure driver = { type: 'chrome', headless: false }
    And driver 'https://www.google.com'

and the upper feature file that needs to use the browser looks something like this (I made a very straight-forward feature file, stripped to the bare necessities):

Feature: Navigate to the site and do stuff

  Background:

    Scenario: screenshot 1
      Then screenshot()

    Scenario: screenshot 2
      Then screenshot()

When I try to run this feature, the driver starts the way it should, but the moment the scenario (both of them) is entered, the test fail because of the following error:

BadVla.feature:6 - evaluation (js) failed: screenshot(), javax.script.ScriptException: ReferenceError: "screenshot" is not defined in <eval> at line number 1
stack trace: jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:477)

So it seems the scenario that uses the driver does not seem to be able to do anything with/in it (I used screenshot() as an example, but it does not with any method that should work).

I am working with

  • IntelliJ 2019.3.1 Community edition
  • Java version 1.8
  • Karate version 0.9.6

Maybe this method simply does not work for UI tests, and then so be it. But if somebody already encountered this issue and/or know how to solve it, it would be very much appreciated!

  • you are right `callSingle()` is simply not possible in UI tests, I have linked an answer which has all the details you need. I will also add a note on `callSingle()` there and to the docs – Peter Thomas Nov 27 '20 at 15:17

0 Answers0