RESTful API Automation Library

The CukeTest product provides a RESTful API automation library called leanpro.got. It is a simple wrapper for the npm library got. The CukeTest has this library and a toolkit for generating the got API call code built in, so you can automate the API without installing packages. You can click on the name to see the type file for this library. A brief description of how to use this library is provided below. If you need more detailed documentation, you can check out the got official documentation

HTTP library - use of Got

Use the following line of code to introduce the built-in library for initiating HTTP requests - got.

JavaScript
const got = require('leanpro.got');

Accessing the API using the got library is simple and intuitive, divided into three parts: the HTTP request method, the API link, and the configuration item option. two common ones are.

  1. The first one: Use the corresponding method corresponding to the HTTP request:

JavaScript
got.get(url, [options]);
got.post(url, [options]);
got.put(url, [options]);
got.delete(url, [options]);
...

  1. Second: Write the HTTP request method to the configuration item options:

JavaScript
let options = {
   method: "GET" [| "POST" | "PUT" | "DELETE" |... ],
   ...
};
got(url, options);

The first method is recommended here to ensure readability of the code.

Configuration options

The configuration item options controls all the details of request action initiation, such as retry count, timeout, proxy, HTTP protocol, etc. Since the properties of options of the got library are designed according to the node.js native https.request library, the configuration can be seen in the options documentation, also see https.request documentation.

For stability reasons, CukeTest uses the got version 9.6.0, so the documentation has been chosen accordingly. If you need a different version, you can install the got library independently.

API Code Toolbox

Of course, in addition to writing your own configuration, you can also choose the code generation window provided by the CukeTest toolbox, in the left-hand side "Toolbar" -> "Collaboration" -> " RESTful API", the tool is shown below:

API Tools

API Tools Interface

Drag and drop the tool into the editor and, with a simple configuration, the corresponding code will be generated in that area. The tool provides four commonly used methods get, post, put and delete, and two other methods can be used to modify the code directly. In the API tool interface we can see that several options are provided.

  • Methods: four optional methods get, post, put, delete.
  • Url address: the link to the API, be careful not to omit the prefix http:// or https://, although the tool can automatically identify and distinguish without the prefix, but it is recommended to add it manually.
  • HTTP headers: that is, the HTTP request headers, the specific content can refer to HTTP Headers. default for the POST request header content-type attribute provides the default value application/json target API accepts JSON in the form of the request body body.
  • JSON: usually just keep the default open, open to accept the request body in the form of json (body) passed in.
  • Request body: HTTP request content, usually JSON format data in API tests.

In addition, there are many API automation libraries available in NPM, so if you want to use other libraries, or use a different version of the got library, you can install it directly with npm: for example: npm install got installs the latest version of the got library, which may have slightly different functionality than the built-in leanpro.got.

results matching ""

    No results matching ""