playwright icon indicating copy to clipboard operation
playwright copied to clipboard

Need to understand how can I wait for a promise to end and then execute the tests function

Open elementarymindscape opened this issue 1 year ago • 1 comments

I was working to write tests by reading an xlsx file, the parser I am using exceljs reads file using promises.

So the data that is returned from the function is an async call, so by the time the function executes, the tests() function of playwright returns no tests found. If I am trying to use readDataFromExcel().then(()=>{}) it returns no tests found, but the moment I add another test separately, the compiler gives an error saying Running 1 test using 1 worker

Running 1 test using 1 worker

{user}/node_modules/@playwright/test/lib/common/testType.js:70
      throw new Error([`Playwright Test did not expect ${title} to be called here.`, `Most common reasons include:`, `- You are calling ${title} in a configuration file.`, `- You are calling ${title} 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.`].join('\n'));
            ^

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 ({user}/node_modules/@playwright/test/lib/common/testType.js:70:13)
    at TestTypeImpl._createTest (/{user}/node_modules/@playwright/test/lib/common/testType.js:76:24)
    at {user}/node_modules/@playwright/test/lib/common/transform.js:226:12
    at forEach ({user}/tests/questionnaire.spec.ts:52:13)
    at Array.forEach (<anonymous>)
    at {user}/tests/questionnaire.spec.ts:50:18

Node.js v18.15.0

I am looking to know how can I wait for my array to be assigned the data after the promise finishes reading the excel file and then I can iterate over it for the test() function on playwright.

Any help on this is appreciated, Thanks!

elementarymindscape avatar May 19 '23 19:05 elementarymindscape

@elementarymindscape IIUC, you are trying to generate your test cases dynamically. This is not supported. I'd recommend one of the two workaround:

  • Make a single test() that will load data and dynamically run multiple step with test.step(). Essentially converting multiple tests into a single one.
  • Load data asynchronously before tests, either as a separate command, or in your globalSetup, and save it to a json file. Then in your test file, fs.readFileSync() the data and generate tests dynamically.

Let me know whether this helps.

dgozman avatar May 19 '23 19:05 dgozman

It seems like the issue has been resolved. If you still encounter problems, please file a new issue with a repro and link to this one.

dgozman avatar May 22 '23 15:05 dgozman