playwright-bdd icon indicating copy to clipboard operation
playwright-bdd copied to clipboard

Feature: support several custom test instances

Open vitalets opened this issue 2 years ago • 1 comments

Describe the problem Currently only one custom test instance can be provided via importTestFrom option. In many cases this is no enough. Depending on project structure, different groups of tests may require own auto-fixtures, not applicable for other tests. Even without auto-fixtures sometimes it's more convenient to keep fixtures separated instead of having all-in-one test.extend.

Imagine we have an app that has 3 main parts: user login, todo list and administration of uses. Each part could have own Page Object Models and fixtures. Example structure:

features
    login.feature
    todolist.feature
    admin.feature
steps
   Login
      LoginPage.ts
      LogoutPage.ts
      fixtures.ts
   TodoList
      TodoListPage.ts
      TodoDetailsPage.ts
      fixtures.ts
   Admin
      UserList.ts
      UserDetails.ts
      fixtures.ts

Currently to run such tests with playwright-bdd we need to combine all fixtures.ts into one and point to it with importTestFrom.

Describe the solution The solution I'm thinking about - is to refuse using importTestFrom at all. Technically, when we know all steps used in particular feature file, we can detect what test instance should be imported. To achieve it we need to switch from Cucumber's steps loading to own implementation (that is already used for decorator steps). To make it backwards compatible I can add new config option steps: string | string[] that should point to all steps definitions including fixtures. When steps option is defined, no need for Cucumber's require or import options and laso no need for importTestFrom. The config will looks very simple:

const testDir = defineBddConfig({
  paths: ['features'],
  steps: './steps',
});

Describe alternatives you've considered Alternatively we can allow passing object to importTestFrom mapping different features with different test instances, e.g.:

const testDir = defineBddConfig({
  importTestFrom: {
    'features/Login': 'features/Login/fixtures.ts',
    'features/TodoList': 'features/TodoList/fixtures.ts',
    'features/Admin': 'features/Admin/fixtures.ts',
  },
  paths: ['features'],
  steps: './steps',
});

This is less convenient as we need manually sync changes in files structure with config.

vitalets avatar Aug 11 '23 16:08 vitalets

Playwright 1.39 introduced mergeTests helper that merge several test instances into one.

vitalets avatar Nov 04 '23 11:11 vitalets

Playwright-bdd v7 brings auto-detection of importTestFrom that allows to omit this option in most cases. The importTestFrom option is not removed, because there still can be situations where it is required. Feel free to create new issue if there are any problems with it.

vitalets avatar Jul 22 '24 15:07 vitalets