如何为空手道框架中CSV文件中的特定列调用JS函数? [英] How do I invoke my JS Function for a particular column in a csv file in the Karate Framework?
本文介绍了如何为空手道框架中CSV文件中的特定列调用JS函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个读取具有多行的CSV文件的功能文件,我需要验证如果其中两列满足特定条件,则第三列需要是强制的。我尝试过使用js函数,但是我不确定应该如何为第三列调用该函数。因此,根据下面的示例,如果名称为鸡,单位公斤则成本需要为必填项。
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">#
noinspection CucumberTableInspection
Feature: Serverless
Scenario Outline: Per Row *
def row =
""
" {
name: '<Name>',
unit: '<Unit>',
cost: '<Cost>',
}
@FunctionComparison
""
" *
def name = '<Name>' *
def unit = '<Unit>' *
def cost = '<Cost>' *
def checkMandatory =
""
"
function(arg1, arg2, arg3) {
if (arg1 in ('Chicken')) {
if (arg2 in ('kg')) {
if (arg3.length < 0) {
return false;
}
return true;
}
return true;
}
return true;
}
""
" *
match row ==
""
" {
name: '#ignore',
unit: '#regex ea|kg|ml',
cost: 'checkMandatory(name, unit, cost) == true'
}
""
"
Examples:
|
read('input' + '.csv') |
input.csv只是一个包含以下内容的CSV文件:
行、名称、单位、成本(列)
1,奶酪,EA,34.50 2,蛋糕,EA,55.54 3、鸡肉、千克、 4,水泥,毫升,4.32
推荐答案
您的验证函数可以简单得多,试试这个:
* def third = { name: 'foo', unit: 'bar', cost: '' }
* def isValid = function(x){ return (x.name && x.unit) ? x.cost.length != 0 : true }
* assert !isValid(third)
注意,Scenario Outlines
将注入变量,因此不需要<
和>
占位符:https://github.com/intuit/karate#scenario-outline-enhancements
您可以尝试此自包含的工作示例:
Feature:
Background:
* text data =
"""
name,unit,cost
Cheese,ea,34.50
Cake,ea,55.54
Chicken,kg,
Cement,ml,4.32
"""
* csv data = data
* def isValid = function(x){ return (x.name && x.unit) ? x.cost.length != 0 : true }
Scenario Outline:
* assert isValid(__row)
Examples:
| data |
这篇关于如何为空手道框架中CSV文件中的特定列调用JS函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文