I have setup cucumber to handle both service (e.g. web service API) level testing and UI (e.g. browser DOM) level testing.
I do this using tags. I tag each API level scenario with the @api tag. In my @Before scenario hook is an if statement which checks if scenario.getSourceTagNames() contains @api. If @api is not detected then driver is initialized and a browser instance is created.
Since my tests are using the PageObject paradigm the step definitions are shielded from each page's HTML details. That lead me to wonder if I could use dependency injection to control which of two sets of PageObject classes get loaded. The first set would bypass the Selenium browser and directly hit server endpoints to obtain values to be processed and sent back to the step definitions. The second set would load the selenium PageObject classes and go through the browser to satisfy step definition calls. Clearly the first set would execute in less (elapsed) time than the second.
So I began an internet search to see what others have done. I ran across this blog post by Aslak Hellesøy. Aslak has this to say:
*"Cucumber power users who understand the benefits of the test pyramid will prefer to run most scenarios against the middle layer (without going through a UI). Then they'll pick a small subset of the same scenarios to run through the UI and run those separately.
If you keep all the automation logic behind an AutomationApi interface you can provide two different implementations - one that talks directly to the domain model, and another one that uses Selenium WebDriver or another UI automation library."*
Given a Jenkins CI server, step 1 is build the software, step 2 is run unit tests, step 3 is run the domain (Web server API) tests and step 4 is run the UI (e.g. web) tests. Thus the testing pyramid is realized.
Has anybody implemented Aslak's idea? If yes, was there actual overlap between scenarios? That is, did you run the same scenarios both with an API implementation of PageObjects (say in your dev environment) and also with an HTML (Selenium) PageObject implementation (say on the CI)? Or were the test scenarios exclusively API xor web?