📜 ⬆️ ⬇️

Getting Started with the Automation API: Part 1 - Overview

Translation of an article by Shashi Kumar Raja


So, let's say you want to do the automation of the API, but every time you type the best api automation tool in the search box in Google, you see so many links to dozens of top tools that you get confused and decide that you will definitely do it tomorrow




Let's start by understanding what we need, if we want to set up (hopefully tomorrow) an API automation platform . Hold this thought for a second ... I said, stop thinking ... I know that your boss does not allocate a penny to any paid tools . Look, I read your mind, don't think out loud



Paid tools must be hung, not executed :)


1. where the hell should I start writing tests?

You will need something that will provide you with sets of rules and guidelines for writing tests, and also allow you to do this by giving you access to several tools and techniques. I hear the bell ringing, no !!! Well, I know what will make the bell ring.


Have you ever heard of TESTNG , JUNIT , MOCHA , PYTEST , ROBOT !? Yes, they are all test automation environments.


You need to find a suitable testing environment based on your requirements: which existing technology stack does your company use? What automation do you want to do? Which language is more convenient for you, etc. You will find an automation environment in most popular languages ​​that will allow you to write modular, functional, and other types of API testing.


To learn more about the test environment, refer to the 2nd part of the series, where I presented Mocha and Pytest in detail .


2. How am I going to make API calls in a test environment?

Most of these platforms support API calls, including the HTTP request library , since the REST API uses the HTTP protocol to communicate.


Some frameworks, such as mocha , give you the freedom to use a library of HTTP requests of your choice, such as superagent .


  <code class = "lang-JS"> request
   .post ('/ api / pet')
   .send ({name: 'Manny', species: 'cat'}) // sends a JSON post body
   .set ('X-api-key', 'foobar')
   .set ('accept', 'json')
   .end ((err, res) => {
     // Calling the end function will send the request
   });
 </ code> 

They give you easy support for GET, PUT, POST, DELETE and all other methods. You can transfer headers, cache, query parameters, you called it - you got it


3. Cool, but some of my APIs give JSON, and others give XML in response, how can I deal with this?

Most of these HTTP request libraries will allow you to send and receive data in JSON, XML, CSV, Text, Image, form-data, encoded-data formats with several supported authorization standards.


They also allow you to process the HTTP response status code and check whether we received the required response status code or not.







4. Good, but how will I process the test data?

It depends on where you get the test data. These test frameworks will allow you to use all the features of the language in which they are based.


a. Database: you can easily create database connections to read data.


b. External file: you can read external text, JSON, CSV or any other files.


c. Random data: you can use libraries, such as faker , to generate random test data on the fly.


  <code> var faker = require ('faker');

 var randomName = faker.name.findName ();  // Rowan Nikolaus
 var randomEmail = faker.internet.email ();  // Kassandra.Haley@erich.biz
 var randomCard = faker.helpers.createCard ();  // random contact card containing many properties
 </ code> 

d. Data from the API response: Many times during testing, you will need to pass the response of one API as request data to another. You can do this using hooks . You will get functions, such as Before, Before each, After, After each , which, as the name implies, are performed before / after any or all of the tests. Call API1 before API2, and pass its response to API2. Just to the right !!! ️


5. Processing the test data and executing the API calls seems simple, but how am I going to check the API responses?

To check the answers you need a library called Assertion library . Many test environments come bundled with assertion libraries, which gives you the opportunity to write asserts in plain English, such as syntax. They also allow you to check the JSON scheme of your response.


In mocha, you can use any library of statements, for example, chai .


  <code> response.status.should.equal (200)
 foo.should.be.a ('string');
 foo.should.have.lengthOf (3);
 tea.should.have.property ('flavors')
   .with.lengthOf (3);
 </ code> 

6. Fabulous !!! There was a trifle, after all this testing, to somehow show my boss what I did and where I found problems?

Most of these frameworks will provide you with a basic HTML report on a test run, which you can download and share. If you need more beautiful reports with graphs and charts, you can use several open source reporting tools, such as allure or mochawesome .



7. Only if I could get some kind of template with these things to start automating the API now.

What did I tell you, you called it, you got


This is the template that I created to automate the API using mocha in node.js.


It includes-



If you need a template in another language, I can prepare it for tomorrow.


If you liked this article you can clap your hands , it will probably make me write more


')

Source: https://habr.com/ru/post/433726/


All Articles