forked from microsoft/contosotraders-cloudtesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.spec.ts
47 lines (41 loc) · 2.13 KB
/
cart.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { test, expect, } from '@playwright/test';
// SETUP: Get first 5 product ID's and add them all to cart
test.beforeEach(async ({ page, request }) => {
const response = await request.get(process.env.REACT_APP_APIURL + '/products')
await expect(response).toBeOK();
const productsFromResponse = (await response.json()).products.slice(0, 5);
for (const product of productsFromResponse) {
await page.goto(`/product/detail/${product.id}`);
await page.getByRole('button', { name: 'Add To Bag' }).click();
}
await page.getByRole('button', { name: 'cart' }).click();
});
test.describe('Shopping Cart', () => {
test('should be able to view cart', async ({ page }) => {
await expect(page.getByRole('heading', { name: 'My Cart' })).toBeVisible();
await expect(page.getByText('Order Summary')).toBeVisible();
await expect(page.getByText('Product Name')).toBeVisible();
await expect(page.getByText('Price', { exact: true })).toBeVisible();
await expect(page.getByText('Qty', { exact: true })).toBeVisible();
await expect(page.getByText('Subtotal', { exact: true })).toBeVisible();
});
test('should be able to increase and decrease test quantity', async ({ page }) => {
const subtotal = async () => Number((await page.getByTestId('subtotal').innerText()).replace('$', ''));
const clickButton = async (name: string) => await page.getByRole('button', { name }).first().click();
const subtotalBefore = await subtotal();
await clickButton('+');
await expect(async () => {
expect(await subtotal()).toBeGreaterThan(subtotalBefore);
}).toPass();
const subtotalAfter = await subtotal();
await clickButton('-');
await expect(async () => {
expect(await subtotal()).toBeLessThan(subtotalAfter);
}).toPass();
});
test('should be able to remove items from cart', async ({ page }) => {
while (!(await page.getByRole('heading', { name: 'Your Cart is empty' }).isVisible())) {
await page.getByRole('link', { name: 'Remove' }).first().click();
}
});
});