skip to Main Content

playwright document has parameterize sample like this. It works

const people = ['Alice', 'Bob'];
for (const name of people) {
  test(`testing with ${name}`, async () => {
    // ...
  });
  // You can also do it with test.describe() or with multiple tests as long the test name is unique.
}

however, I want to get data array before each test,and generate test loop

//data like data[] = [{name:'Alice',age:'20'},..]
let data: any[] = []; 

test.beforeEach(async ({ request }) => {
    
    data = await getData(request);
});

for (let i = 0; i < data.length; i++) {
  test(`testing with ${name}`, async (page) => {
    // ...
  });

}

In visual studio code,It shows Error : No Test found

But it works when I assign a exact value let data: any[] =[{name:’Alice’,age:’20’}];

I test similar case
https://github.com/microsoft/playwright/issues/9916
How do I properly run individual Playwright tests from a dynamic array of built urls?

2

Answers


  1. Chosen as BEST ANSWER

    I have a bad solution which is define length of Array

    let data: any[] = new Array(10);
    

    Then This tests can be determined by VScode

    for (let i = 0; i < data.length; i++) {
      test(`testing with ${name}`, async (page) => {
        if(data[i] !== undefined){
             //test
         }
      // ...
     });
    

    }


  2. That’s because you are using beforeEach hook and most probably getData method is assigning undefined value to data or data length is zero.
    I recommend you debugging what’s being assigned to data in beforeEach hook.

    One more important thing: beforEach hook run once before EACH test. In your case,

    1. your test starts with and empty array data;
    2. Your hook works once;
    3. But since data is empty, test run stops and process exits with error: No Test found.

    Solution: Use test.beforeAll

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search