How to Take Over the World
or .... How to API all the things
· testing,apis
As software testers, we often feel forced to write website test automation at the highest level possible, pulling up an actual browser window, clicking elements, entering text, etc. Selenium has become one of our oldest friends, whose company we enjoy, while at the same time wanting to minimize our time with them. We joke about our love/hate relationship, and we know it is always there when we need it.
Recently, we wanted to automate some tasks in a web site, but not for the testing itself, only to set up some data to be used in the test. This web site was a monolith, with no clean way to set data up under the hood (just trust me on that, but we can talk about it if you want to).
We were testing a mobile app, and thus did not want to have to separately run a fragile Selenium script just to set up data (sorry, Selenium, love you but #sorrynotsorry). What we did instead was write a very simple javascript library that used http requests directly to interact with the site. We found that using some simple developer tools and Postman, it was INCREDIBLY SIMPLE.
Open your target site site in a browser and open developer tools to the Network tab, like this:
Then, do the thing. Once you have done the thing, look for the network call that actually did the action (usually a GET or a POST) (it may be helpful to clear the network tab just before you do the action to help narrow down what you are looking for).
Right-click it and choose "copy" -> "copy as cURL"
Open Postman (a free tool for calling api's directly and repeatedly). At the top left, click Import and then choose "Paste Raw Text"
"BUT WAIT!" you say? Yeah, I saw that too. I think that's a bug in Postman. The action says "GET", but we gave it a "POST". Go on about your bad self and just change that.
You now have that call as a Postman interface. You can choose the Headers tab and manipulate what's in there, or Body, there may be things in there. Different web sites require different headers, so we usually just experiment there.
Once you have that configured how you want, you can export the call as code. Look for the word "Code" in the upper right of the Postman screen and then you can choose from many different languages.
Tada! Now just copy that code and paste it into your favorite editor.
Making the future come sooner!
The UN Team is ALL IN