Keith Jackson
Keith Jackson

Reputation: 3259

Mocking API responses with C# Selenium WebDriver

I am trying to figure out how (or even if) I can replace the API calls made by my app when running a Webdriver test to return stubbed output. The app uses a lot of components that are completely dependent on a time frame or 3rd party info that will not be consistent or reliable for testing. Currently I have no way to test these elements without using 'run this test if...' as an approach which is far from ideal.

My tests are written in C#.

I have found a Javascript library called xhr-mock which seems to kind of do what I want but I can't use that with my current testing solution.

The correct answer to this question may be 'that's not possible' which would be annoying but, after a whole day reading irrelevant articles on Google I fear that may be the outcome.

Upvotes: 1

Views: 1608

Answers (1)

Stefan Laity
Stefan Laity

Reputation: 130

WebDriver tests are End to End, Black Box, User Interface tests. If your page depends on an external gateway, you will have a service and models to wrap that gateway for use throughout your system, and you will likely already be referencing your models in your tests.

Given the gateway is time dependent, you should use the service consumed by your api layer in your tests as-well, and simply check that the information returned by the gateway at any time is displayed on the as page as you would expect it to be. You'll have unit tests to check the responses model correctly.

As you fear, the obligatory 'this may not be possible': Given the level of change your are subject to from your gateway, you may need to reduce your accuracy or introduce some form of refresh in your tests, as the two calls will arrive slightly apart.

You'll likely have a mock or stub api in order to develop the design, given the unpredictable gateway. It would then be up to you if you used the real or fake gateway for tests in any given environment. These tests shouldn't be run on production, so I would use a fake gateway for a ci-test environment and the real gateway for a manual-test environment, where BBT failures don't impact your release pipeline.

Upvotes: 1

Related Questions