---
# System prepended metadata

title: Karete + BDD example

---

## Additional module in test-automation-framework
```
├── src
│   ├── api-bdd
│   │   ├── karate-config.js
│   │   ├── features
│   │   │   └── example.feature
│   │   ├── examples
│   │   │   └── exampleTest.js
│   │   ├── helpers
│   │   │   └── apiHelper.js
│   │   ├── hooks
│   │   │   └── setup.js
│   │   ├── schemas
│   │   │   └── expected-schema.json
│   │   ├── config
│   │   │   └── endpoints.js
│   │   ├── reports
│   │   └── karate.jar
```

## example.feature
```
Feature: Example API BDD Feature

 Background:
    * def endpoints = read('classpath:config/endpoints.js')
    * url baseUrl
    * call read('classpath:hooks/setup.js')

  Scenario: Test endpoint C by chaining requests through endpoints A and B
    Given path endpoints.endpointA
    When method get
    Then status 200
    And def field1Value = response.data.field1

    Given path endpoints.endpointB
    And request { field1: '#(field1Value)' }
    When method patch
    Then status 200
    And def field2Value = response.data.field2

    Given path endpoints.endpointC
    And request { field2: '#(field2Value)' }
    When method post
    Then status 200
    And match response == { expectedField: 'expectedValue' }

    # Validate the response against the JSON schema
    And def schema = read('classpath:schemas/expected-schema.json')
    And match response == schema
    
  Scenario: Invalid auth token
    * configure headers = { Authorization: 'Bearer INVALID_ACCESS_TOKEN', 'Content-Type': 'application/json' }
    Given path endpoints.endpointA
    When method get
    Then status 401
    
 # Teardown
 * call read('classpath:hooks/setup.js').teardown

```

## expected-schema.json

```
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "expectedField": {
      "type": "string"
    },
    "anotherField": {
      "type": "number"
    }
  },
  "required": ["expectedField", "anotherField"]
}

```

## exampleTest.js

```
const karate = require('karate');

karate.run('classpath:features/example.feature').tags('@example');

```

## apiHelper.js
```
function setUpLogger() {
    // Example logger setup
    console.log('Logger is set up');
}

function clearEnvironmentVariables() {
    // Example: Clear specific environment variables if needed
    delete process.env.TEMP_VAR;
    console.log('Environment variables cleared');
}

module.exports = {
    setUpLogger,
    clearEnvironmentVariables
};

```

## karate-config.js
```
function() {
    var config = {
        baseUrl: 'http://example.com/api',
        headers: {
            'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
            'Content-Type': 'application/json'
        }
    };

    karate.configure('headers', config.headers);

    return config;
}

```

## endpoints.js
```
const endpoints = {
    endpointA: '/endpointA',
    endpointB: '/endpointB',
    endpointC: '/endpointC'
};

module.exports = endpoints;
```
## setup.js
```
const karate = require('karate');
const { setUpLogger, clearEnvironmentVariables } = require('./helpers/apiHelper');

function setup() {
    karate.log('Setup starting');
    // Add any setup logic here, if necessary
    karate.log('Setup complete');
}

function teardown() {
    karate.log('Teardown starting');
    // Add any teardown logic here, if necessarydatabase connections
    karate.log('Teardown complete');
}

module.exports = {
    setup,
    teardown
};

```