SimpleAPI.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. How to create a simple API Test
  2. ===============================
  3. API-Tests are usually something, that the classic business people wouldn't know about. With ``baangt`` and a bit
  4. of effort it should be possible even for business people to do some simple API-Tests.
  5. As with all tests in ``baangt`` also API-Tests are defined in a simple format in MS Excel. See ``MovieSimpleAPI.xlsx``
  6. as working example.
  7. Prerequisit for simple API definition format:
  8. * Filename must contain the word "api", otherwise simple format will try to create a browser test run.
  9. Steps to test the simple API Format:
  10. ------------------------------------
  11. Fire up ``baangt``, chose file ``MovieSimpleApi.XLSX`` as your run definition. Start the testrun by clicking on the
  12. button ``Execute``. After a few seconds you should see the popup "Testrun finished". Now open the result file
  13. ``baangt_MovieSimpleAPI_<date>.xlsx`` and see overview and details of the API Test run.
  14. Play around
  15. ^^^^^^^^^^^
  16. To extend this very simple example you could want to add the field "Actors" to your result sheet. To do so, add one line
  17. in the Tab ``TestStepExecution``.
  18. * ``Activity`` is ``SAVE``
  19. * ``Value`` is ``RESULT_Actors``
  20. * ``Value2`` is ``$(ANSWER_CONTENT.Actors)``
  21. Save the Excel-Sheet. Re-run the test case and you should see the new column "Result_Actors" with the values retrieved
  22. from the API.
  23. Activities for API-Tests:
  24. -------------------------
  25. (Even though we write all activtities in UPPER CASE, you can write them in any way you like)
  26. .. list-table:: Values for Activities for simple API format
  27. :widths: 25 75
  28. :header-rows: 1
  29. * - Activity
  30. - Description
  31. * - ``APIURL``
  32. - Set's the main URI/URL for your API-Tests. Could be omitted, if you want to always specify full path in ENDPOINT.
  33. * - ``ENDPOINT``
  34. - Set's the Endpoint-Name for the following API-Call. E.g. if your Endpoint is located at
  35. https://app-eu.earthsquad.global/api/rest-auth/login and during this test case execution you'd call a lot of APIs
  36. on this server, then you'd set ``APIURL`` to https://app-eu.earthsquad.global and set ``ENDPOINT`` to "/api/rest-auth/login"
  37. * - ``POST``
  38. - Send a "POST"-Request to the API. Place the content, that you want to send to this endpoint in the column ``value``
  39. * - ``GET``
  40. - Send a "GET"-Request to the API. URL-Parameters are taken from ``APIURL`` and ``ENDPOINT``. Result is stored for
  41. immediate retrieval (see below).
  42. * - ``HEADER``
  43. - Set additional parameters for the next API-Calls into the Header. In combination with the special fields (see below)
  44. it's easy to take a result from one API-Request and use it (or parts of it) as input for the next call.
  45. * - ``SAVE``
  46. - Save a value from the header or from result to output file (XLSX). ``value`` is the field-name. If you name it
  47. "RESULT_<something>" it is automatically added to the export field list. If you work in API-Simple mode, this is
  48. your only chance to get fields added into the result sheet.
  49. ``value2`` is the source (e.g. ``$(ANSWER_CONTENT.imdbRating)`` would retrieve the value "imdbRating" of the
  50. answer of your API-Call.
  51. * - ``ASSERT``
  52. - This will retrieve value of element specified by `locator`
  53. And compare with expected_value specified in `value`
  54. if expected_value not matches with output_value it will raise TestStepExecution and result in FAILED.
  55. * - ``ADDRESS_CREATE``
  56. - Create Address Data for various test cases and save in testDataDict
  57. The following field variable can be used via $(field_name).
  58. ['HouseNumber', 'AdditionalData1', 'AdditionalData2', 'StreetName', 'CityName', 'PostalCode', 'CountryCode']
  59. Example:
  60. Default Data: (value=<blank> and value2=<blank>)
  61. 'HouseNumber': '6', 'AdditionalData1': 'Near MahavirChowk', 'AdditionalData2': 'Opposite St. Marish Church', 'StreetName': 'GoreGaon', 'CityName': 'Ambala', 'PostalCode': '160055', 'CountryCode': 'India'
  62. `value` optional
  63. if provided : (value= {"CountryCode":"US","CityName":"Athens"} value2=<blank>)
  64. FieldValue updated to:
  65. {'HouseNumber': '6', 'AdditionalData1': 'Near MahavirChowk',
  66. 'AdditionalData2': 'Opposite St. Marish Church',
  67. 'StreetName': 'GoreGaon', 'CityName': 'Athens',
  68. 'PostalCode': '160055', 'CountryCode': 'US'}
  69. `value2` optional
  70. Field will be prefixed with "office_<field_name>". Ex. "office_CountryCode"
  71. Special data fields in API-Tests:
  72. ---------------------------------
  73. In WEB-Testing you check results either via ``Assert``-Statement or via mapping the text or attribute of an element to a
  74. field in the TestDataDictionary. In API-Tests you have some automatic internal variables, that you can use without
  75. manually declaring them:
  76. .. list-table:: Special Internal Variables in API-Testing
  77. :widths: 25 75
  78. :header-rows: 1
  79. * - Variable
  80. - Contents
  81. * - RESULT_CODE
  82. - Result code of the last call to an API. Ideally you'd be able to match result codes as described in here
  83. https://restfulapi.net/http-status-codes/, but in the end setting the status code is the job of the developer of
  84. the API you're using - they might follow a different path or simply have bugs.
  85. * - ANSWER_HEADER
  86. - Last Header. You can access a certain part of the header by using $(ANSWER_HEADER.<partName>), so if you want to
  87. use the part ``login_key`` of a header you'd write ``$(ANSWER_HEADER.LOGIN_KEY)``
  88. * - ANSWER_CONTENT
  89. - Last content of an API-Call (Post, Get, etc.). Again you can access/extract/replace parts of this content using
  90. the "." like described in the line above (e.g. ``$(ANSWER_CONTENT.FRANZI)`` to refer to a content part ``FRANZI``.
  91. Random
  92. ------
  93. Sometimes we need random values like string, name, integer, float, date, time now in such case we have ``random``
  94. functionality. It is used inside value column of and its structure is
  95. ``$(random{"type":<Type>},"min":<Minimum>,"max"<Maximum>,"format":<Format>)``. Only ``type`` field is compulsory and
  96. every other fields are optional, also each fields are not useful in every type, e.g.- ``name`` type doesn't need any
  97. other optional fields as they are use less for it. You can see fields and types supporting them.
  98. .. list-table:: Fields supporting types
  99. :widths: 25 75
  100. :header-rows: 1
  101. * - Field
  102. - Type
  103. * - type
  104. - This field is compulsory and base of ``random`` funtionality.
  105. string, name, int, float, date, time are the types currently supported
  106. * - min
  107. - string, int, float, date, time are the types supporting this field. Value of min will be with respect to its
  108. type like value for string will be an integer containing minimum number of characters in string and for all other
  109. it will be lower limit, for int it will be an integer & float for float, for date value will be a date e.g. -
  110. "31/01/2020" and for time it would look like "20:30:59"
  111. * - max
  112. - string, int, float, date, time are the types supporting this field. Value of max will be same like in min,
  113. value for string will be an integer containing maximum number of characters in string and for all other it
  114. will be upper limit, for int it will be an integer & float for float, for date value will be a date e.g. -
  115. "01/06/2020" and for time it would look like "13:10:30"
  116. * - format
  117. - date, time are the only types supporting format field. In above date examples date is in %d/%m/%Y format and
  118. time is in %H:%M:%S format. Here "%d" stands for the day, "%m" stands for month, "%Y" stands for year including
  119. century e.g.- 2020, if you want only year you can use "%y" e.g. 20. If you use min and max fields in date, time
  120. then you must input its written format in format field, default will be ""%d/%m/%Y" for date. Now if you want
  121. date with "-" as seperator you can write format as "%d-%m-%Y" so the output would be like "31-01-2020".
  122. `examples`
  123. $(random{"type":"name"})
  124. $(random{"type":"string", "min":10, "max":100})
  125. $(random{"type":"int", "min":10, "max":100})
  126. $(random{"type":"float"})
  127. $(random{"type":"date", "min":"20/1/2020", "max":"30/6/2020", "format":"%d/%m/%Y"})
  128. $(random{"type":"time"})
  129. $(random{"type":"time", "min":"10.30.00", "max":"15.00.00"}, "format": "%H.%M.%S")