Git Product home page Git Product logo

Comments (5)

dvargas46 avatar dvargas46 commented on May 25, 2024

Likely due to no shared scope here. karate.call and karate.callSingle do not default to shared scope. In fact, I don't think callSingle even has an option for that.

Can you try this instead:
* callonce read('header.feature')

from karate.

donholgo avatar donholgo commented on May 25, 2024

That works indeed, at least in the simplified example files. In my real tests I'd like to have the call configurable, though (we have a development mode that doesn't need it), so it really looks like

* if (useAuthentication) karate.callSingle('header.feature', { username: 'myuser' } )

Any idea how I can have that with callonce?

from karate.

dvargas46 avatar dvargas46 commented on May 25, 2024

Yes, that can be achieved by putting the conditional within the called feature/js instead.

However, this would be arguably less readable since someone would have to check the called file to find out the condition, plus it's a wasted read/call to the file in dev. Tho, maybe that's all you need given that there's no shared scope overload for callSingle.

@ptrthomas , how would you feel about karate.callSingle having an overload for shared scope to be in parity with karate.call ?

from karate.

ptrthomas avatar ptrthomas commented on May 25, 2024

@dvargas46 yes I think that makes sense. interesting why this has not come up before. I was looking at the existing reference example:

  1. https://github.com/karatelabs/karate/blob/03a840805ec0db5ec3f4330754545f1eefbcde0b/karate-demo/src/test/java/karate-config.js
  2. https://github.com/karatelabs/karate/blob/2e73fc38e397cf4e46c38b5256c0d91261ea5920/karate-demo/src/test/java/demo/headers/headers-single.feature

so even the examples, the pattern is - a) callSingle returns some data b) that data is used to configure headers "centrally" in karate-config.js

from karate.

dvargas46 avatar dvargas46 commented on May 25, 2024

@ptrthomas I think you are right, I believe what you outlined would be the recommended pattern to use here too.

For example, it could looks something like this to set the auth header globally instead using callSingle:

karate-config.js

function fn()  {
  const config = {}
  const headers = {}

  if (karate.env !== 'dev') {
    // configure headers with authentication only in non-dev environments - callSingle only called once
    const results = karate.callSingle('header.feature', {username: 'myUsername'})
    headers.Authorization = results.authHeader
  }

  //
  // any other headers or config variables can also be set ...
  //

  karate.configure('headers', headers)

  return config
}

header.feature

@ignore
Feature: Header

  Scenario: Return Authorization header
    # or perform some operation with the input first, like Base64 encoding of credentials, etc.
    * def authHeader = 'Basic 12345'

test.feature

Feature: Test

  Background:
    * url 'http://example.com'
    # set any scenario specific headers as needed
    * header X-TOKEN = 'token-needed-here'

  Scenario: Some test
    When method GET
    Then status 200

from karate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.