空手道多URL访问在单个方案中

时间:2019-07-12 18:22:00

标签: karate

我想在一种情况下访问多个URL。

当在Background中定义了URL并在Scenario中使用了另一个URL时,该URL就会更改。

如果我使用path,则不会出现这种情况。 它可以修复url中的Background吗?

Feature: examples

Background:
    * url 'https://jsonplaceholder.typicode.com'

Scenario: get all users and then get the first user by id
    Given path 'users'
    When method get
    Then status 200

    Given url 'https://api.github.com/search/repositories'
        And param q = 'intuit/karate' 
    When method get
    Then status 200

    # The expected behavior is accessed to 'https://jsonplaceholder.typicode.com/users'.
    # But the accual behavior is accessed to 'https://api.github.com/search/repositories/users'.
    Given path 'users'
    When method get
    Then status 200

1 个答案:

答案 0 :(得分:1)

否,但是如果将Given url 'https://api.github.com/search/repositories'移至第二个Scenario:,它将正常工作。

这是故意设计的。查看hello world example。它进行了2次调用,但是url仅被提及一次,因为第二次调用只是添加了path。这是典型的REST模式。

因此,如果您确实需要执行其他API调用,则必须使用完整的url

Background:
    * def baseUrl = 'https://jsonplaceholder.typicode.com'

Scenario: get all users and then get the first user by id
    Given url baseUrl
    And path 'users'
    When method get
    Then status 200

    Given url 'https://api.github.com/search/repositories'
    And param q = 'intuit/karate' 
    When method get
    Then status 200

    Given url baseUrl
    And path 'users'
    When method get
    Then status 200