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

cant send anything from wdio mocha after hook #138

Open
sgordiev opened this issue Jan 19, 2021 · 16 comments
Open

cant send anything from wdio mocha after hook #138

sgordiev opened this issue Jan 19, 2021 · 16 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@sgordiev
Copy link

The problem

Nor sendLogToTest neither sendFileToTest works with mocha wdio from afterHook. Error is next: Can not send file to test "after all" hook for... In my case I use mocha before and after for log in and log out. When something is wrong with that functionality I want to have some extra debug info.

I think issue is that test item for hooks is not created at that time because of this logic in node_modules/wdio-reportportal-reporter/lib/reporter.ts:

  private onHookStart(hook: any) {
    log.trace(`Start hook ${hook.title} ${hook.uid}`);
  }

  private onHookEnd(hook: any) {
    log.trace(`End hook ${hook.title} ${hook.uid} ${JSON.stringify(hook)}`);
    if (hook.error) {
      const testItem = this.storage.getCurrentTest();
      if (testItem === null) {
        this.onTestStart(hook, TYPE.BEFORE_METHOD);
      }
      this.testFinished(hook, STATUS.FAILED);
    }
  }

As you can see onHookStart doesn't create any test item. And if I am not mistaken this hook starts before client's beforeHook. And onHookEnd executes after client's afterHook so code in clients after hook will try to send info to test item that not exists yet. If update onHookStart with this.onTestStart(hook, TYPE.BEFORE_METHOD); that will add hooks to all reports but also will enable sending info to that item.

Environment

  • WebdriverIO version:6.10.5
  • wdio-reportportal-version version:6.1.0
  • Node.js version:v14.15.3
  • WDIO Testrunner and version(mocha\cucumber\jasmine):mocha 6.10.4
  • Additional wdio packages used (if applicable):

Code To Reproduce Issue [ Good To Have ]

const reportportal = require('wdio-reportportal-reporter');
const RpService = require("wdio-reportportal-service");
/**
* Loades all typedi containers before tests starts
*/
const path = require('path');
const fs = require('fs');
  afterHook: async function (test, context, { error, result, duration, passed, retries }/*, stepData, world*/) {
    if (error&&(error.message !=='sync skip; aborting execution')) {
      const filename = `screnshot_${Date.now()}.png`;
      const outputFile = path.join(__dirname, filename);
      await browser.saveScreenshot(outputFile);
      await reportportal.sendFileToTest(test, 'info', filename, fs.readFileSync(outputFile));
    }
  },
@BorisOsipov
Copy link
Owner

Yes I intentially not report hooks.
Wdio hooks reporting with suppporting mocha+jasmine+cucumber with correct handling failed hooks and retries is hell. See for example allure imlementation that still has bunch of bugs
https://github.com/webdriverio/webdriverio/blob/master/packages/wdio-allure-reporter/src/index.ts#L260

@sgordiev
Copy link
Author

honestly I cant undestand why one need to exclude hooks from report. I mean I undestand that them want read less, but mocha or junit, jasmine won't care about such 'less reading' and will always show them in console.I think all reporters should repeat that logic. And if cucumber doesn't show before in its console then I think there might be a check isCucumber in both onHookStart and onHookEnds that keeps old logic instead check if mocha

@BorisOsipov
Copy link
Owner

honestly I cant undestand why one need to exclude hooks from report

I can easily explain. I don't have enough time to implement a solid solution for it. There are a lot of corner cases that must be handled and tested. That's why I report only failed hooks.

@sgordiev
Copy link
Author

sgordiev commented Jan 20, 2021

But you have a broken functionality. When hook is failed you can't show what happened. I'd rather always show it in report than have this functionality cut off. Without check on as your have shared in allure( for some reason them added 'disableMochaHooks' flag for mocha to not to show before hooks and I think thats why have that problem now with lots of bugs). Please let me know if there is a way to add screenshots to hooks

@BorisOsipov
Copy link
Owner

Don't get me wrong I understand your concerns.

nope

@sgordiev
Copy link
Author

so for those who came with the same question - your answer - won't fix ? And if one need that one should create his own report portal service ?

@BorisOsipov
Copy link
Owner

I didn't say "won't fix". For sure it is lack of functionality. I don't have time to implement it. It is an open source. You can research\implement\test it and make a pull request. But make sure it should work not only for mocha and not only in straight cases.

@BorisOsipov
Copy link
Owner

main issue here - if you don't handle hooks right you can easy broke whole suite reporting because you will break testitems tree - it is worse than don't have screen in hooks.

@sgordiev
Copy link
Author

ok, right now I my project is in POC stage with report portal, I will create a custom service with the fix, and after when/if we choose it as our solution, and project give me time on the fix, I will try to provide a fix, or when I have my free time

@BorisOsipov
Copy link
Owner

That will be awesome. If you will need help feel free to ask here or in report portal slack

@BorisOsipov BorisOsipov added enhancement New feature or request help wanted Extra attention is needed labels Jan 21, 2021
@sgordiev
Copy link
Author

sgordiev commented Jan 22, 2021

need your help. When I add the logic to start hook to enable sand the logs, it doesn't find that hook with sendFileToTest, and it seams hook name in afterHook(in wd.conf.js - client side) and in onHookStart onHookEnd(in reporter.js) has a different title. I mean startedTest.wdioEntity.title(from storage) === test.title(from wd.conf.js). So the logic of finding item by title works only for test and suite but not for hook for some reason. And error message is send to hook cause it doesn't use logic of finding by title but rather bu internal id you are assign this.client.sendLog(testItem.id....

Bottom line. If I than use await reportportal.sendFile( 'info', filename, fs.readFileSync(outputFile)); in 'afterHook' it works cause it also looks for 'testItem.id' instead of 'title' match

startedTest.wdioEntity.title "after all" hook for Check ads on LiveCoveragePage auth
test.title(from wdio.config) "after all" hook for "should have Symbol(AD_LIVECOVERAGE)"

Check ads on LiveCoveragePage auth - is a suite name
"should have Symbol(AD_LIVECOVERAGE)" - is a test name

@BorisOsipov
Copy link
Owner

hook name in afterHook(in wd.conf.js - client side) and in onHookStart onHookEnd(in reporter.js) has a different title.

That's true. Seems you can create issue in wdio repo to fix it.

And error message is send to hook cause it doesn't use logic of finding by title but rather bu internal id you are assign this.client.sendLog(testItem.id....

Yes. It is ok.

  1. reporter.sendLog(level, message) should attach log to current testitem( uses in case when you don't know current testitem name or don't care)
  2. reporter.sendLogToTest(test, level, message) should attach log to specific testitem with test title. It uses when you know title and want add logs to specific testitem

@sgordiev
Copy link
Author

ok thanks, will create an issue for wide. Thanks for help, will be back with its url

@BorisOsipov
Copy link
Owner

from #139

I don't know how we should report per-file after suite mocha hooks. In fact they are not bounded to some reportportal suite and when they are executed there is no active suite item and there is no place to report them...

If I'm not mistaken allure just ignore them, see https://github.com/webdriverio/webdriverio/blob/main/packages/wdio-allure-reporter/src/index.ts#L278

Example:

describe('My suite 1', () => {
///
});
describe('My suite 2', () => {
///
});
describe('My suite 3', () => {
///
});
after(() => {
    //will throw error
})

@sgordiev
Copy link
Author

that would be step number 2 to think how to deal with it, at the moment regular hook doesn't work. After I fix that I will return to this issue

@sgordiev
Copy link
Author

if any I opened a ticket webdriverio/webdriverio#6336

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants