vitalets / playwright-bdd

BDD testing with Playwright runner
https://vitalets.github.io/playwright-bdd/
MIT License
287 stars 33 forks source link

Question: Possible way to create a custom word alike in cucumber-js? #132

Closed zmimi-lael closed 5 months ago

zmimi-lael commented 5 months ago
const { setWorldConstructor, setDefaultTimeout} = require('@cucumber/cucumber');

setDefaultTimeout(60 * 1000);

class CustomWorld {
    count = 0;
    constructor({parameters, attach, scenario}) {
        this.context = {};
        this.variable = 0;
        this.attach = attach;
        this.scenario = scenario;

    }

    setTo(number) {
        this.variable = number;
    }

    incrementBy(number) {
        this.variable += number;
    }

}

setWorldConstructor(CustomWorld);
zmimi-lael commented 5 months ago

Goal is to have a this.context from a step file so it can share some information across different file steps?

vitalets commented 5 months ago

Yes, you can create CustomWorld inherited from BddWorld. More details in docs.

zmimi-lael commented 5 months ago

@vitalets I tried to use the CustomWorld.

image

image

I am implementing my own this._context as a global storage. But, looks like I am missing something. I tried to use the Given<CustomWorld> from the example. Im using js btw.

zmimi-lael commented 5 months ago

@vitalets Hi, good day!

It worked! But not sure why :D I do not add CustomWorld like this Given<CustomWorld>. Since I am using js, I added the **/*.js in the config file and it worked.

image image

vitalets commented 5 months ago

Hi @zmimi-lael ! You are right, Given<CustomWorld> is only for TypeScript, for js you can just write Given(...)

zmimi-lael commented 5 months ago

Thank you so much @vitalets ! Appreciate it.