skip to Main Content

i am doing below to click

await this.page.getByRole(‘tab’, { name: "’"+statusHolder+"’"}).click();

i am getting error message
waiting for getByRole(‘tab’, { name: ”Plan trip/buy tickets” })

if i hardcode this it works fine.

await this.page.getByRole(‘tab’, { name: ‘Plan trip/buy tickets’}).click();

in the error it is appending extra at the end.

await this.page.getByRole(‘tab’, { name: "’"+statusHolder+"’"}).click();

await this.page.getByRole(‘tab’, { name: ‘Plan trip/buy tickets’}).click();

how can i paramterized the get by role in javascript playwright.

2

Answers


  1. Chosen as BEST ANSWER

    just now i tried with this statement and it worked await this.page.getByRole('tab', { name : statusHolder}).click();


  2. The issue you’re encountering is due to the way JavaScript handles string concatenation and escape characters. When you concatenate strings, especially those containing special characters like /, you need to ensure that the resulting string is correctly formatted.

    Here’s a more robust way to handle this using template literals, which are enclosed by backticks (`) and allow for embedded expressions:

    await this.page.getByRole('tab', { name: `'${statusHolder}'` }).click();
    

    This approach ensures that the string is correctly interpreted without the need for extra escape characters.

    Here’s a complete example:

    const statusHolder = 'Plan trip/buy tickets';
    await this.page.getByRole('tab', { name: `'${statusHolder}'` }).click();
    

    If statusHolder is a variable that contains the string 'Plan trip/buy tickets', this will work correctly without the extra backslashes.

    Alternatively, if you prefer to stick with string concatenation, you can escape the backslashes manually:

    const statusHolder = 'Plan trip/buy tickets';
    await this.page.getByRole('tab', { name: "'" + statusHolder.replace(/'/g, "\'") + "'" }).click();
    

    This ensures that any single quotes within statusHolder are properly escaped.

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