skip to Main Content

I’m creating a new user on a test in Playwright and at next point, I will need to take the id of this new user to to be deleted.
At the end of the code, I’m creating a variable with the id of this user, like this:

const loginResponseJson = await response.json();
const id = loginResponseJson.id;
console.log(id);
console.log("New Created User is: " + id);

And then my question is:
How to pass a variable like an ID to a URL in Playwright?
To delete, I should do like this:

test('Should be able to delete a new created user', async ({ request }) => {
const response = await request.delete(`/public/v2/users/`, {
});

The ID should enter after the URL in request, but I don’t get to put this properly. How I pass this in Playwright?

2

Answers


  1. You may use the Template literals feature in Javascript.

    Template Literals: Use backticks (“) to create a template literal, which allows you to embed expressions inside${}` placeholders rather than the quotes ("") to define a string.

     test('Should be able to delete a new created user', async ({ request }) => {
           const response = await request.delete(`/public/v2/users/${id}`, {
     }); 
    

    Resources:

    https://www.w3schools.com/js/js_string_templates.asp
    https://www.freecodecamp.org/news/template-literals-in-javascript/
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

    Login or Signup to reply.
  2. You could also concatenate the id to the URL string:

    const id = someId; 
    const response = await request.delete('/public/v2/users/' + id);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search