Skip to content

Commit f332689

Browse files
committed
test(e2e): integration tests for store-front and store-admin
1 parent be34eda commit f332689

8 files changed

Lines changed: 1415 additions & 57 deletions

tests/e2e/example.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test('has title', async ({ page }) => {
66
await page.goto('/');
77

88
// Expect a title "to contain" a substring.
9-
await expect(page).toHaveTitle(/store-admin/);
9+
await expect(page).toHaveTitle(/Contoso Pet Store Admin Portal/);
1010
});
1111

1212
test('has Products link', async ({ page }) => {

tests/e2e/products.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { test, expect } from '@playwright/test';
33
test.use({ baseURL: process.env.STORE_ADMIN_URL });
44

55
test('renders functionally', async ({ page }) => {
6-
await page.goto('/');
7-
8-
// Click the get started link.
9-
await page.getByRole('link', { name: 'Products' }).click();
10-
11-
// Expects page to have an Add Product button.
12-
await expect(page.getByRole('button', { name: 'Add Product' })).toBeVisible();
13-
await expect(page.getByText('Product ID')).toBeVisible();
14-
await expect(page.getByText('Product Name')).toBeVisible();
15-
await expect(page.getByText('Product Description')).toBeVisible();
16-
});
6+
await page.goto('/');
7+
8+
// Click the get started link.
9+
await page.getByRole('link', { name: 'Products' }).click();
10+
11+
// Expects page to have an Add Product button.
12+
await expect(page.getByRole('button', { name: 'Add Product' })).toBeVisible();
13+
await expect(page.getByText('Product ID')).toBeVisible();
14+
await expect(page.getByText('Product Name')).toBeVisible();
15+
await expect(page.getByText('Description')).toBeVisible();
16+
});

tests/e2e/store-admin.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
const STORE_ADMIN_URL = process.env.STORE_ADMIN_URL || 'http://';
4+
5+
test.describe('store-front tests', () => {
6+
test.skip(STORE_ADMIN_URL === 'http://', 'STORE_ADMIN_URL is not set');
7+
8+
test('has title', async ({ page }) => {
9+
await page.goto(STORE_ADMIN_URL);
10+
await expect(page).toHaveTitle(/Contoso Pet Store Admin Portal/);
11+
});
12+
13+
14+
test('can add product ', async ({ page }) => {
15+
await page.goto(STORE_ADMIN_URL);
16+
test.setTimeout(90000);
17+
18+
await page.getByRole('link', { name: 'Products' }).click();
19+
await page.getByRole('button', { name: 'Add Product' }).click();
20+
21+
await page.getByRole('textbox', { name: 'Name' }).fill('Super Snacks');
22+
await page.getByRole('spinbutton', { name: 'Price' }).fill('2.99');
23+
await page.getByRole('textbox', { name: 'Keywords' }).fill('dog, snack, treat');
24+
25+
const askOpenAI = page.getByRole('button', { name: 'Ask AI Assistant' });
26+
27+
if (await askOpenAI.isVisible()) {
28+
const aiApiCall = page.waitForResponse('/api/ai/generate/description');
29+
await askOpenAI.click();
30+
await aiApiCall;
31+
}
32+
else {
33+
await page.getByRole('textbox', { name: 'Description' }).fill('Something tasty for the pups');
34+
}
35+
36+
page.once('dialog', dialog => {
37+
console.log(`Dialog message: ${dialog.message()}`);
38+
expect(dialog.message()).toBe('Product saved successfully');
39+
dialog.dismiss().catch(() => { });
40+
});
41+
42+
await page.getByRole('button', { name: 'Save Product' }).click();
43+
});
44+
});

tests/e2e/store-front.spec.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
const STORE_FRONT_URL = process.env.STORE_FRONT_URL || 'http://';
4+
5+
test.describe('store-front tests', () => {
6+
test.skip(STORE_FRONT_URL === 'http://', 'STORE_FRONT_URL is not set');
7+
8+
test('has title', async ({ page }) => {
9+
await page.goto(STORE_FRONT_URL);
10+
await expect(page).toHaveTitle(/Contoso Pet Store/);
11+
});
12+
13+
test('has products and cart links', async ({ page }) => {
14+
await page.goto(STORE_FRONT_URL);
15+
await page.getByRole('link', { name: /Products/ }).click();
16+
await page.getByRole('link', { name: /Cart/ }).click();
17+
});
18+
19+
test('can navigate to product details and add one to cart', async ({ page }) => {
20+
await page.goto(STORE_FRONT_URL);
21+
22+
await page.locator('.product-list .product-card').first().click()
23+
await expect(page.url()).toContain('product')
24+
await expect(page.locator('.product-info h2')).toHaveText("Contoso Catnip's Friend");
25+
26+
const cartLink = page.getByRole('link', { name: /Cart \(\d+\)/ });
27+
const initialCartCount = parseInt((await cartLink.textContent() || '').match(/\d+/)?.[0] || '0');
28+
29+
await page.getByRole('button', { name: /Add to Cart/ }).click();
30+
await expect(cartLink).toHaveText(`Cart (${initialCartCount + 1})`);
31+
});
32+
33+
test('can add one to cart from home page', async ({ page }) => {
34+
await page.goto(STORE_FRONT_URL);
35+
36+
await expect(page.locator('.product-list')).toBeVisible();
37+
38+
const cartLink = page.getByRole('link', { name: /Cart \(\d+\)/ });
39+
const initialCartCount = parseInt((await cartLink.textContent() || '').match(/\d+/)?.[0] || '0');
40+
41+
const firstProduct = page.locator('.product-list .product-controls').first();
42+
await firstProduct.getByRole('button', { name: /Add to Cart/ }).click();
43+
44+
await expect(cartLink).toHaveText(`Cart (${initialCartCount + 1})`);
45+
});
46+
47+
test('can add multiple items to cart from home page', async ({ page }) => {
48+
await page.goto(STORE_FRONT_URL);
49+
50+
await expect(page.locator('.product-list')).toBeVisible();
51+
52+
const cartLink = page.getByRole('link', { name: /Cart \(\d+\)/ });
53+
const initialCartCount = parseInt((await cartLink.textContent() || '').match(/\d+/)?.[0] || '0');
54+
55+
const firstProduct = page.locator('.product-list .product-controls').first();
56+
await firstProduct.getByRole('button', { name: /Add to Cart/ }).click();
57+
58+
const lastProduct = page.locator('.product-list .product-controls').last();
59+
await lastProduct.getByRole('button', { name: /Add to Cart/ }).click();
60+
61+
await expect(cartLink).toHaveText(`Cart (${initialCartCount + 2})`);
62+
});
63+
64+
test('can place an order', async ({ page }) => {
65+
await page.goto(STORE_FRONT_URL);
66+
67+
await expect(page.locator('.product-list')).toBeVisible();
68+
69+
const firstProduct = page.locator('.product-list .product-controls').first();
70+
await firstProduct.getByRole('button', { name: /Add to Cart/ }).click();
71+
72+
const lastProduct = page.locator('.product-list .product-controls').last();
73+
await lastProduct.getByRole('button', { name: /Add to Cart/ }).click();
74+
75+
await page.getByRole('link', { name: /Cart \(\d+\)/ }).click();
76+
await page.getByRole('button', { name: 'Checkout' }).click();
77+
78+
page.on('dialog', async dialog => {
79+
expect(dialog.message()).toContain('Order submitted successfully');
80+
await dialog.accept();
81+
});
82+
});
83+
});

0 commit comments

Comments
 (0)