vitalets / playwright-bdd

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

Question: Docstring transformation #176

Open viktor-silakov opened 1 month ago

viktor-silakov commented 1 month ago

Is it possible to use a similar to the defineParameterType transformation for all docstrings?
Or maybe you can suggest some workaround? I want to add some templating system across all docstrings.

vitalets commented 1 month ago

Could you provide an example?

viktor-silakov commented 1 month ago

@vitalets Yes sure, for example, I have step:

When I fill out the form:
"""
name: [random username]
email: [random email]
"""

And make some callback 'fillPlaceholders' which should render this template and give me the output:

name: John
email: john@example.com

Under the hood...

For usual string parameters, I can use defineParameterType transformation but for docstrings - can't

vitalets commented 1 month ago

At what moment fillPlaceholders should be called? During spec files generation or in runtime? If in runtime - looks like it's custom content type for doc strings, I see it in cucumber-jvm (and don't see in cucumber-js).

I think API can be the following:

defineDocStringType({
  name: 'placeholders', // if empty -> applied to all docStrings
  transformer: (s) => fillPlaceholders(s, { name: 'John', email: 'john@example.com' });
});

And in feature file:

When I fill out the form:
"""placeholders
name: [random username]
email: [random email]
"""
viktor-silakov commented 1 month ago

Yes, it would be good to have this API on runtime.

vitalets commented 1 month ago

Ok. Lets track in this issue. For now the only workaround I see is to make transformation inside step:

When("I fill out the form:", async ({ page }, docStringTpl: string) => {
  const docString = fillPlaceholders(docStringTpl, { name: 'John', email: 'john@example.com' });
  // ...
});