BorisOsipov / wdio-reportportal-reporter

A WebdriverIO plugin. Report results to Report Portal.
MIT License
23 stars 29 forks source link

cant send anything from wdio mocha after hook #138

Open sgordiev opened 3 years ago

sgordiev commented 3 years ago

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

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Don't get me wrong I understand your concerns.

nope

sgordiev commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

sgordiev commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

BorisOsipov commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

if any I opened a ticket https://github.com/webdriverio/webdriverio/issues/6336