空手道 - 在单一场景中有多个“何时”

Karate - Having multiple 'When's in single scenario

我有一个场景,其中有多个步骤/REST 操作需要 运行 完成一个过程。

每个REST操作都需要授权用户名和密码。我在 Background session 中提供了这个。这就是我现在的功能的样子。

Feature: Adding to cart

  Background: 
    * url 'https://soa-mp-rmsk-someurl.com'
    * header agent_uid = 'AUTO_TST'
    * configure ssl = true
    * header Authorization = call read('classpath:Project/JSFiles/auth.js') { username: 'ABC', password: '123' }
    * configure logPrettyResponse = true
    * configure logPrettyRequest = true

  Scenario: Find available mobiles
    Given path '/v1/available-mobiles'
    When method get
    Then status 200
    * def mobile = response.iPhoneXSMax

  # Add a mobile to cart
    Given path '/v1/mobiles/'+mobile+'/add
    And request {name: 'iPhoneXSMax'}
    When method put
    Then status 200

现在抛出错误说 "faultstring": "Authentication challenge issued"

我可以将它们分组到不同的场景中,这样它们每次都会调用 header authorization,从而导致成功 运行;我也试过这个。为我工作。但我认为将这些步骤分组到不同的场景中并不是一个好的做法,因为它们实际上构成了一个场景。我怎样才能克服这个错误?或者我应该去分发它们在不同的场景?

https://automationpanda.com/2018/02/03/are-gherkin-scenarios-with-multiple-when-then-pairs-okay/

EDIT-1 这就是我尝试为授权详细信息添加配置 headers 的方式;我没能完全理解这一点,你能帮忙吗?

headers.js

function fn() {
      var username = karate.get('username');
      var password = karate.get('password');
      if (username && password) {
        return { 
            Authorization: username + password
        };
      } else {
        return {};
      }
    }

我在功能背景中这样称呼它;但没有用。

* configure headers = read('classpath:headers.js')
* def username = 'ABC'
* def password = '123'

我认为您完全错过了基于 header 的身份验证 - 空手道有一种全球性的钩子 - 这是大多数团队使用的。请参阅 configure headers.

的文档

只想说,对于 EDIT-1,我设法得到了解决方案。这就是我编写具有 BASE64 格式转换的 headers.js 文件的方式。

如果我能以更好的方式增强它,请让我知道。

function fn() {
      var username = karate.get('username');
      var password = karate.get('password');

      if (username && password) {
      var temp = username + ':' + password;
      var Base64 = Java.type('java.util.Base64');
      var encoded = Base64.getEncoder().encodeToString(temp.bytes)
        return { 
            Authorization: 'Basic ' + encoded
        };
      } else {
        return {};
      }
    }