Tip | Check how frontend app is using your API

Updated: Sep 14, 2020

Measure api and xhr duplicates

The problem

I run into a situation were the API test was passing correctly and E2E tests too, but the app was doing over calls to the backend, and we end up with a server down.

I was forced to check XHR requests of the app and see which actions produce duplicated XHR requests and infinite loops.

This done by hand was a nightmare. I realized that the app was doing a lot of requests to the app, and check each one by hand for each action was not a good idea.

How this can be done automatically ?

I found this utility for selenium that at first look great.

First off you have to modify your driver in order to be able to use it, and then you can check requests.

The first implementation of this looked slow and ended up with a tedious code to handle the request.

Puppeteer and intercepted requests

Now the little boy puppeteer comes in action, a "new" tool for automating that gives a lot more options than selenium.

Puppeteer has a great API to handle the request in the browser. So as simple as this:

We can block all XHR of type images to load the page faster.

This is a lot faster and easy than build with browser mob and selenium really.

Proposed solution

The helper method created for puppeteer is this one, that can store duplicated requests and console errors in a file.

And this is a basic use case

Final notes about puppeteer

The control that puppeteer offers is impressive, can handle requests really easy and console error.

Is really more fast than selenium finding and interacting with web elements

The bad part is that javascript debugging is horrible compared to java, I really had a bad time finding where the code was not working and why.

I do not recommend use javascript and puppeteer for a large or complex production framework because you will spend more time trying to figure where the bug is than doing the stuff.

#topic #puppeteer #selenium #api #webDriver #testing

9 views0 comments

Recent Posts

See All