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

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

Related

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

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 |

Selenium IDE Chrome - Regex

I am trying to get Selenium IDE to check some text and make sure it is Numbers Only, I have created the below (using Google) and it seems to fail each time.
Error :
assertText on css=tbody > tr:nth-child(1) > td:nth-child(1) with value regexpi:^[0-9]+$ Failed:
09:44:54
Actual value "10640355" did not match "regexpi:^[0-9]+$"
enter image description here
Can anyone tell me what I need to change, or have a step by step guide on how to add this check properly
https://www.selenium.dev/selenium-ide/docs/en/api/commands#assert-text
Regex is not supported for assert text:
https://github.com/SeleniumHQ/selenium-ide/issues/141 for details.
Workaround:
screenshot
code:
store text | css=tbody > tr:nth-child(1) > td:nth-child(1) | actualText
execute script | return RegExp('^[0-9]+$','g').test(${actualText}) | isNumber
assert | isNumber | true

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

Chrome Selenium IDE 3.4.5 extension "execute script" command not storing as variable

I am trying to create a random number generator:
Command | Tgt | Val |
store | tom | tester
store | dominic | envr
execute script | Math.floor(Math.random()*11111); | number
type | id=XXX | ${tester}.${dominic}.${number}
Expected result:
tom.dominic.0 <-- random number
Instead I get this:
tom.dominic.${number}
I looked thru all the resources and it seems the recent selenium update/version has changed the approach and I cannot find a solution.
I realize this question is 2 years old, but it's a fairly common one, so I'll answer it and see if there are other answers that address it.
If you want to assign the result of a script run by the "execute script" in Selenium IDE to a Selenium variable, you have to return the value from JavaScript. So instead of
execute script | Math.floor(Math.random()*11111); | number
you need
execute script | return Math.floor(Math.random()*11111); | number
Also, in your final assignment that puts the 3 pieces together, you needed ${envr} instead of ${dominic}.

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
})