question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] array setup to use in a forEach loop

See original GitHub issue

Context:

  • Playwright Version: 1.21.1
  • Operating System: Mac
  • Node.js version: 16.14.2
  • Browser: not applicable
  • Extra: I use axios as the node module to make api calls as mentioned below.

Code Snippet

Help us help you! Put down a short code snippet that illustrates your bug and that we can run and debug locally. For example:

// helperFunctions.ts

export function usernameGen(length) {
    let result = '';
    const characters = `'你好喂èíàóえこれúñç@들은서ระกาศถ่ายทอดสดศึกฟุตบอล비스호출에추èящий%用語ăをțどん#ね.[]{}`;
    const charactersLength = characters.length;
    for (let i = 0; i< length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength))
    }

    return result;
}

export function usernameArr(arrLength, usernameLength) {
    let arr = [];
    for (let i = 0; i < arrLength; i++) {
        arr.push(usernameGen(usernameLength));
    }
    return arr;
}
// test.spec.ts

test.describe("Custom Username Login Tests", async () => {


   let names = usernameArr(4, 18);


     names.forEach(function (username) {
        test(`save a special character username, username saved is: ${username}`, async () => {
            
            let response = await putSaveUsername(username);

            await expect(response.status).toBe(200);
            await expect(response.data.status).toBe("SUCCESS");
          
        })
    })
})

Describe the bug

Hey guys, this might just be something Playwright can’t do at execution or maybe I just don’t understand but wanted to see if this was a bug or if this is something not supported/won’t be a feature.

I’m doing a test that’s a sort of hybrid API/UI test where I randomly generate some strings and then send those strings to an API to ensure it saves correctly and then on the UI side I ensure that string that was saved can also be used to login with. If you look at the code example above, you can see the setup where within the test file I am calling the function to setup the usernames I want to save. What happens when this starts running is it just constantly runs through this function and doesn’t stop so it never gets to the test execution. If I do NOT call that usernameArr function and instead pass in hardcoded strings into the array, then the test works and runs successfully. However, having the ability to have these randomly generated would be nice but I think Playwright is limiting me doing that with the way it executes.

Let me know if I can do this another way or if this is just something Playwright may not support / won’t support.

Thank you!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aslushnikovcommented, Jun 1, 2022

@bshostak you should remove async from the test.describe callback function, and it should all work:

// test.spec.ts

test.describe("Custom Username Login Tests", () => {


   let names = usernameArr(4, 18);

     names.forEach(function (username) {
        test(`save a special character username, username saved is: ${username}`, async () => {
            
            let response = await putSaveUsername(username);

            await expect(response.status).toBe(200);
            await expect(response.data.status).toBe("SUCCESS");
          
        })
    })
})
0reactions
bshostakcommented, Jun 6, 2022

@aslushnikov wow, consider my brain broken looking at how that function works 😅 . Thanks so much for your help with figuring out this issue! I will go ahead and close this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PHP Foreach Pass by Reference: Last Element Duplicating ...
When the loop gets to the third value of the array, it will contain the value bar because it was just set by...
Read more >
PHP :: Bug #29992 :: foreach by reference corrupts the array
Paul, my guess is that $item is a reference to an element in the $packages array going into this loop. Try using a...
Read more >
Bug: References used in "foreach" loops should be "unset"
When a reference is used in a foreach loop instead of using a simple variable, the reference remains assigned and keeps its "value"...
Read more >
How to Use forEach() to Iterate an Array in JavaScript
1. Basic forEach example ... array.forEach() method iterates over the array items, in ascending order, without mutating the array. The first ...
Read more >
The foreach loop in C++ - DigitalOcean
So basically a for-each loop iterates over the elements of arrays, vectors, or any other data sets. It assigns the value of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found