Skip to content

sqass/playwright-api-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

playwright-api-testing

Installing and Running an Express Server

Prerequisites

Before you begin, make sure you have the following prerequisites installed on your system:

  • Node.js: Express.js is built on top of Node.js, so you'll need Node.js to run Express applications. You can download and install node from this link

Installation

  1. Clone the repository
  2. Navigate to api-server directory from terminal
  3. Run npm init -y to install necessary packages
  4. Start the Express server by running the following command node app.js

Running playwright tests

  1. Navigate to candy-api-tests directory
  2. Run npm init -y to install necessary packages
  3. Run npx playwright test to run the necessary tests

Lesson 1 - GET

  1. Compose a GET request for the API endpoint api/Candy?storeName=Target to fetch candy details from the store. Verify the HttpStatusCode is 200 and ensure that the Quantity is 35. Refer to the video for guidance on writing the test

Practice Tests :

  1. Now, create another test with Kroger as input and verify whether the HttpStatusCode is 404 or not and whether the response body contains the storename or not
Click to reveal answer
        test('When no candy details present in store', async ( {request})=> {
            const response = await request.get('http://localhost:3000/api/Candy?storeName=Kroger');
            const responseAsJson = await response.json();

            await expect(response.status()).toBe(404);
            expect(responseAsJson.error).toContain('Kroger');
        })

Lesson 2 - POST

Create a POST request for the API endpoint /api/candy , with below requestBody to create a new candy entry details for a store. Verify the HttpStatusCode is 201. Refer to this video for guidance on writing the test

{
    "Name": "Snickers",
    "Company": "Wrigley Company",
    "Store": "Target",
    "Quantity" : 10
}

Practice Tests

  1. Create a assertion to validate Whether the Quantity is 10 or not from the output response
Click to reveal answer ```js expect(responseAsJson.Quantity).toEqual(10); ```
  1. Create a assertion to validate whether the value of Name is Snickers or not
Click to reveal answer ```js expect(responseAsJson.Name).toEqual("Snickers"); ```
  1. Create a test to validate 400 status when one of the input details is passed invalid/emtpy
Click to reveal answer
       test('Validate error status when Quantity is missing from request Body', async ( {request})=> {
            const requestBody = {
                "Name": "Snickers",
                "Company": "Wrigley Company",
                "Store": "Target"
            }

            const response = await request.post('http://localhost:3000/api/Candy', { data: requestBody});
            const responseAsJson = await response.json();

            await expect(response.status()).toBe(400);
            expect(responseAsJson.error).toContain('Invalid candy data. Please provide Name, Company, Quantity and Store.');
      })

Lesson 3 - PUT

Create a PUT request for the API endpoint /api/candy , with below requestBody to update the quantity of candy for a store. Verify the HttpStatusCode is 200 and new quantity value from output. Refer to this video for guidance on writing the test

{
    "Name": "Snickers",
    "Company": "Mars, Incorporated",
    "Quantity": 50,
    "Store": "Target"
}

Practice Tests

  1. Create a test case with the provided request body, and ensure that the HTTP status code is 404 when candy details are not found in the specified store. Additionally, validate the error message in the response output.
{
     "Name": "Hershey",
     "Company": "Hershey llc",
     "Quantity": 50,
     "Store": "Target"
}
Click to reveal answer ```js test('update quantity when candy details are not found in store', async ( {request})=> { const requestBody = { "Name": "Hershey", "Company": "Hershey llc", "Quantity": 50, "Store": "Target" }
const response = await request.put('http://localhost:3000/api/Candy', { data: requestBody});
const responseAsJson = await response.json();

await expect(response.status()).toBe(404);
expect(responseAsJson.error).toContain("Candy 'Hershey' not found.");

})

</details>


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors