Possible to run Playwright outside of test files to dynamically generate tests?

447 Views Asked by At

Is it possible to run Playwright tests outside of a test file - for example, dynamically creating tests based on user input? I'm trying to do something like this to allow users to build their test in the client and run it on the server:

import { test, expect } from '@playwright/test';

export function runner(instructions) {
  test(name, async ({ page }) => {
    await page.goto(instructions.startPage);

    //...
  });
}

but the error I get says:

Error: Playwright Test did not expect test() to be called here.
Most common reasons include:
- You are calling test() in a configuration file.
- You are calling test() in a file that is imported by the configuration file.
- You have two different versions of @playwright/test. This usually happens
  when one of the dependencies in your package.json depends on @playwright/test.
    at TestTypeImpl._currentSuite (/Users/dace/Code/demo/node_modules/playwright/lib/common/testType.js:71:13)
    at TestTypeImpl._createTest (/Users/dace/Code/demo/node_modules/playwright/lib/common/testType.js:77:24)
    at /Users/dace/Code/demo/node_modules/playwright/lib/transform/transform.js:236:12
    at runner (file:///Users/dace/Code/demo/src/core/runner/runner.js:5:3)
    at ingest (file:///Users/dace/Code/demo/src/core/ingestion/ingest.js:16:10)
    at file:///Users/dace/Code/demo/src/index.js:13:3
    at Layer.handle [as handle_request] (/Users/dace/Code/demo/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/dace/Code/demo/node_modules/express/lib/router/route.js:144:13)
    at Route.dispatch (/Users/dace/Code/demo/node_modules/express/lib/router/route.js:114:3)
    at Layer.handle [as handle_request] (/Users/dace/Code/demo/node_modules/express/lib/router/layer.js:95:5)

I'm wondering if this is possible and is just possibly missing some config settings?

0

There are 0 best solutions below