API testing XML, forms and other data formats

  • In addition to API testing with JSON, it's possible to use XML, forms or other data formats in API tests
  • This chapter builds on the knowledge gained from the API testing tutorial so make sure to spend 20 minutes to understand the basics from the tutorial first

Testing XML APIs

  • You indicate that you're sending XML messages by setting the format to "XML"
  • Requests to be sent are kept in files, in the "xml/request" directory in your apitest project's folder
  • Similarly, the expected responses are kept in the "xml/response" directory
Feature: API Testing XML

Scenario: *** Create order ***

    Given address "https://example.com"
    Given URL path "/order/create"
    Given format "XML"
    Given request "create_order.xml"

    When the URL is invoked
    Then status is "200"
    And response is equal to that from "expected_order_response.xml"

Submitting forms

  • If an API that you test requires that you send a with the "POST" HTTP verb, use the following steps.
  • Note that you need to clean up the form data if you don't want for other tests to have access it. If you don't clean it up, the same form will be available to any tests that follow the one that created it.
Feature: API Testing Form

Scenario: *** Create order ***

    Given address "https://example.com"
    Given URL path "/order/create"
    Given format "FORM"
    Given REST method "POST"
    Given form field "order_type" is "new"
    Given form field "user_id" is "123"

    When the URL is invoked
    Then status is "200"
    And form is cleaned up

Using other data formats ("RAW")

  • If you use the "RAW" data format, you'll be able to send and receive any string data, no matter what the actual data format is
  • In the following example, an arbitrary string is sent and a CSV response from the API invocation is checked
Feature: API Testing RAW Format

Scenario: *** Create order ***

    Given address "https://example.com"
    Given URL path "/order/create"
    Given format "RAW"
    Given request is "My data"

    When the URL is invoked
    Then status is "200"
    And response is equal to "abc,def\n123,456"

Read more