Skip to content

Fix work with promise chains #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Jun 28, 2017
Merged

Fix work with promise chains #568

merged 33 commits into from
Jun 28, 2017

Conversation

APshenkin
Copy link
Collaborator

@APshenkin APshenkin commented Jun 22, 2017

Our goals:

  • Fix After block not properly executed if Scenario fails #543 - After block not properly executed if Scenario fails
  • Expected behavior in promise chains: _beforeSuite hooks from helpers -> BeforeSuite from test -> _before hooks from helpers -> Before from test - > Test steps -> _failed hooks from helpers (if test failed) -> After from test -> _after hooks from helpers -> AfterSuite from test -> _afterSuite hook from helpers.
  • if during test we got errors from any hook (in test or in helper) - stop complete this suite and go to another
  • if during test we got error from Selenium server - stop complete this suite and go to another
  • if restart option is false - close all tabs expect one in _after (ready for webdriverIO, SeleniumWebdriver and Protractor. For Nightmare it will be hard to add (I found only this way https://github.com/rosshinkley/nightmare-window-manager)
  • Complete _after, _afterSuite hooks even After/AfterSuite from test was failed
  • Don't close browser between suites, when restart option is false. We should start browser only one time and close it only after all tests.
  • Close tabs and clear local storage, if keepCookies flag is enabled

Bonus:

@DavertMik
Copy link
Contributor

Please fix test/runner/interface_test.js, it tests the order of events

@APshenkin APshenkin changed the title Run after block after test fails [WIP] Fix work with promise chains Jun 23, 2017
@DavertMik
Copy link
Contributor

Wow, awesome work. We are getting closer and closer to 1.0 release!

@@ -45,6 +49,12 @@ module.exports = function (suite) {
suites.shift();
}
if (!opts) opts = {};

afterAllHooks = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was already defined at line 27

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@DavertMik
Copy link
Contributor

Please try to use interactive shell to check that nothing was broken for it.
Also, execute one test with within block to be sure this changes to recorder doesn't break them.

@APshenkin
Copy link
Collaborator Author

APshenkin commented Jun 25, 2017

@DavertMik
During these changes I found one interaction that may be not correct. I want to ask your advice about it.
Now we have clear promises chain as described in topic. It will break only after fail. Also if we get error in BeforeSuite, Before or After hooks we will skip all tests in this suite (looks it's ok, because there is no sense to try repeat test with the sameBeforeSuite/Before/After if it's not working). But there is an interesting ways with After and AfterSuite hooks.
If After hook from test will fail, then _after hooks from helpers will not be executed.
The same thing with AfterSuite - if it fails, then _afterSuite hooks from helpers will not be executed. It looks normal, but this can cause an issue, that after failure in these hooks, browser will not close or we will not clear environment for next tests.

I think, that maybe we can track this and force close/clear browser in this cases.

What do you think? Should we track this, or it should be resolved from user side?

@DavertMik
Copy link
Contributor

DavertMik commented Jun 25, 2017

If After hook from test will fail, then _after hooks from helpers will not be executed.

Yes, this makes a big problem for closing browser sessions...
I have 2 ideas about this:

  1. run _after hook for Helper even test has failed in After. Same for AfterSuite. I think it is better to have all _after hooks to be executed no matter what happened.
  2. add new finalize hook.

But this is important to solve it in one way or another.

@APshenkin
Copy link
Collaborator Author

@DavertMik please review the changes. pause() and within block works as expected

@APshenkin APshenkin changed the title [WIP] Fix work with promise chains Fix work with promise chains Jun 26, 2017
* I.closeTabsExceptForOne();
* ```
*/
closeTabsExceptForOne() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to rename:

closeOtherTabs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -22,6 +22,7 @@ class Step {
}

run() {
this.startTime = new Date();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this date-time counter should be moved out to https://github.com/Codeception/CodeceptJS/blob/master/lib/listener/steps.js

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we place it in steps.js then we can't pass this value properly between states. Or I don't know how to do it. If you have any suggestion, it will be awesome

lib/scenario.js Outdated
event.emit(event.test.before);
};

module.exports.teardown = function () {
if (!recorder.isRunning()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you repeat this 3 lines too much.
better to make a method recorder->startUnlessRunning()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -11,6 +11,14 @@ module.exports = {
passed: 'test.passed',
failed: 'test.failed',
},
hook: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is a bit confusing. Ok, these are special events fired by a failing after and afterSuite.

Ok, let's try to clarify their context. Those methods should not fail, so if they do it is an error.
So I propose to have next events: test.after.error, and suite.after.error instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't use test.after/suite.after, because they are already defined. I suggest test.afterError, suite.afterError

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test.afterError, suite.afterError

good

lib/actor.js Outdated
val = step.run.apply(step, args);
val = step.run.apply(step, args).then(() => {
step.endTime = new Date();
output.log(`-> ${step.toString()} was executed in ${(step.endTime - step.startTime) / 1000} sec`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this code to event listener

@@ -88,7 +95,18 @@ module.exports = function (suite) {
fn = opts;
opts = {};
}

if (!afterAllHooksAreLoaded) {
afterAllHooks.forEach(function (hook) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some comments about what issues does it solve

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comment in test.
FYI, we collect all hooks in right order in array and then push them to suite. We start collecting hooks from helpers and after from testfile. this cause hooks reordering
(expected: After from test -> _after hooks from helpers -> AfterSuite from test -> _afterSuite hook from helpers.
actual: _after hooks from helpers -> After from test -> _afterSuite hook from helpers. -> AfterSuite from test )

This change fix this chain

@DavertMik DavertMik merged commit a79a725 into master Jun 28, 2017
@DavertMik DavertMik deleted the use-after-when-fail branch June 28, 2017 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants