Skip to content

Commit 4144ef8

Browse files
committed
refactor: move global locators to base page
1 parent 9967008 commit 4144ef8

File tree

2 files changed

+27
-38
lines changed

2 files changed

+27
-38
lines changed

tests/e2e/pages/BasePage.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1-
import { expect, Page } from "@playwright/test"
1+
import { expect, Locator, Page } from "@playwright/test"
22

33
import { breakpointAsNumber } from "@/lib/utils/screen"
44

55
/**
66
* Base page class with common functionality for all pages
77
*/
88
export class BasePage {
9-
constructor(protected page: Page) {}
9+
// Locators
10+
readonly searchButtonMobile: Locator
11+
readonly searchInputButton: Locator
12+
readonly searchInput: Locator
13+
readonly searchModal: Locator
14+
readonly searchResults: Locator
15+
readonly primaryNav: Locator
16+
readonly mobileMenuButton: Locator
17+
readonly mobileSidebar: Locator
18+
19+
constructor(protected page: Page) {
20+
this.searchButtonMobile = page.getByTestId("search-button-mobile")
21+
this.searchInputButton = page.getByTestId("search-input-button")
22+
this.searchInput = page.getByPlaceholder("Search")
23+
this.searchModal = page.getByTestId("search-modal")
24+
this.searchResults = page.getByRole("listbox")
25+
this.primaryNav = page.getByRole("navigation", { name: "Primary" })
26+
this.mobileMenuButton = this.primaryNav.getByRole("button", {
27+
name: /toggle menu button/i,
28+
})
29+
this.mobileSidebar = page.getByRole("dialog", { name: /ethereum.org/i })
30+
}
1031

1132
/**
1233
* Check if the current viewport is mobile
@@ -89,27 +110,15 @@ export class BasePage {
89110
* Open language picker in desktop view
90111
*/
91112
async openLanguagePickerDesktop(): Promise<void> {
92-
const nav = this.page.getByRole("navigation", { name: /primary/i })
93-
const langButton = nav.getByRole("button", { name: /languages/i })
94-
await expect(langButton).toBeVisible()
95-
await langButton.click()
113+
await this.primaryNav.getByRole("button", { name: /languages/i }).click()
96114
}
97115

98116
/**
99117
* Open language picker in mobile view
100118
*/
101119
async openLanguagePickerMobile(): Promise<void> {
102-
const nav = this.page.getByRole("navigation", { name: /primary/i })
103-
const menuButton = nav.getByRole("button", {
104-
name: /toggle menu button/i,
105-
})
106-
await expect(menuButton).toBeVisible()
107-
await menuButton.click()
108-
const sidebar = this.page.getByRole("dialog", { name: /ethereum.org/i })
109-
await expect(sidebar).toBeVisible()
110-
const langButton = sidebar.getByRole("button", { name: /languages/i })
111-
await expect(langButton).toBeVisible()
112-
await langButton.click()
120+
await this.mobileMenuButton.click()
121+
await this.mobileSidebar.getByRole("button", { name: /languages/i }).click()
113122
}
114123

115124
/**

tests/e2e/pages/HomePage.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, Locator, Page } from "@playwright/test"
1+
import { expect, Page } from "@playwright/test"
22

33
import { testData } from "../fixtures/testData"
44

@@ -10,28 +10,8 @@ import { BasePage } from "./BasePage"
1010
export class HomePage extends BasePage {
1111
private readonly url = "/"
1212

13-
// Locators
14-
private readonly searchButtonMobile: Locator
15-
private readonly searchInputButton: Locator
16-
private readonly searchInput: Locator
17-
private readonly searchModal: Locator
18-
private readonly searchResults: Locator
19-
private readonly primaryNav: Locator
20-
private readonly mobileMenuButton: Locator
21-
private readonly mobileSidebar: Locator
22-
2313
constructor(page: Page) {
2414
super(page)
25-
this.searchButtonMobile = page.getByTestId("search-button-mobile")
26-
this.searchInputButton = page.getByTestId("search-input-button")
27-
this.searchInput = page.getByPlaceholder("Search")
28-
this.searchModal = page.getByTestId("search-modal")
29-
this.searchResults = page.getByRole("listbox")
30-
this.primaryNav = page.getByRole("navigation", { name: "Primary" })
31-
this.mobileMenuButton = this.primaryNav.getByRole("button", {
32-
name: /toggle menu button/i,
33-
})
34-
this.mobileSidebar = page.getByRole("dialog", { name: /ethereum.org/i })
3515
}
3616

3717
/**

0 commit comments

Comments
 (0)