首页 > 解决方案 > 在 karate-netty 中定义一个常数

问题描述

如果我想做类似的事情:

Feature: Register the account and obtain the jwt

Background:
* url baseUrl
* def email = 'qatest_api_' + String(new Date().getTime()) + '@redacted.com'

Scenario: Register

Given path '/identity/v1/register'
  * request {email: '#(email)', password: '#(password)'}
When method post
Then status 200

Scenario: Obtain jwt

Given url 'https://redacted/oauth/token'
  * request 
"""
  {
    realm: "Username-Password-Authentication",
    grant_type: "http://auth0.com/oauth/grant-type/password-realm",
    username: '#(email)',
    password: '#(password)',
    audience: "https://redacted/userinfo",
    scope: "openid",
    client_id: "redacted",
    client_secret: "redacted"
  }
"""
When method post
Then status 200

它会在两种情况下评估变量,所以在第一种情况和第二种情况下email我会得到类似的结果。qatest_api_1542209546879@redacted.comqatest_api_1542209545312@redacted.com

如果可能的话,我会避免硬编码这个值,我读过callonce但没有使用它,有一个独立的文件来生成时间戳听起来很有趣。

我正在使用 karate-netty,你对如何实现这一点有什么建议吗?:)

谢谢!

标签: karate

解决方案


callonce被设计成它可以是右侧的 javascript。

试试这个Background

* def fun = function(){ return 'qatest_api_' + new Date().getTime() + '@redacted.com' }
* def email = callonce fun

拥有第二个文件的好处是它可以在多个场景中重复使用,它没有任何问题:)


推荐阅读