Monday, December 11, 2017

Stubbing API with webdriver and cucumber

Leave a Comment

I want to run some BDD tests against my application using Webdriver configured with Cucumber, but would like to stub out my API. Preferably I'd like to not use Express, as I want my CI server to execute these tests and Express would add complexity.

I haven't been able to find any guidance online about injecting nock or fetch-mock into the browser context from wdio. I would like to setup my stubbed calls before each scenario, and then clean things up after the scenario.

Here are the relevant bits of my wdio.conf.js. Obviously it won't work as written, but I hope illustrates what I would like to do. If anyone has useful advice the deviates from this direction, I'd still love to hear it.

/**  * Gets executed before test execution begins. At this point you can access to all global  * variables like `browser`. It is the perfect place to define custom commands.  * @param {Array.<Object>} capabilities list of capabilities details  * @param {Array.<String>} specs List of spec file paths that are to be run  */ before: function (capabilities, specs) {     fetchMock = require('fetch-mock');      browser.execute(() => {         injectFetchMockSomehow(fetchMock);     }, fetchMock);  },  /**  * Runs before a Cucumber scenario  * @param {Object} scenario scenario details  */ beforeScenario: function (scenario) {     fetchMock.get('/foo', {hello: 'world'}); },  /**  * Runs after a Cucumber scenario  * @param {Object} scenario scenario details  */ afterScenario: function (scenario) {     fetchMock.reset()     fetchMock.restore(); }, 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment