From d91a0970fca061d41d9fe61dd01e59de1a146933 Mon Sep 17 00:00:00 2001 From: shray sharma Date: Wed, 4 Sep 2024 10:54:37 -0400 Subject: [PATCH 1/7] Add playwright tests --- tests/example.spec.js | 19 +++++++++ tests/home.spec.js | 89 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/example.spec.js create mode 100644 tests/home.spec.js diff --git a/tests/example.spec.js b/tests/example.spec.js new file mode 100644 index 00000000..40eddb86 --- /dev/null +++ b/tests/example.spec.js @@ -0,0 +1,19 @@ +// @ts-check +const { test, expect } = require('@playwright/test'); + +test('has title', async ({ page }) => { + await page.goto('https://playwright.dev/'); + + // Expect a title "to contain" a substring. + await expect(page).toHaveTitle(/Playwright/); +}); + +test('get started link', async ({ page }) => { + await page.goto('https://playwright.dev/'); + + // Click the get started link. + await page.getByRole('link', { name: 'Get started' }).click(); + + // Expects page to have a heading with the name of Installation. + await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible(); +}); diff --git a/tests/home.spec.js b/tests/home.spec.js new file mode 100644 index 00000000..fd93ce62 --- /dev/null +++ b/tests/home.spec.js @@ -0,0 +1,89 @@ +const { test, expect } = require('@playwright/test'); + +test('has title', async ({ page }) => { + await page.goto('https://playwright.dev/'); + + // Expect a title "to contain" a substring. + await expect(page).toHaveTitle(/Playwright/); + }); + + test('content container element exists', async ({ page }) => { + await page.goto('localhost:8081'); + + // Check if the content container element exists + const contentContainer = page.locator('#contentContainer'); + await expect(contentContainer).toBeTruthy(); + + // Check if the slot element exists inside the content container + const slot = contentContainer.locator('#slot'); + await expect(slot).toBeTruthy(); + }); + + test('click on link redirects to mens outerwear page', async ({ page }) => { + await page.goto('localhost:8081'); + + // Click on the link + const link = page.locator('a.image-link[href="/list/mens_outerwear"]'); + await link.click(); + + // Check if the page redirects to the expected URL + await expect(page.url()).toContain('/list/mens_outerwear'); + }); + + test('click on shop now button redirects to mens outerwear page', async ({ page }) => { + await page.goto('localhost:8081'); + + // Click on the shop now button + const button = page.locator('shop-button a[href="/list/mens_outerwear"]'); + await button.click(); + + // Check if the page redirects to the expected URL + await expect(page.url()).toContain('/list/mens_outerwear'); + }); + + test('ladies outerwear item is displayed correctly', async ({ page }) => { + await page.goto('localhost:8081'); + + // Get the item element + const item = page.locator('.item'); + + // Verify the image link + const imageLink = item.getByRole('link', { name: 'Ladies Outerwear', exact: true }); + await expect(imageLink).toHaveAttribute('href', '/list/ladies_outerwear'); + + // Verify the image source + const image = item.getByRole('link', { name: 'Ladies Outerwear', exact: true }); + const imageUrl = await image.getAttribute('style'); + + // Verify the heading text + const heading = item.getByRole('heading', { name: 'Ladies Outerwear' }); + await expect(heading).toHaveText('Ladies Outerwear'); + + // Verify the shop button link + const shopButton = item.getByLabel('Ladies Outerwear Shop Now'); + await expect(shopButton).toHaveAttribute('href', '/list/ladies_outerwear'); + await expect(shopButton).toHaveText('Shop Now'); + }); + + test("men's t-shirts item is displayed correctly", async ({ page }) => { + await page.goto('localhost:8081'); + + // Get the item element + const item = page.locator('.item'); + + // Verify the image link + const imageLink = item.getByRole('link', { name: 'Ments T-Shirts', exact: true }); + + // Verify the image source + const image = item.getByRole('link', { name: 'Men\'s T-Shirts', exact: true }); + const imageUrl = await image.getAttribute('style'); + + // Verify the heading text + const heading = item.getByRole('heading', { name: 'Men\'s T-Shirts' }); + await expect(heading).toHaveText("Men's T-Shirts"); + + // Verify the shop button link + const shopButton = item.getByLabel('Men\'s T-Shirts Shop Now'); + await expect(shopButton).toHaveAttribute('href', '/list/mens_tshirts'); + await expect(shopButton).toHaveText('Shop Now'); + }); \ No newline at end of file From e798a5ac571b2aa811978d567a639abb1d7edbaf Mon Sep 17 00:00:00 2001 From: shray sharma Date: Thu, 5 Sep 2024 08:16:11 -0400 Subject: [PATCH 2/7] Home page tests --- tests/home.spec.js | 96 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/tests/home.spec.js b/tests/home.spec.js index fd93ce62..cf92d539 100644 --- a/tests/home.spec.js +++ b/tests/home.spec.js @@ -86,4 +86,98 @@ test('has title', async ({ page }) => { const shopButton = item.getByLabel('Men\'s T-Shirts Shop Now'); await expect(shopButton).toHaveAttribute('href', '/list/mens_tshirts'); await expect(shopButton).toHaveText('Shop Now'); - }); \ No newline at end of file + }); + + +test.describe('Check all links exist and verify redirection', () => { + + test.beforeEach(async ({ page }) => { + await page.goto('localhost:8081'); + }); + + test('Check Men\'s Outerwear link and redirection', async ({ page }) => { + const link = page.getByRole('link', { name: 'Men\'s Outerwear', exact: true }); + + // Check the link exists and has correct text + await expect(link).toBeVisible(); + + // Click and verify redirection + await link.click(); + await page.waitForLoadState('networkidle'); + await expect(page).toHaveURL('http://localhost:8081/list/mens_outerwear'); + + // Go back to the original page + await page.goBack(); + }); + + test('Check Ladies Outerwear link and redirection', async ({ page }) => { + const link = page.getByRole('link', { name: 'Ladies Outerwear', exact: true }); + + // Check the link exists and has correct text + await expect(link).toBeVisible(); + // await expect(link).toHaveText("Ladies Outerwear"); + + // Click and verify redirection + await link.click(); + await page.waitForLoadState('networkidle'); + await expect(page).toHaveURL('http://localhost:8081/list/ladies_outerwear'); + + // Go back to the original page + await page.goBack(); + }); + + test('Check Men\'s T-Shirts link and redirection', async ({ page }) => { + const link = page.getByRole('link', { name: 'Men\'s T-Shirts', exact: true }); + + // Check the link exists and has correct text + await expect(link).toBeVisible(); + // await expect(link).toHaveText("Men's T-Shirts"); + + // Click and verify redirection + await link.click(); + await page.waitForLoadState('networkidle'); + await expect(page).toHaveURL('http://localhost:8081/list/mens_tshirts'); + + // Go back to the original page + await page.goBack(); + }); + + test('Check Ladies T-Shirts link and redirection', async ({ page }) => { + const link = page.getByRole('link', { name: 'Ladies T-Shirts', exact: true }); + + // Check the link exists and has correct text + await expect(link).toBeVisible(); + + // Click and verify redirection + await link.click(); + await page.waitForLoadState('networkidle'); + await expect(page).toHaveURL('http://localhost:8081/list/ladies_tshirts'); + + // Go back to the original page + await page.goBack(); + }); + +}); + + +test.describe('Check cart link and redirection', () => { + + test.beforeEach(async ({ page }) => { + await page.goto('localhost:8081'); + }); + + test('Check cart link and redirect to cart page', async ({ page }) => { + const cartLink = page.locator('a[href="/cart"]'); + + // Check that the cart link is visible + await expect(cartLink).toBeVisible(); + + await cartLink.click(); + + // Wait for the navigation to complete + await page.waitForLoadState('networkidle'); + + // Assert that the URL contains '/cart' + await expect(page).toHaveURL('http://localhost:8081/cart'); + }); +}); \ No newline at end of file From f818edca62d5dbc061f0484d8adf4a11920f08d7 Mon Sep 17 00:00:00 2001 From: shray sharma Date: Fri, 6 Sep 2024 10:30:57 -0400 Subject: [PATCH 3/7] Product page tests --- tests/product.spec.js | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/product.spec.js diff --git a/tests/product.spec.js b/tests/product.spec.js new file mode 100644 index 00000000..da49b80c --- /dev/null +++ b/tests/product.spec.js @@ -0,0 +1,77 @@ +const { test, expect } = require('@playwright/test'); + +test('header element exists', async ({ page }) => { + await page.goto('http://localhost:8081/list/mens_outerwear'); + + const header = await page.$('header'); + expect(header).not.toBeNull(); + + const h1 = await page.$('header > h1'); + expect(h1).not.toBeNull(); + + // text content of the h1 element is correct + const h1Text = await h1.textContent(); + expect(h1Text).toBe('Men\'s Outerwear'); + + //the span element exists within the header + const span = await page.$('header > span'); + expect(span).not.toBeNull(); + + //text content of the span element is correct + const spanText = await span.textContent(); + expect(spanText).toBe('(16 items)'); + }); + + + test('first 5 elements exist', async ({ page }) => { + await page.goto('http://localhost:8081/list/mens_outerwear'); + + //the ul element exists + const ul = await page.$('ul.grid'); + expect(ul).not.toBeNull(); + + //the first 5 li elements exist + const liElements = await page.$$('ul.grid > li'); + expect(liElements.length).toBeGreaterThan(5); + + // Check if the first 5 li elements contain a and shop-list-item elements + expect(await liElements[0].$$('a > shop-list-item')).not.toBeNull(); + expect(await liElements[1].$$('a > shop-list-item')).not.toBeNull(); + expect(await liElements[2].$$('a > shop-list-item')).not.toBeNull(); + expect(await liElements[3].$$('a > shop-list-item')).not.toBeNull(); + expect(await liElements[4].$$('a > shop-list-item')).not.toBeNull(); + }); + + +test('image exists', async ({ page }) => { + await page.goto('http://localhost:8081/list/mens_outerwear'); + + const image = page.getByRole('img', { name: 'Men\'s Outerwear' }); + + // Assert that the image is visible on the page + await expect(image).toBeVisible(); + }); + + test('check header tabs exist', async ({ page }) => { + await page.goto('http://localhost:8081/list/mens_outerwear'); + + // #tabContainer exists + const tabContainer = page.locator('#tabContainer'); + await expect(tabContainer).toBeVisible(); + + // "Men's Outerwear" tab exists + const mensOuterwearTab = tabContainer.locator('a[href="/list/mens_outerwear"]'); + await expect(mensOuterwearTab).toBeVisible(); + + // "Ladies Outerwear" tab exists + const ladiesOuterwearTab = tabContainer.locator('a[href="/list/ladies_outerwear"]'); + await expect(ladiesOuterwearTab).toBeVisible(); + + // "Men's T-Shirts" tab exists + const mensTshirtsTab = tabContainer.locator('a[href="/list/mens_tshirts"]'); + await expect(mensTshirtsTab).toBeVisible(); + + // "Ladies T-Shirts" tab exists + const ladiesTshirtsTab = tabContainer.locator('a[href="/list/ladies_tshirts"]'); + await expect(ladiesTshirtsTab).toBeVisible(); + }); \ No newline at end of file From c48dc2575054bafbcfa1f5e2215f3bd996c51cc0 Mon Sep 17 00:00:00 2001 From: shray sharma Date: Sat, 7 Sep 2024 11:04:05 -0400 Subject: [PATCH 4/7] Product page tests --- tests/product.spec.js | 55 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/tests/product.spec.js b/tests/product.spec.js index da49b80c..c23e77be 100644 --- a/tests/product.spec.js +++ b/tests/product.spec.js @@ -74,4 +74,57 @@ test('image exists', async ({ page }) => { // "Ladies T-Shirts" tab exists const ladiesTshirtsTab = tabContainer.locator('a[href="/list/ladies_tshirts"]'); await expect(ladiesTshirtsTab).toBeVisible(); - }); \ No newline at end of file + }); + + test('should check if the Ladies Outerwear image exists', async ({ page }) => { + await page.goto('http://localhost:8081/list/ladies_outerwear'); + + // Check if the image exists + const image = await page.getByRole('img', { name: 'Ladies Outerwear' }); + + //image is visible + await expect(image).toBeVisible(); + + await expect(image).toHaveAttribute('src', 'images/ladies_outerwear.jpg'); + }); + + +test('should check if the header for Ladies Outerwear exists', async ({ page }) => { + await page.goto('http://localhost:8081/list/ladies_outerwear'); + + // text "Ladies Outerwear" + const headerTitle = await page.locator('header h1'); + await expect(headerTitle).toHaveText('Ladies Outerwear'); + + // "(6 items)" + const headerItems = await page.locator('header span'); + await expect(headerItems).toHaveText('(6 items)'); +}); + + +test('should check if specific list items in the grid exist', async ({ page }) => { + await page.goto('http://localhost:8081/list/ladies_outerwear'); + + // Check if the