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

World does not exit inside setDefinitionFunctionWrapper in Cucumber 3 #937

Closed
koprivica opened this issue Sep 28, 2017 · 8 comments
Closed

Comments

@koprivica
Copy link

koprivica commented Sep 28, 2017

Hi all,
I am using Cucumber 3.0.3 and Node.js 8.5.
I am trying to attach screenshot after each step by using setDefinitionFunctionWrapper, but it looks like world does not exist in it !

Here is my code:

import { browser } from 'protractor';
import {defineSupportCode} from 'cucumber';

defineSupportCode (({setDefinitionFunctionWrapper}) => {

 setDefinitionFunctionWrapper(function (fn) {

    (async () => {
      const screenShot = await browser.takeScreenshot();
      this.attach(screenShot, 'image/png');
    }) ();
    
   return fn;
  });

And here is log:

(node:4669) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'attach' of undefined

Anyone pls help.

@wswebcreation
Copy link

Hi @koprivica

You are using a arrow function which has no this scope. I think that's the problem.

This should do the trick (sorry for the IT-joke ;-))

import { browser } from 'protractor';
import {defineSupportCode} from 'cucumber';

defineSupportCode (({setDefinitionFunctionWrapper}) => {

    (async function () {
      const screenShot = await browser.takeScreenshot();
      this.attach(screenShot, 'image/png');
    }) ();
    
   return fn;
  });

@koprivica
Copy link
Author

Hi @wswebcreation
Thank you for your reply, but issue still remains.

I tried with:

import { browser } from 'protractor';
import {defineSupportCode} from 'cucumber';

defineSupportCode (function (consumer: any) {

consumer.setDefinitionFunctionWrapper(function (fn) {
    (async function () {
      const screenShot = await browser.takeScreenshot();
      this.attach(screenShot, 'image/png');
    }) ();
    
   return fn;
  });

and got the same:
(node:14846) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'attach' of undefined

@wswebcreation
Copy link

wswebcreation commented Sep 28, 2017

Hmm,

You are using cucumber i.c.m. with Protractor. When do you want to attach the screenshot? This is for example my implementation and it works as a charm

https://github.com/wswebcreation/protractor-cucumber-typescript-boilerplate/blob/master/e2e-tests/config/helpers/after.scenario.ts

It attaches the screenshot to the report and saves the image to the filesystem (the last can be removed). In my case I only save a screenshot in an After-hook which still has the world (this) context and only when it is failed.

Personally I never worked with the setDefinitionFunctionWrapper, but based on the docs I can't find that it has it's own World, see here. So maybe there you will find the rootcause of your problem

@gd46
Copy link
Contributor

gd46 commented Sep 29, 2017

I think because your trying to access this.attach within a closure. I believe the reference to this is different and won't have access to world. You need to rewrite to remove closure.

@charlierudolph
Copy link
Member

setDefinitionFunctionWrapper was added in order to deal with steps defined as generators (in order to wrap them in something that can properly run them). I would not suggest using it for attaching a screenshot after each step. I think we need a new API if you want to do something after each step.

@wandroll
Copy link

Steps Hooks would have been convenient for this use case. Too bad we removed them.

@charlierudolph
Copy link
Member

charlierudolph commented Jan 4, 2018

Closing in favor of #997 as I'm good adding the step hooks back in. They can be built as first class citizens instead of being dependent on an internal

@lock
Copy link

lock bot commented Jan 4, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants