Giter VIP home page Giter VIP logo

karate-npm's Introduction

@karatelabs/karate

Seamlessly use the power of Karate from Node / JS projects.

Usage

Given this script test.js:

#! /usr/bin/env node
const karate = require('@karatelabs/karate');
karate.exec();

And in package.json (use the latest version from npm):

{
  "scripts": {
    "test": "node test.js"
  },
  "devDependencies": {
    "@karatelabs/karate": "^0.2.2"
  }
}

When you run npm install, jbang and other Karate dependencies needed will be installed via jbang-npm.

And to run a single test:

npm run test karate/httpbin.feature

Or to run all tests in a folder:

npm run test karate

Known Issues

Users on Windows have reported issues such as the npm install failing to complete and without any errors shown.

Please do contribute if you can and improve how JavaScript projects can integrate smoothly with Java projects !

As a workaround, please install JBang manually and re-try the npm install step.

Setting Karate Version

To use a specific version of Karate, just set karate.version before calling karate.exec():

#! /usr/bin/env node
const karate = require('@karatelabs/karate');
karate.version = '1.5.0';
karate.exec();

CLI Reference

All Karate capabilities can be invoked by the command-line.

The most common needs are to:

  • run feature-file(s) or all feature-files in a folder as Karate tests
  • start an API mock-server

The complete documentation can be found here.

You can also use the --help command-line option to see all the possible options and brief descriptions on the console.

karate-config.js

Karate will look for a karate-config.js file in the current working directory.

But if you need to point to a different directory, you can set karate.config.dir before calling karate.exec()

To pass arguments to the JVM use the jvm object karate.jvm.args

CLI options can be passed as a string to the karate.exec('-T=5') method.

#! /usr/bin/env node
const karate = require('@karatelabs/karate');
karate.config.dir = '/users/myname/some/dir';
karate.jvm.args = `-Dlogback.configurationFile=${__dirname}/logback-test.xml`;
karate.exec("-T=5");

Custom Java Classpath

For teams that want to customize the Java classpath by adding libraries or custom-code, please refer to the wiki: Custom Fat JAR. This may be easier to achieve by using Maven to prepare a Docker image, which can then be used by teams on the command-line without needing NPM, Java or Karate - and only Docker is a pre-requisite.

If you want to use additional Java libraries or custom code and avoid Maven and Java dev-tools, refer to this example: Using Java Libraries with NPM.

karate-npm's People

Contributors

ptrthomas avatar steve1337 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

karate-npm's Issues

Unknown error while loading Javascript file

Feature File

Background:
  * def jwtUtils = read('generatejwt.js')

Scenario: Generate JWT Token
  * def token = jwtUtils.createJWT()
  * print 'Generated JWT Token:', token

Javascript File-(generatejwt.js)

const jwt = require("jsonwebtoken");
const payload = {x
  user: {
    first_name: "fname",
    last_name: "lname",,
  },
};
const hashkey = 'hashkey';
const addOptions = {
  algorithm: "RS256",
};
function createJWT() {
  sails.log("Generating token..");
  const token = jwt.sign(payload, hashkey, addOptions);
  return token;
}

when i run the feature file i am getting syntax error

org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:1:1 Expected an operand but found const
(const jwt = require("jsonwebtoken");
 ^
Unnamed:20:0 Expected ; but found )
)

I dont see any error in syntax. please correct me if i am having any syntax error. I tried with read, karate call etc

How can I connect to DB with javascript?

Hi everyone!

I try to use karate-npm but I can't connect to DB by javascript.

Also, when I try to use javascript, I need to begin with the "function" word, and I can't use a library npm.

Please, can you help me?

Could not resolve dependencies when running npm i @karatelabs/karate

Environment:
OS: macos
node: v20.14.0
jbang: v0.116.0

Error:
[jbang] [ERROR] Could not resolve dependencies: Failed to collect dependencies at com.intuit.karate:karate-core:jar:all:LATEST

Description
I tried to install karate into my nodejs project, got the same error with running npm install in the example project at my local MacBook. Do you have any idea about the cause?

image

Test Cases are not getting executed.

I have cloned this and try to execute it. But the test case didnt execute it.

OS: Ventura 13.5 (22G74)
node version: v18.17.0
npm version: 9.6.7

santhoshkumarn@MacBook-Air nodejs-example % npm run test karate

@karatelabs/[email protected] test
node test.js karate

using curl + bash: -Dkarate.config.dir=karate com.intuit.karate:karate-core:1.3.0:all karate
user1@MacBook-Air nodejs-example %
user1@MacBook-Air nodejs-example %
user1@MacBook-Air nodejs-example % npm run test karate/httpbin.feature

@karatelabs/[email protected] test
node test.js karate/httpbin.feature

using curl + bash: -Dkarate.config.dir=karate com.intuit.karate:karate-core:1.3.0:all karate/httpbin.feature
user1@MacBook-Air nodejs-example %

Installation is getting stuck with 'npm i @karatelabs/karate'

Installation is getting stuck at :

OS : Windows 10
Method tried : VS Code terminal, Command prompt, PowerShell

PS C:\Workspace\Learnings\karate-js> npm i @karatelabs/karate
[##################] / reify:resolve: timing reifyNode:node_modules/yargs Completed in 649ms

Docker image vulnerabilities

I am using karate-npm 0.3.1 as a dev dependency for one of my projects which is released as a docker container. However, docker scout is detecting the following vulnerabilities being introduced by the underlying karate framework version (1.4.1). What I have seen is that not only docker scout detects these vulnerabilities but also ECR scanning process:

High Vulnerabilities

Medium Vulnerabilities

I have seen that karate 1.5.0 is recently released, where it seems that some vulnerabilities are being solved. Is there any plans to release a new version of karate-npm with all critical and high vulnerabilities are solved, so we can use it with docker images?

Can we use Karate framework for API automation testing in Node js based project using Karate NPM ?

Are there any samples or reference talk about this usage of Karate framework in Typescript based project ?

i tried to setup following read me and executed the tests. but i dont see test running. got stuck after "waiting for 2 features to complete"
no error or nothing shown up

using jbang: com.intuit.karate:karate-core:1.4.1:all ./src/features -t @create_user [jbang] Resolving dependencies... [jbang] Dependencies resolved 19:40:22.400 [main] INFO com.intuit.karate - Karate version: 1.4.1 19:40:22.579 [main] DEBUG com.intuit.karate.Suite - [config] karate-config.js 19:40:22.623 [main] DEBUG com.intuit.karate.Suite - waiting for 2 features to complete

Request to support karate.properties configuration to allow pass the variables

I'm using this library and not able to find a way to pass the variables which are the response data from pre javascript functions calls.

ANd I'm trying to find a way to pass the response data to karate. karate.properties should be the way, but for now in this library we don't provide the ability for users to customize the command prefix.
I know we could add my requests in .feature files, but those requests are some previous steps which doesn't make sense to involve them to the test features.

I'm wondering whether you could please raise a PR to support '-DMY_VARIABLE=some_value' to the excutable command?

image

Nothing happen after execution - Java 17.0.1 - Node 16.14.0 - NPM 8.5.0

After following the steps to run on windows, nothing happen, there ir no error and no execution log.

After adding @karatelabs/karate dependency and install.

npm i

SO: Windows 10 Home

Java, NPM and Node version:

C:\dev\api>npm -v
8.5.0

C:\dev\api>node -v
v16.14.0

C:\dev\api>echo %JAVA_HOME%
C:\Program Files\Java\jdk-17.0.1

My test.js

#! /usr/bin/env node
const karate = require('@karatelabs/karate');
karate.exec();

My package.json.

{
  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "node test.js"
  },
  "keywords": [],
  "author": "CharlyAutomatiza",
  "license": "ISC",
  "devDependencies": {
    "@karatelabs/karate": "^0.2.1"
  }
}

But nothing happen when I run in cmd.

C:\dev\api>npm run test karate/httpbin.feature

> [email protected] test
> node test.js "karate/httpbin.feature"

using curl + bash: com.intuit.karate:karate-core:1.2.0.RC5:all karate/httpbin.feature

C:\dev\api>

CLI parameter for threads is not recognized

Problem

I'm trying to configure karate to run scenarios in parallel but the cli options are not recongized

To reproduce

#! /usr/bin/env node
const karate = require("@karatelabs/karate")

karate.config.dir = "./testIntegration"
karate.exec("-T=5")

I also tried

  • -T 5
  • --threads=5
  • --threads 5

Output

using jbang:  -Dkarate.config.dir=./testIntegration com.intuit.karate:karate-core:LATEST:all -T=5
20:23:10.945 [main]  INFO  com.intuit.karate - Karate version: 1.3.0.RC2
Usage: <main class> [-CDhsSW] [-B[=<backupReportDir>]] [-d[=<debugPort>]]
                    [--debug-keepalive[=<keepDebugServerAlive>]] [-c=<cert>]
                    [-e=<env>] [-g=<configDir>] [-j=<jobServerUrl>] [-k=<key>]
                    [-n=<name>] [-o=<output>] [-p=<port>] [-P=<prefix>]
                    [-T=<threads>] [-w=<workingDir>] [-f=<formats>[,
                    <formats>...]]... [-H=<hookFactoryClassNames>[,
                    <hookFactoryClassNames>...]]... [-m=<mocks>[,
                    <mocks>...]]... [-t=<tags>]... [<paths>[($|,)<paths>...]...]
      [<paths>[($|,)<paths>...]...]
                            one or more tests (features) or search-paths to run
  -B, --backup-reportdir[=<backupReportDir>]
                            backup report directory before running tests
  -c, --cert=<cert>         ssl certificate (default: cert.pem)
  -C, --clean               clean output directory
  -d, --debug[=<debugPort>] debug mode (optional port else dynamically chosen)
  -D, --dryrun              dry run, generate html reports only
      --debug-keepalive[=<keepDebugServerAlive>]
                            keep debug server open for connections after
                              disconnect
  -e, --env=<env>           value of 'karate.env'
  -f, --format=<formats>[,<formats>...]
                            comma separate report output formats. tilde
                              excludes the output report. html report is
                              included by default unless it's negated.e.g. '-f
                              ~html,cucumber:json,junit:xml' - possible values
                              [html: Karate HTML, cucumber:json: Cucumber JSON,
                              junit:xml: JUnit XML]
  -g, --configdir=<configDir>
                            directory where 'karate-config.js' is expected
                              (default 'classpath:' or <workingdir>)
  -h, --help                display this help message
  -H, --hook=<hookFactoryClassNames>[,<hookFactoryClassNames>...]
                            class name of a RuntimeHook (or RuntimeHookFactory)
                              to add
  -j, --jobserver=<jobServerUrl>
                            job server url
  -k, --key=<key>           ssl private key (default: key.pem)
  -m, --mock, --mocks=<mocks>[,<mocks>...]
                            one or more mock server files
  -n, --name=<name>         scenario name
  -o, --output=<output>     directory where logs and reports are output
                              (default 'target')
  -p, --port=<port>         server port (default 8080)
  -P, --prefix=<prefix>     mock server path prefix (context-path)
  -s, --ssl                 use ssl / https, will use 'cert.pem' and 'key.pem'
                              if they exist in the working directory, or
                              generate them
  -S, --serve               app server using --workdir (experimental)
  -t, --tags=<tags>         cucumber tags - e.g. '@smoke,~@skipme' [@ignore is
                              always skipped by default]
  -T, --threads=<threads>   number of threads when running tests
  -w, --workdir=<workingDir>
                            working directory, defaults to '.'
  -W, --watch               watch (and hot-reload) mock server file for changes

Expected

Karate runs tests in parallel

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.