Skip to content
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

[Cucumber + v5] How to make hooks (afterStep) to run and finish before next step is executed? #4207

Closed
jorgepOktana opened this issue Jul 16, 2019 · 16 comments · Fixed by #4288
Labels
Bug 🐛 help wanted Issues that are free to take by anyone interested

Comments

@jorgepOktana
Copy link

Hi,
Using wdio v4 + cucumber i used some afterSteps and know im trying to use the same in v5 + cucumber but it seems not to be running in order.
On my afterStep i have certain validations, i.e. if result.status === failed then i take a screenshot and do some extra steps, the problem is i am able to take the screenshot but im never able to do the extra steps because when the afterStep is executed, the runner is already executing another test case.

This is the code im using on my afterStep for the v5 (updated from v4):

afterStep: function (uri, feature, scenario, step, result) {
        if (result.status === 'failed') {
            const path =  './errorShots/'+Date.now()+'.png';
            browser.saveScreenshot(path);
            const allitems = $('.slds-icon-waffle');
            const logoutBut = $('button[title="Logout"]');
            const adminMenu = $('.oneAppLauncherItemList a[title="Parsable Admin"]');
            const sfProfilelogo = $('.profileTrigger');
            const sflogOut = $('.profile-link-label.logout.uiOutputURL');
            this.allitems.waitForDisplayed();
            this.allitems.click();
            this.adminMenu.waitForVisible();
            this.adminMenu.click();
            browser.pause(2000)
            if (this.logoutBut.isVisible()) {   
                this.logoutBut.click();
            }
            this.sfProfilelogo.waitForVisible();
            this.sfProfilelogo.click();
            this.sflogOut.waitForVisible();
            this.sflogOut.click();
        } 
        console.log(step.text + ' is ' + result.status)
    },

Sometimes it successfully takes the screenshot of the page with the error, but the other steps are never completed since when it gets there, the runner is already executing another test case..

**WebdriverIO version: v5.11.2
**Mode:Selenium Standalone
**If WDIO Testrunner, running sync/async: sync
**Node.js version: v10.16.0
**NPM version: v6.9.0
**Browser name and version: Chrome
**Platform name and version: MacOS

How can i ensure the afterStep is completely executed before the next step is executed?
I have tried using promises but im not sure if i was implementing well, any help?

@wswebcreation
Copy link
Member

wswebcreation commented Jul 16, 2019

First of alles there is no afterStep anymore, with the migration from V4 to V5 CucumberJS was also updated. Please check the blog post to see also the reference to the changes in CucumberJS.

Last but not least the payload from a after-hook changed. Please update all code according to the latest versions and please see if you still have an issue.

Going to close it now but feel free to reopen it if you migrated to all the latest versions and the issue is still there

🤦🏼‍♂️, I totally forgot about the config hooks, 😳, my bad

@christian-bromann
Copy link
Member

First of alles there is no afterStep anymore

According to the docs there is an afterStep hook: https://webdriver.io/docs/configurationfile.html

@jorgepOktana
Copy link
Author

jorgepOktana commented Jul 16, 2019

@wswebcreation thanks for your reply but as @christian-bromann mentioned, there is an afterStep for cucumber on the wdio configuration file (according to wdio documentation).

If you see my code above im actually using the hook and it triggers certain actions after every step but it doesnt wait for the hook to finish running to execute the next step... I already updated my code to be compatible to v5 but the actions seem to be called way too late for what I need.

@mgrybyk
Copy link
Member

mgrybyk commented Jul 16, 2019

This is how it currently designed in wdio, there are no issues with your code.

Should we change the behavior? @wswebcreation @christian-bromann

@christian-bromann
Copy link
Member

Should we change the behavior?

Yeah, wdio cucumber hooks should run commands synchronously/respect promises like any other hooks too.

@christian-bromann christian-bromann added Bug 🐛 help wanted Issues that are free to take by anyone interested labels Jul 17, 2019
@jorgepOktana
Copy link
Author

Hello!, sorry to bother!
Is there any updates on this?

@christian-bromann
Copy link
Member

Is there any updates on this?

As you can see you are welcome to contribute a bug fix.

@jorgepOktana
Copy link
Author

Is there any updates on this?

As you can see you are welcome to contribute a bug fix.

Would love to help but actually im not really a good coder, just a QA with the most basic programming knowledge... and actually have no idea how to help with this :(

@christian-bromann
Copy link
Member

and actually have no idea how to help with this :(

No problem. Then we have to wait until someone can tackle this. You will either see a pull request connected here or other communications in this thread.

@jorgepOktana
Copy link
Author

jorgepOktana commented Jul 23, 2019

In the meantime i applied this workaround for my project:

  • Use cucumber hooks on the stepDefinitions file:
After((scenario) => {
    const path =  './errorShots/'+Date.now()+'.png';
    if(scenario.result.status === 'failed'){
        browser.saveScreenshot(path);
        console.log('Screenshot location:', path);
        //do some more actions here (i call a function to logout from an API)
    }
    console.log('Scenario '+  scenario.pickle.name +' '+ scenario.result.status)
    browser.deleteCookies()
    browser.refresh()
    browser.pause(2000)
});

This is executes commands synchronously/respect promises.

Edit: This workaround makes reporters act weird, even the standard wdio reporter is not displaying the right numbers. i:e.- I ran 13 test cases (some passed, some failed) and this is what i got:

Spec Files:	 0 passed, 1 failed, 1 total (100% completed) in 00:12:54

@nextlevelbeard
Copy link
Member

Should we change the behavior?

Yeah, wdio cucumber hooks should run commands synchronously/respect promises like any other hooks too.

In that case, would declaring the hook an async function make it not resolve promises?

@jbblanchet
Copy link

I've looked into this to try and find a solution, and I don't think there's a good generic (i.e. working for both sync/async) way to do this for all the hooks defined in the config file.

Cucumber itself offers only 4 hooks: BeforeAll (which maps to before in the config I think), Before (beforeScenario), After (afterScenario) and AfterAll (after). For these, we could try and use the hook mechanism of Cucumber to respect promises.

All the other hooks are only attached to fire-an-forget events fired by Cucumber. It might be possible to hack around it in sync mode (using browser.call?), but in async mode I don't see a way to make it work properly.

If you have ideas on how this could be done, I could put in some time. However in the meantime, I will document our project config file (which is running in async mode) to note that these hooks should not be used, and to use the Cucumber ones instead.

@miguelyoobic95
Copy link

We have a similar issue, where we run some async code in the beforeFeature hook but the tests do not wait until this is finished before moving onto the first Given step of our feature. Sticking a browser.pause(1500) seems to be sufficient as a workaround but it would be great if the hooks respected the async/await order.

@mgrybyk
Copy link
Member

mgrybyk commented Jul 31, 2019

I think all the cucumber hooks work same way

@mgrybyk
Copy link
Member

mgrybyk commented Jul 31, 2019

Hey guys, here is update.

  • before/after scenario / step will work! 👍
  • before/after feature won't work until it is implemented, however as a workaround we'll use BeforeAll / AfterAll that should work pretty much the same because wdio has dedicated worker per feature file
  • need some hacking for before/after step hooks as far as there are no such hooks implemented in cucumberjs itself add BeforeStep / AfterStep hooks cucumber/cucumber-js#997

@mgrybyk mgrybyk mentioned this issue Jul 31, 2019
5 tasks
@mgrybyk
Copy link
Member

mgrybyk commented Aug 1, 2019

working is in progress, let's proceed conversation in PR #4288

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 help wanted Issues that are free to take by anyone interested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants