-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.ts
More file actions
217 lines (175 loc) · 6.48 KB
/
base.ts
File metadata and controls
217 lines (175 loc) · 6.48 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { AssertionError } from 'node:assert/strict'
import { type Browser, type BrowserContext, type Page as PlayWrightPage } from '@playwright/test'
import { BASE_URL, BROWSER } from './config.ts'
import { Anchor } from './support/anchor.ts'
import { Button } from './support/button.ts'
import { waitForStableState } from './support/locator.ts'
import { type ViewportSizeName, transposeViewport, viewportNameMap } from './support/viewport.ts'
import { type WebsiteLanguageId, parseSupportedLanguageString } from '../../common/languages.ts'
import { UrlPath } from '../../common/support/url_path.ts'
import { type IFinalizeable } from '../../common/types/finalizable.ts'
import { type ColorScheme } from '../../common/types/page.ts'
interface IBasePageOptions {
javaScriptEnabled?: boolean
locale?: string
httpHeaders?: Record<string, string>
colorScheme?: ColorScheme
reducedMotion?: 'reduce' | 'no-preference'
}
const LOADING_INDICATOR_WAIT_TIMEOUT = 1000
export class BasePage implements IFinalizeable {
static async initialize<PageT extends BasePage>(options?: IBasePageOptions): Promise<PageT> {
const browser = await BROWSER.launch()
const context = await browser.newContext({
baseURL: BASE_URL,
javaScriptEnabled: options?.javaScriptEnabled,
locale: options?.locale,
extraHTTPHeaders: { prefer: 'data-source=mocked', ...options?.httpHeaders },
colorScheme: options?.colorScheme,
reducedMotion: options?.reducedMotion ?? 'reduce',
})
const page = await context.newPage()
return new this(options ?? {}, browser, context, page) as PageT
}
readonly options: IBasePageOptions
readonly #browser: Browser
readonly #context: BrowserContext
protected _pwPage: PlayWrightPage
protected constructor(
options: IBasePageOptions,
browser: Browser,
context: BrowserContext,
pw_page: PlayWrightPage,
) {
this.options = options
this.#browser = browser
this.#context = context
this._pwPage = pw_page
}
async isContentDynamic() {
return this.options.javaScriptEnabled
}
async goto(path: string) {
await this._pwPage.goto(path)
await this._pwPage.waitForLoadState('load')
if (await this.isContentDynamic()) {
await this._pwPage.waitForSelector('.dynamic-content')
}
}
async captureScreenshot(path: string) {
await this._pwPage.screenshot({ path, fullPage: true })
}
async press(key: string) {
await this._pwPage.keyboard.press(key)
}
async getTitle() {
// This turns out to be less flaky than _pwPage.title()
const locator = this._pwPage.locator('title')
await waitForStableState(locator)
return locator.innerText()
}
getUrlPath() {
return UrlPath.parse(this._pwPage.url())
}
async scaleViewport(name: ViewportSizeName, { vertical }: { vertical?: boolean } = {}) {
const size = vertical ? transposeViewport(viewportNameMap[name]) : viewportNameMap[name]
await this._pwPage.setViewportSize(size)
}
getLoadingIndicatorLocator() {
return this._pwPage.locator('.loading-indicator')
}
isLoading() {
return this.getLoadingIndicatorLocator().isVisible()
}
async waitWhileLoading() {
try {
await this.getLoadingIndicatorLocator().waitFor({ state: 'visible', timeout: LOADING_INDICATOR_WAIT_TIMEOUT })
} catch (err) {
// Ignore timeout error
}
await this.getLoadingIndicatorLocator().waitFor({ state: 'hidden' })
}
getMainMenuLocator() {
return this._pwPage.locator('.main-menu').first()
}
getBodyLocator() {
return this._pwPage.locator('body').first()
}
getPageContainerLocator() {
return this._pwPage.locator('.page-scroll-container').first()
}
getMainLocator() {
return this._pwPage.locator('main').first()
}
async getLanguage(): Promise<WebsiteLanguageId | undefined> {
return parseSupportedLanguageString(
await this._pwPage.locator('html').first().getAttribute('lang') ?? undefined,
)
}
async getColorScheme(): Promise<ColorScheme> {
const locator = this.getBodyLocator()
const background: string = await locator.evaluate(function (element) {
return window.getComputedStyle(element).getPropertyValue('--body-color-background')
})
const match = background.match(/#(?<r>[0-9a-f])(?<g>[0-9a-f])(?<b>[0-9a-f])/)
if (match === null) {
throw new AssertionError({
message: `Unrecognized color ${background}`,
})
}
const { r, g, b } = match.groups!
const average = (parseInt(r + r, 16) + parseInt(g + g, 16) + parseInt(b + b, 16)) / 3
return average < 128 ? 'dark' : 'light'
}
async isLayoutCollapsed(): Promise<boolean> {
return await this._pwPage
.evaluate(
_page => window.getComputedStyle(document.body).getPropertyValue('--layout-collapsed') === 'true',
)
}
getMainMenuPanel() {
return this._pwPage.locator('.compact-navbar-dialog')
}
getMainMenuOpenButton() {
return new Button(this, this._pwPage.locator('.compact-navbar-button-menu').first())
}
getMainMenuCloseButton() {
return new Button(this, this.getMainMenuPanel().getByRole('button', { name: 'Close menu' }))
}
getMainMenuLanguageButtons() {
return {
en: new Button(this, this.getMainMenuLocator().getByRole('button', { name: 'English', exact: true }).first()),
ru: new Button(this, this.getMainMenuLocator().getByRole('button', { name: 'Русский', exact: true }).first()),
}
}
getMainMenuColorLocator() {
return this.getMainMenuLocator().locator('.main-menu-color-toggle').first()
}
async getMainMenuAnchors() {
const locators = await this.getMainMenuLocator().getByRole('link').all()
return locators.map(l => new Anchor(this, l))
}
async parseError() {
const errorTitleLocator = this._pwPage.locator('.error-page-title')
const errorSubtitleLocator = this._pwPage.locator('.error-page-subtitle')
const errorDetailsLocator = this._pwPage.locator('.error-page-details')
if (!(await errorTitleLocator.isVisible())) {
throw new AssertionError({
message: 'Expected an error, but no error information found',
})
}
const hasCause = await errorDetailsLocator.isVisible()
return {
title: await errorTitleLocator.textContent(),
subtitle: await errorSubtitleLocator.textContent(),
details: hasCause ? await errorDetailsLocator.textContent() : undefined,
}
}
async reset() {
await this._pwPage.close()
this._pwPage = await this.#context.newPage()
}
async finalize() {
await this.#browser.close()
}
}