Skip to content

Commit

Permalink
feat: added some more lifecycle support (#36)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
artemv authored and samccone committed Feb 4, 2018
1 parent 9a2471b commit 8b1c63b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
22 changes: 14 additions & 8 deletions docs/api.md
Expand Up @@ -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'));
},
Expand All @@ -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
Expand Down Expand Up @@ -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.

Expand Down
37 changes: 25 additions & 12 deletions lib/index.js
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Expand Up @@ -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)
4 changes: 0 additions & 4 deletions test/tests.js
Expand Up @@ -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;

Expand Down

0 comments on commit 8b1c63b

Please sign in to comment.