Skip to content

Commit 48904bb

Browse files
committed
Introduce fixtures & fix broken tests
1 parent 1a9313d commit 48904bb

File tree

5 files changed

+77
-69
lines changed

5 files changed

+77
-69
lines changed

fixtures/swiftie.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { test as base, Locator } from '@playwright/test';
2+
import type { TestInfo } from '@playwright/test';
3+
import ProductViewPage from '../pages/productView';
4+
import SearchResultsPage from '../pages/searchResults';
5+
import AddedToCartPage from '../pages/addedToCart';
6+
import ProductSpecsSection from '../pages/sections/productSpecs';
7+
const { screenshot } = require('../utils')()
8+
9+
type MyFixtures = {
10+
productViewPage: ProductViewPage;
11+
searchResultsPage: SearchResultsPage;
12+
productSpecsSection: ProductSpecsSection;
13+
addedToCartPage: AddedToCartPage;
14+
}
15+
16+
export const test = base.extend<MyFixtures>({
17+
productViewPage: async ({ page }, use, testInfo: TestInfo) => {
18+
const productViewPage: ProductViewPage = new ProductViewPage(page);
19+
await productViewPage.goto();
20+
await use(productViewPage);
21+
await screenshot(page, `${testInfo.title} - productView`)
22+
},
23+
searchResultsPage: async ({ page }, use, testInfo: TestInfo) => {
24+
const searchResultsPage: SearchResultsPage = new SearchResultsPage(page);
25+
await use(searchResultsPage);
26+
await screenshot(page, `${testInfo.title} - searchResults`)
27+
},
28+
productSpecsSection: async ({ page }, use, testInfo: TestInfo) => {
29+
const productViewPage: ProductViewPage = new ProductViewPage(page);
30+
await productViewPage.goto();
31+
const productSpecs: Locator = await productViewPage.productSpecs;
32+
const productSpecsSection: ProductSpecsSection = new ProductSpecsSection(productSpecs);
33+
await use(productSpecsSection);
34+
},
35+
addedToCartPage: async ({ page }, use, testInfo: TestInfo) => {
36+
const addedToCartPage: AddedToCartPage = new AddedToCartPage(page);
37+
await use(addedToCartPage);
38+
await screenshot(page, `${testInfo.title} - addedToCart`)
39+
},
40+
})
41+
42+
export { expect } from '@playwright/test';

pages/addedToCart.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import BasePage from './generics/page'
22
import { Page } from '@playwright/test'
33

4-
export default class AddedToCartConfirmationPage extends BasePage {
4+
export default class AddedToCartPage extends BasePage {
55
constructor(page: Page) {
66
super(page)
77
}

pages/productView.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import BasePage from './generics/page'
22
import { Page } from '@playwright/test'
33

44
export default class ProductViewPage extends BasePage {
5-
constructor(page: Page) {
5+
constructor(public readonly page: Page) {
66
super(page)
77
}
88

@@ -15,4 +15,14 @@ export default class ProductViewPage extends BasePage {
1515
productName = this.element('id', 'productTitle')
1616
productSpecs = this.element('id', 'detailBullets_feature_div')
1717
addToCartBtn = this.element('id', 'add-to-cart-button')
18+
19+
async goto() {
20+
await this.page.goto('https://www.amazon.com/1989-Taylors-Version-Taylor-Swift/dp/B0CFM76QSG?content-id=amzn1.sym.cb9c3293-7215-4d97-8c71-e1fed9b4d8f1&pd_rd_i=B0CFM76QSG&pd_rd_r=180b3bdc-c298-4f86-bb91-4ef0c80d1cb8&pd_rd_w=tHKD5&pd_rd_wg=kJ0QW&pf_rd_p=cb9c3293-7215-4d97-8c71-e1fed9b4d8f1&pf_rd_r=V7JWQ7ZF2DB34CY3VM1V&ref_=Oct_d_onr_d_5174_2')
21+
await this.page.waitForLoadState('domcontentloaded')
22+
}
23+
24+
async newSearch(searchPhrase: string) {
25+
await (await this.searchBox).fill(searchPhrase)
26+
await this.page.keyboard.press('Enter')
27+
}
1828
}

playwright.config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export default defineConfig({
99
['html', { open: 'never' }]
1010
],
1111
use: {
12-
testIdAttribute: 'id'
12+
testIdAttribute: 'id',
13+
// For debugging
14+
headless: false,
1315
},
1416

1517
projects: [

tests/swiftie.spec.ts

+20-66
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,38 @@
1-
import { test, expect, chromium, Browser, BrowserContext, Page } from '@playwright/test';
2-
import type { TestInfo } from '@playwright/test';
3-
const { screenshot } = require('../utils')()
4-
import SearchResultsPage from '../pages/searchResults';
5-
import ProductViewPage from '../pages/productView';
6-
import ProductSpecsSection from '../pages/sections/productSpecs';
7-
import AddedToCartConfirmationPage from '../pages/addedToCart';
1+
import { test, expect } from '../fixtures/swiftie';
82

93
test.describe('T Swift 1989 Album', async () => {
10-
let browser: Browser;
11-
let context: BrowserContext;
12-
let page: Page;
134
let foundName: string;
145
let foundASIN: string;
15-
let ProductView: ProductViewPage;
16-
let SearchResults: SearchResultsPage;
17-
let ProductSpecs: ProductSpecsSection;
18-
let AddedToCartConfirmation: AddedToCartConfirmationPage;
196

20-
async function newSearch(searchPhrase: string) {
21-
ProductView = new ProductViewPage(page)
22-
await (await ProductView.searchBox).fill(searchPhrase)
23-
await page.keyboard.press('Enter')
24-
}
25-
26-
test.beforeEach(async () => {
27-
// For Debugging Only: Do not push uncommented
28-
// browser = await chromium.launch({ headless: false });
29-
// For CI
30-
browser = await chromium.launch();
31-
context = await browser.newContext();
32-
page = await context.newPage();
33-
await page.goto('https://www.amazon.com/1989-Taylors-Version-Taylor-Swift/dp/B0CFM76QSG?content-id=amzn1.sym.cb9c3293-7215-4d97-8c71-e1fed9b4d8f1&pd_rd_i=B0CFM76QSG&pd_rd_r=180b3bdc-c298-4f86-bb91-4ef0c80d1cb8&pd_rd_w=tHKD5&pd_rd_wg=kJ0QW&pf_rd_p=cb9c3293-7215-4d97-8c71-e1fed9b4d8f1&pf_rd_r=V7JWQ7ZF2DB34CY3VM1V&ref_=Oct_d_onr_d_5174_2')
34-
await page.waitForLoadState('domcontentloaded')
35-
})
36-
37-
test.afterEach(async ({}, testInfo: TestInfo) => {
38-
await screenshot(page, testInfo.title)
39-
await page.close()
40-
await context.close()
41-
await browser.close();
42-
})
43-
44-
test('Can find T Swift album by product name and view details page', async () => {
45-
ProductView = new ProductViewPage(page)
46-
foundName = await (await ProductView.productName).textContent() ?? ''
47-
await newSearch(foundName)
7+
test('Can find T Swift album by product name and view details page', async ({ page, productViewPage, searchResultsPage }) => {
8+
foundName = await (await productViewPage.productName).textContent() ?? ''
9+
await productViewPage.newSearch(foundName)
4810
await page.waitForLoadState('domcontentloaded')
49-
SearchResults = new SearchResultsPage(page)
50-
await expect(await SearchResults.firstResult).toHaveText(SearchResults.firstResultM)
51-
await (await SearchResults.firstResultLink).click()
11+
await expect(await searchResultsPage.firstResult).toHaveText(searchResultsPage.firstResultM)
12+
await (await searchResultsPage.firstResultLink).click()
5213
await page.waitForLoadState('domcontentloaded')
53-
await expect(await ProductView.productName).toHaveText(ProductView.productNameM)
14+
await expect(await productViewPage.productName).toHaveText(productViewPage.productNameM)
5415
})
5516

56-
test('ASIN is correct', async () => {
57-
ProductView = new ProductViewPage(page)
58-
ProductSpecs = new ProductSpecsSection(await ProductView.productSpecs)
59-
foundASIN = await (await ProductSpecs.asin).textContent() ?? ''
60-
expect(foundASIN).toEqual(ProductSpecs.asinM)
17+
test('ASIN is correct', async ({ page, productSpecsSection }) => {
18+
foundASIN = await (await productSpecsSection.asin).textContent() ?? ''
19+
expect(foundASIN).toEqual(productSpecsSection.asinM)
6120
})
6221

63-
test('Can find T Swift album by ASIN', async () => {
64-
ProductView = new ProductViewPage(page)
65-
ProductSpecs = new ProductSpecsSection(await ProductView.productSpecs)
66-
await newSearch(foundASIN)
22+
test('Can find T Swift album by ASIN', async ({ page, productViewPage, searchResultsPage }) => {
23+
await productViewPage.newSearch(foundASIN)
6724
await page.waitForLoadState('domcontentloaded')
68-
SearchResults = new SearchResultsPage(page)
69-
await expect(await SearchResults.firstResult).toHaveText(SearchResults.firstResultM)
70-
await (await SearchResults.firstResultLink).click()
25+
await expect(await searchResultsPage.firstResult).toHaveText(searchResultsPage.firstResultM)
26+
await (await searchResultsPage.firstResultLink).click()
7127
await page.waitForLoadState('domcontentloaded')
72-
await expect(await ProductView.productName).toHaveText(ProductView.productNameM)
28+
await expect(await productViewPage.productName).toHaveText(productViewPage.productNameM)
7329
})
7430

75-
test('T Swift album can be added to cart', async () => {
76-
ProductView = new ProductViewPage(page)
77-
await expect(await ProductView.cartCount).toHaveText('0')
78-
await (await ProductView.addToCartBtn).click()
31+
test('T Swift album can be added to cart', async ({ page, productViewPage, addedToCartPage }) => {
32+
await expect(await productViewPage.cartCount).toHaveText('0')
33+
await (await productViewPage.addToCartBtn).click()
7934
await page.waitForLoadState('domcontentloaded')
80-
AddedToCartConfirmation = new AddedToCartConfirmationPage(page)
81-
await expect(await AddedToCartConfirmation.addedToCart).toContainText(AddedToCartConfirmation.addedToCartM)
82-
await expect(await ProductView.cartCount).toHaveText('1')
35+
await expect(await addedToCartPage.addedToCart).toContainText(addedToCartPage.addedToCartM)
36+
await expect(await productViewPage.cartCount).toHaveText('1')
8337
})
8438
})

0 commit comments

Comments
 (0)