karate: Dynamic Scenario Outline not working when json is defined in background - dynamic

Feature: Dynamic Scenario Outline
Background:
* def kittens = [{"name":"abc"},{"name":"def"}]
Scenario Outline: cat name: <name>
* print <name>
Examples:
| kittens |
error after executing this code is:
org.graalvm.polyglot.PolyglotException: ReferenceError: "kittens" is not defined
Whereas, if I put this JSON directly into examples, this is working and executing 2 time. passing as shown below:
Examples:
| [{"name":"abc"},{"name":"def"}] |
why am i getting "kittens" is not defined, any idea?

The Background does not work like this in new versions of Karate.
Please read: https://github.com/karatelabs/karate/releases/tag/v1.3.0
So try:
Feature:
#setup
Scenario:
* def kittens = [{"name":"abc"},{"name":"def"}]
Scenario Outline: cat name: <name>
* print name
Examples:
| karate.setup().kittens |

Related

I am struggling to get Karate to read my data from my csv file before I call my function [duplicate]

This question already has an answer here:
How do I invoke my JS Function for a particular column in a csv file in the Karate Framework?
(1 answer)
Closed 1 year ago.
#noinspection CucumberTableInspection
Feature: Serverless
Background:
* def data = read('input.csv')
* csv data = data
* def isValid = function(x){ return (x.name && x.unit) ? x.cost.length != 0 : true }
Scenario Outline:
* assert isValid(__row)
Examples:
| data |
The test runs but doesn't check anything.
Karate version: 1.1.0.RC4
elapsed: 1,77 | threads: 1 | thread time: 0,00
features: 0 | skipped: 1 | efficiency: 0,00
scenarios: 0 | passed: 0 | failed: 0
HTML report: (paste into browser to view) | Karate version: 1.1.0.RC4
===================================================================
Process finished with exit code 0
The * csv data = data is not needed and was used to explain something totally different earlier. When "read" a CSV becomes a JSON array automatically: https://github.com/intuit/karate#csv-files
If you are new to Karate I recommend you don't use CSV files and the Scenario Outline. Please just get used to Examples first and then move on from there. You are combining 3 advanced topics - CSV, dynamic outlines and conditional assertions. Do your tests really need to be so clever and complex ? Read this please: https://stackoverflow.com/a/54126724/143475
And please please read the docs and the examples: https://github.com/intuit/karate#dynamic-scenario-outline

Cucumber version - 6.9.1 - Couldn't pass blank string in Scenario Outline "Examples"

After upgraded cucumber version to 6.9.1, blank (empty string) couldn't be passed in Examples.
Passing empty strings used to be possible before
Scenario Outline : Testing datatable with empty string
When passing "<first>" and "second>" as String parameter
Then exception should be thrown
Examples:
| first | second |
| simple | |
I am able to use this in cucumber-java version 6.9.1 . Just tested it
Scenario Outline: example
Given I log <TestString>
Examples:
|TestString|
|" " |
#Given("I log {string}")
public void logSomething(String teststr ) {
System.out.println("sample text:"+ teststr+"somethingtoIdentifySpace");
}
Prints this
sample text: somethingtoIdentifySpace

Reading json from file in Karate feature fails with js evaluation failed error

I'm using Karate Netty, version 0.9.6 on Windows 10 with openJDK 14.0.2.
I'm trying to read data from a json file in a feature file.
The following code fails:
Scenario: Get the credit balance
* def data = read('classpath:examples1/user_credit_balance_get.json')
My console output looks as follows:
Karate version: 0.9.6
======================================================
elapsed: 2.31 | threads: 1 | thread time: 0.02
features: 1 | ignored: 0 | efficiency: 0.01
scenarios: 1 | passed: 0 | failed: 1
======================================================
failed features:
features.protect_a_prospect: protect_a_prospect.feature:4 - evaluation (js) failed: read('classpath:examples1/user_credit_balance_get.json'), java.lang.RuntimeException: evaluation (js) failed: ?{
"session_data": {
"user_id": "101",
"session_id": "dslkdaskljd",
"token": "02389poasklj"
},
"call_data": {
"user_id": "101"
}
}, javax.script.ScriptException: <eval>:2:18 Expected ; but found :
"session_data": {
^ in <eval> at line number 2 at column number 18
stack trace: jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:477)
stack trace: com.intuit.karate.ScriptBindings.eval(ScriptBindings.java:155)
com.intuit.karate.exception.KarateException: there are test failures !
at ...(.)
This leads me to believe that Karate is trying to read my json file as if it were JavaScript.
What could be the reason for this behaviour?
-- Edit --
Using karate.readAsString instead of read works as a workaround for me:
Scenario: Get the credit balance
* def data = karate.readAsString('classpath:examples1/user_credit_balance_get.json')
This is mighty confusing, you say Karate mocks but then you show the log for a Karate test. Karate should never try to evaluate a *.json file.
I think the best thing to do is to follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

In Cucumber 'Feature file '-> 'Examples' , how to set path for CSV file

My sample feature file rather than giving data from Examples I want it to pass from csv how to achieve that can anyone help me out.
Feature file:
Feature: Rocky Search Status
Scenario Outline: Rocky Search Status with Filters
Given Open firefox and start application for Rocky Search Status
When User enters "<price_right>" and "<Carat_left>" and "<Color_right_param>" and "<Cut_right_param>" and "<Clarity_right_param>"
Then Message displayed Rocky Search Status Successful
Then Application should be closed after Rocky Search Status
Examples:
| price_right | Carat_left | Color_right_param | Cut_right_param | Clarity_right_param |
| 10000 | 1.5 | 80 | 180 | 84 |
I want the data values to be defined in CSV outside the Project.
Not directly. However, you can have a record ID (or test case number) of sorts in the Example table. You can then retrieve records from the CSV in the step code based on the ID.
Scenario Outline: Rocky Search Status with Filters
Given Open firefox and start application for Rocky Search Status
When User enters data specified in test case <tcn>
Then Message displayed Rocky Search Status Successful
Then Application should be closed after Rocky Search Status
Examples:
|tcn|
|1 |
|2 |
The "When" step will use the tcn to retrieve the corresponding record from the CSV.
You can't with Gherkin. What you can do is to give your CSV file an appropriate name, refer to the name inside your Gherkin step, and then load and read the file inside your step definition.
abc.feature
Feature: A
Scenario: 1
Given data at abc.csv
...
step-definitions.js
Given(/^data at (.*)$/, function (fileName) {
const data = jsonfile.readFileSync(`${__dirname}/${fileName}`);
// iterate over data
})

"-" character is converted to ? in console for gherkin language used in cucumber feature file

I am using cucumber feature file to execute or write my test cases. Issue is that I am using data table as :
| abc | 1234567890 |
| defg | New Activation – Monthly (Credit in store) |
Now issue is this that console is throwing an error that :
Cannot locate element with text: New Activation ? Monthly (Credit in store).
I am not able to understand why ? is being displayed instead of "–" .