如何使用空手道功能文件中的 .js 文件中的参数调用 Javascript 函数

How to call a Javascript function with arguments from .js file in karate feature file

假设我在函数 js 文件中创建了 javascript 个函数。

函数 getReviews(评论){<br> var length_reviews = reviews.length return length_reviews }

函数 getReviewsLength(reviewLength){<br> return 评论长度 }

这里函数getReviews的参数reviews是一个数组。 现在我将如何在一个功能文件中调用 getReviews 函数。 当我尝试下面的代码时

* def jsFunction = call read('functions.js') * def review = jsFunction.getReviews(reviewFromFeatureFile) 我收到

错误

Cannot read property "length" from undefined

我已经打印了 reviewFromFeatureFile 并且它正确地出现在打印语句中。

在一个"common"特征文件中,定义多个这样的方法:

* def uuid = function(){ return java.util.UUID.randomUUID() + '' }
* def now = function(){ return java.lang.System.currentTimeMillis() }

您现在可以这样调用此功能:

* call read('common.feature')

现在该功能中的所有功能都可以使用了:

* def id = uuid()
* def time = now()

正如 Peter 上面提到的,您可以让 js 内嵌在您的功能中

* def reviews = [{"r1":2},{"r1":3},{"r1":4}]
* def getReviews = function(reviews){ return reviews.length }
* def getReviewsLength = getReviews(reviews)
* print getReviewsLength

在这个例子中,它应该打印 3。

有关处理 javascript 或空手道中其他可重用模块的更多其他选项,请参阅本文

Organizing re-usable functions in karate