From 8b1c63b111d759529c5cf10186823d3500410ff9 Mon Sep 17 00:00:00 2001 From: Artem Vasiliev Date: Mon, 5 Feb 2018 02:33:43 +0300 Subject: [PATCH] feat: added some more lifecycle support (#36) * feat: added some more lifecycle support: * be able to do action steps multiple times as a warm up * be able to do smth after prewarm, e.g. pause * close browser in case the assertions report errors, too * docs: browser closing can be switched off * docs: minor wording * fix(tests): corrected - browser is closed automatically now * refactor(flow): removed `closeBrowser` option It was useful but uglysh: we start browser outside of `flow`. So let's have better docs instead. * docs(flow): removed `closeBrowser` option --- docs/api.md | 22 ++++++++++++++-------- lib/index.js | 37 +++++++++++++++++++++++++------------ readme.md | 6 +++++- test/tests.js | 4 ---- 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/docs/api.md b/docs/api.md index ca73c18..8566b2a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -32,7 +32,7 @@ var driver = drool.start({ The next step is to define a flow. A [flow](#flow) is a declarative hash where you define actions at given points in the lifecycle of your drool tests. ```js -return drool.flow({ +var res = drool.flow({ setup: function() { driver.get('file://' + path.join(__dirname, 'examples/', 'leaking.html')); }, @@ -48,7 +48,12 @@ return drool.flow({ Once you are done interacting with the driver, you then will want to quit the driver (to close the browser). ```js -driver.quit(); +res +.then(() => driver.quit()) +.catch(e => { + driver.quit(); + throw e; +}) ``` ### API @@ -76,13 +81,14 @@ The `flow` method returns a Promise, that will be resolved (or rejected) after t the "flow" action object is a set of life cycle key value pairs that will be invoked in the following order. 1. `setup` -2. `action` (to prewarm any DOM/Listener cache) -3. Initial Measurement is taken aftter via [getCounts](#getcounts) -4. `action` * `repeatCount` times (repeat count defaults to 5) -5. `beforeAssert` -6. Final Measurement is taken after via [getCounts](#getcounts) -7. `assert` +2. `action` * `prewarmRepeatCount` (to prewarm any DOM/Listener cache) +3. Initial Measurement is taken after via [getCounts](#getcounts) +4. `afterPrewarm`, e.g. `() => driver.sleep(1000)` if it worth pausing before getting initial counts +5. `action` * `repeatCount` times (repeat count defaults to 5) +6. `beforeAssert` +7. Final Measurement is taken after via [getCounts](#getcounts) 8. `exit` +9. `assert` Each step in the flow, **except for assert**, is optional. Keep in mind however that your flow should cleanly `exit`, and `action` should be able to be invoked an unlimited number of times. diff --git a/lib/index.js b/lib/index.js index daa51e4..3d30f1e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -95,30 +95,43 @@ function sumGcCounts(traces, last) { } function flow(set, driver) { - var initial = []; + var initialCounts; if (!driver) { throw new Error(driverErrorMessage); } - set.repeatCount = (typeof set.repeatCount === 'undefined') ? 5 : set.repeatCount; + set = Object.assign({repeatCount: 5, prewarmRepeatCount: 1}, set); executeInFlow(set.setup); // prewarm the cache - executeInFlow(set.action); + console.log('warming up'); + for (var i = 0; i < set.prewarmRepeatCount; ++i) { + executeInFlow(set.action); + } - getCounts(driver) - .then(initial.push.bind(initial)); + executeInFlow(set.afterPrewarm).then(() => { + console.log('getting initial counts'); + getCounts(driver) + .then(data => initialCounts = data) + .then(() => console.log('main testing')); + }); for (var i = 0; i < set.repeatCount; ++i) { executeInFlow(set.action); } - executeInFlow(set.beforeAssert); + var afterCounts; - getCounts(driver) - .then(function(data) { - set.assert(data, initial[0]); - }); - - return executeInFlow(set.exit); + executeInFlow(set.beforeAssert) + .then(() => { + console.log('getting result counts'); + return getCounts(driver) + }) + .then(data => afterCounts = data) + .then(function() { + return executeInFlow(set.exit) + }) + .then(function() { + set.assert(afterCounts, initialCounts); + }) } function start(opts) { diff --git a/readme.md b/readme.md index e415d09..0f6363e 100644 --- a/readme.md +++ b/readme.md @@ -60,8 +60,12 @@ drool.flow({ assert.equal(initial.counts.nodes, after.counts.nodes, 'node count should match'); } }, driver) +.then(() => driver.quit()) +.catch(e => { + driver.quit(); + throw e; +}) -driver.quit(); ``` [View the API Docs](docs/api.md) diff --git a/test/tests.js b/test/tests.js index 6e1376c..ac3ca3c 100644 --- a/test/tests.js +++ b/test/tests.js @@ -15,10 +15,6 @@ describe('memory tests', function() { this.driver = drool.start(config); }); - afterEach(function() { - return this.driver.quit(); - }); - it('inputs should not leak when added and removed', function() { var self = this;