Functional Test with Fake API Data

By

Tessa McDaniel

,

Content Specialist

2022-02-08

I have returned! This time, with something a smidge more complicated: Functional API testing! In case you missed it, I wrote a blog post about it that you can find here, but now I’m trying out API testing within Virtuoso and using API calls in functional tests. I’m not going to lie, there was some trial and error! But I’ll walk you through everything that happened to show you that it’s all part of the learning process.

I definitely thought that setting up an API call would be complicated, but it was surprisingly easy! There’s a dedicated tab for it towards the bottom of the navbar, and you start by creating a folder and a request. For a simple API call, there were three things that I paid attention to: creating an input key, creating a GET call, and formatting the output key. The input key was “url” with the value of the URL for the site that I used to get data from, which was reqres.in, a site full of fake data used to test API calls. Which is great news for me because I had no idea where I was going to get data from before I stumbled on this! Anyway, after creating the key, I used the GET function and pasted in some edited user info found in the Request box on reqres. Finally, I mapped the output key as “response” for the value of $response.

Once the API call was created, it was time to write a functional test! I grabbed the URL from our handy-dandy fake eCommerce site and wrote a very simple test. Navigate to the site, click on the user icon to take us to the login page, call the API to return the response, and type the fake email into the “email” box. I’m not gonna lie, here’s where I ran into a bit of trouble. I pasted in all the info from reqres’ “Request” box, which was “/api/users?page=2” when I should’ve referred to a specific user by entering “/api/users/2”. But that’s really nothing to do with Virtuoso. 

The slight Virtuoso hiccup is when I wrote the step to type the email of the user into the login box. First, I had to find the information tree, and I did this by calling the API first. Then when I clicked on the step and dipped into the “Side Effects” tab, there’s a little data tree under the key response. So once I figured out that I had to write “response.data.data.email” in the Virtuoso step, that’s what I did. Buuuuuut that didn’t work. And the coders among you can probably guess why. Yep, I wrote “type “response.data.data.email” in ““username”” and kept the quotation marks. This meant that Virtuoso typed exactly what was in quotes into the field, which isn’t what I wanted. A few minutes of troubleshooting later and I amended my step to $response.data.data.email so that Virtuoso actually pulled the response from the API call. Then everything worked!

I definitely felt a surge of pride upon figuring this out, because I feel that when working on API testing for the first time, a little whoops is expected. And Virtuoso still made it easy to figure out because of the screenshot function. Virtuoso showed that they wrote “response.data.data.email” in the field instead of the email I wanted, so I knew exactly what the problem was. 

If you’re reading my blog posts for the first time just as you’re getting Virtuoso, then my hope is for you to learn from my trial and error! Virtuoso really is easy to use; the whole learning process from start to finish (including me referred to my cheat sheet) took about 13 minutes, which is pretty good considering that if I wanted to, I could now author a baker’s dozen in the same time frame now that I know what I’m doing. So if I managed to figure this out, what are you waiting for! Let my blog and I become your cheat sheet for setting up API calls, and if you don’t have Virtuoso, then again, what are you waiting for?! Get in touch and see how API testing will work in relation to your site info!

Tags

Building Test Automation
for True Shift Left

How to shift your testing left and build web automation that keeps up with your development.

Download eBook

Subscribe to our Newsletter