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
- Clone the repository
- Navigate to api-server directory from terminal
- Run
npm init -yto install necessary packages - Start the Express server by running the following command
node app.js
- Navigate to candy-api-tests directory
- Run
npm init -yto install necessary packages - Run
npx playwright testto run the necessary tests
- Compose a GET request for the API endpoint
api/Candy?storeName=Targetto 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
- 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');
})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
}- Create a assertion to validate Whether the Quantity is
10or not from the output response
Click to reveal answer
```js expect(responseAsJson.Quantity).toEqual(10); ```- Create a assertion to validate whether the value of Name is
Snickersor not
Click to reveal answer
```js expect(responseAsJson.Name).toEqual("Snickers"); ```- 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.');
})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"
}- 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>