ubirak / rest-api-behat-extension

Stuff to easily test your rest api with Behat
MIT License
38 stars 24 forks source link

Search for json element in a GET api response #98

Open johnreytanquinco opened 6 years ago

johnreytanquinco commented 6 years ago

Im happy that I was able to use and test this extension for my needs. But I encounter an issue, might be a support feature given the condition and result below.

Condition: GET api/123-123-123?_format=json - FAILED

[
    {
        "uuid": "123-123-123"
    }
]

GET content/1?_format=json - PASSED

{
    "uuid": [
        {
            "value": "abc-abc-abc"
        }
    ]
}

Test Result


  Scenario: Sending GET request to existing resource should lead to 200                         # /opt/app-root/src/html/modules/custom/spa/behat/features/capabilities/custom/send_request.feature:10
    When I send a GET request to "content/1?_format=json"                                      # Ubirak\RestApiBehatExtension\RestApiContext::iSendARequest()
    Then the response status code should be in 200                                              # Ubirak\RestApiBehatExtension\RestApiContext::theResponseCodeShouldBeIn()
    And the JSON node "uuid[0].value" should be equal to "abc-abc-abc" # Ubirak\RestApiBehatExtension\Json\JsonContext::theJsonNodeShouldBeEqualTo()

  Scenario: Sending GET request to existing resource should lead to 200                         # /opt/app-root/src/html/modules/custom/spa/behat/features/capabilities/custom/send_request.feature:15
    When I send a GET request to "api/123-123-123?_format=json" # Ubirak\RestApiBehatExtension\RestApiContext::iSendARequest()
    And print last responses                                                                    # ApiFeatureContext::printLastResponses()
    Then the response status code should be in 200                                              # Ubirak\RestApiBehatExtension\RestApiContext::theResponseCodeShouldBeIn()
    And the JSON node "uuid[0].value" should be equal to "123-123-123"    # Ubirak\RestApiBehatExtension\Json\JsonContext::theJsonNodeShouldBeEqualTo()
      Failed to evaluate expression "uuid[0].value" (Ubirak\RestApiBehatExtension\Json\WrongJsonExpectation)
    │
    ╳  Warning: file_put_contents(/opt/app-root/src/html/modules/custom/spa/behat/screenshot/20180528-04_33_52-test-rest-api-request-error.jpg): failed to open stream: Permission denied in /opt/app-root/src/html/modules/custom/spa/behat/features/bootstrap/SocialMinkContext.php line 58
    │
    └─ @AfterStep # SocialMinkContext::takeScreenShotAfterFailedStep()

Does it have to do with the json format response? If yes, can anyone help of what is the correct way of searching for an element for uuid[0].value(on the failing result).

Already tried uuid and uuid[0], same result.

stephpy commented 6 years ago

Hi, did you try root[0].uuid ?