As a Test Automation Engineer, how would you test API calls failing in your app without them actually failing?
How can you test for failing API requests with your selenium tests?
Let's imagine we want to test in a frontend SPA Application if there's a dialog error that appears whenever an API call fails - depending on the correct feature.
The reason we only want to simulate the API error on the client-side rather than modifying the actual backend is, that if we're using a dev or acceptance environment, other people can't use them.
Nevertheless, this approach is not precise enough. I'm not targeting the exact process, which is the API HTTP Requests but stopping everything.
Is there a better way?
Selenium Network Interception has a feature that enables us to intercept the network. In theory, we should be able to intercept the API request and modify it so that the request is null or invalid. This will trigger an error state in the application. Nevertheless, I've not tested it yet because there's no support for C#.
HTTP Response Codes
Using Selenium WebDriver for directly manipulating responses is considered an anti-pattern. You can nevertheless use Selenium RC as a proxy to do so.
If you insist, an advanced solution to capturing HTTP status codes is to replicate the behaviour of Selenium RC by using a proxy. WebDriver API provides the ability to set a proxy for the browser, and there are a number of proxies that will programmatically allow you to manipulate the contents of requests sent to and received from the web server. Using a proxy lets you decide how you want to respond to redirection response codes. Additionally, not every browser makes the response codes available to WebDriver, so opting to use a proxy allows you to have a solution that works for every browser.
We can also use other proxies in between the tests with tools like BrowserMob as illustrated here. This can be a complicated process though, often requiring technical skills, especially in programming languages like Java. Even the use of Selenium RC can be intimidating especially if you're going to use it with CI/CD.
On probing further, I've discovered you can actually use your Browser's Developer's tools with Selenium. I know from experience as a frontend developer, that using this tool, you can do a lot of stuff including manipulating the network requests such as blocking an API call for instance.
Using the Debugging tool, you can set the network as normal, and then load the page. Once done, before doing any API call, you change the network condition such as putting it offline. This should trigger the error as expected in the application.
An even more specific approach is to block the exact API call or at least the domain name. However, I've not tested this yet and if it works, I'll update this post.