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

Question: On the new version 7, is it still supported to have context with World? #236

Open
zmimi-lael opened this issue Oct 28, 2024 · 2 comments
Labels
question Further information is requested

Comments

@zmimi-lael
Copy link

I do have this before and able to utilize the CustomWorld that I have created.

  • world.js
const { BddWorld } = require('playwright-bdd');

class CustomWorld extends BddWorld {
  constructor(options, page) {
    super(options);
    this._context = {};
  }


}

setWorldConstructor(CustomWorld);
  • Then in my steps, I can set and share the _context values this._context = {};
When(
  "a valid search request",
  async function ({ page, request }) {
    const tests = new SearchText(page);
    this._context["order_search_response"] =
      await tests.searchAPI.callSearchRequest(
        request,
        this._context["valid_token"],
      );
  }
);

Then(
  "the API  returned all  the search criteria",
  async function ({ page }) {
     const tests = new SearchText(page);

    tests.cartSearchAsserts.assertValidResponseWithSchema(
      this._context["order_search_response"]
    );
  }
);

With the use of context, for each worker that I used, they are unique during run time.

@zmimi-lael zmimi-lael added the question Further information is requested label Oct 28, 2024
@vitalets
Copy link
Owner

I've noticed that you use mixed steps style: rely on this and pass fixtures as a first argument to step functions. In general, it's better to follow pure Playwright-style or pure Cucumber-style steps. There are more details here #208 (comment).

If using playwright-style, you can share context between steps by defining _context as a fixture:

import { test as base } from 'playwright-bdd';

export const test = base.extend({
  _context: async ({}, use) => {
    await use({}); // <- _context initialized with empty object
  }
});

Then in steps:

When(
  "a valid search request",
  async function ({ page, request, _context }) { // _context passed as argument
    const tests = new SearchText(page);
    _context["order_search_response"] =
      await tests.searchAPI.callSearchRequest(
        request,
        _context["valid_token"],
      );
  }
);

Or, you can set up full world via fixture, check out this guide.

Alternatively, you can use existing approach. Playwright-bdd provides a fallback for mixed steps style: it always initializes this as empty object, so you can attach _context to it:

When(
  "a valid search request",
  async function ({ page, request }) {
    const tests = new SearchText(page);
    this._context = this._context || {}; // ensure this._context is initialized, because by default `this` is empty object
    this._context["order_search_response"] =
      await tests.searchAPI.callSearchRequest(
        request,
        this._context["valid_token"],
      );
  }
);

In that case, you don't need to define CustomWorld at all.

@zmimi-lael
Copy link
Author

Thank you @vitalets , I will try all these options. I am keen to have one entry, and initialize this._context = this._context || {}; this once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants