-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbrowser-check.ts
More file actions
221 lines (201 loc) · 7.12 KB
/
browser-check.ts
File metadata and controls
221 lines (201 loc) · 7.12 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
218
219
220
221
import fs from 'node:fs/promises'
import path from 'node:path'
import { CheckProps, RuntimeCheck, RuntimeCheckProps } from './check'
import { Session, SharedFileRef } from './project'
import { pathToPosix } from '../services/util'
import { Content, Entrypoint, isContent, isEntrypoint } from './construct'
import { detectSnapshots } from '../services/snapshot-service'
import { PlaywrightConfig } from './playwright-config'
import { Diagnostics } from './diagnostics'
import { InvalidPropertyValueDiagnostic } from './construct-diagnostics'
import { BrowserCheckBundle } from './browser-check-bundle'
import { ConfigDefaultsGetter, makeConfigDefaultsGetter } from './check-config'
export interface BrowserCheckProps extends RuntimeCheckProps {
/**
* A valid piece of Node.js javascript code describing a browser interaction
* with the Puppeteer or Playwright frameworks.
*/
code: Content | Entrypoint
/**
* A valid fully qualified domain name (FQDN) to check for SSL certificate
* expiration. For example, 'app.checklyhq.com'.
*/
sslCheckDomain?: string
/**
* A valid playwright config object, same format and keys as you would use on
* playwright.config.ts
*/
playwrightConfig?: PlaywrightConfig
}
/**
* Creates a Browser Check to monitor web applications using Playwright.
*
* Browser checks allow you to monitor complex user interactions, page performance,
* and visual regressions. They run real browser scripts using Playwright to simulate
* user behavior and validate web application functionality.
*
* @example
* ```typescript
* // Basic browser check with script file
* new BrowserCheck('login-flow', {
* name: 'User Login Flow',
* frequency: Frequency.EVERY_10M,
* locations: ['us-east-1', 'eu-west-1'],
* code: {
* entrypoint: path.join(__dirname, 'login.spec.js')
* }
* })
*
* // Browser check with inline code
* new BrowserCheck('homepage', {
* name: 'Homepage Check',
* frequency: Frequency.EVERY_5M,
* code: {
* content: `
* const { test, expect } = require('@playwright/test')
*
* test('homepage loads correctly', async ({ page }) => {
* await page.goto('https://example.com')
* await expect(page.locator('h1')).toContainText('Welcome')
* await expect(page).toHaveTitle(/Example/)
* })
* `
* },
* playwrightConfig: {
* use: {
* viewport: { width: 1280, height: 720 }
* }
* }
* })
* ```
*
* @see {@link https://www.checklyhq.com/docs/constructs/browser-check/ | BrowserCheck API Reference}
* @see {@link https://www.checklyhq.com/docs/detect/synthetic-monitoring/browser-checks/overview/
* Browser Checks Documentation}
* @see {@link https://playwright.dev/ | Playwright Documentation}
*/
export class BrowserCheck extends RuntimeCheck {
readonly code: Content | Entrypoint
readonly sslCheckDomain?: string
readonly playwrightConfig?: PlaywrightConfig
/**
* Constructs the Browser Check instance
*
* @param logicalId unique project-scoped resource name identification
* @param props check configuration properties
* {@link https://www.checklyhq.com/docs/constructs/browser-check/ Read more in the docs}
*/
constructor (logicalId: string, props: BrowserCheckProps) {
super(logicalId, props)
const config = this.applyConfigDefaults(props)
this.code = config.code
this.sslCheckDomain = config.sslCheckDomain
this.playwrightConfig = config.playwrightConfig
Session.registerConstruct(this)
this.addSubscriptions()
this.addPrivateLocationCheckAssignments()
}
describe (): string {
return `BrowserCheck:${this.logicalId}`
}
protected configDefaultsGetter (props: CheckProps): ConfigDefaultsGetter {
return makeConfigDefaultsGetter(
props.group?.getBrowserCheckDefaults(),
Session.browserCheckDefaults,
props.group?.getCheckDefaults(),
Session.checkDefaults,
)
}
protected applyConfigDefaults<T extends RuntimeCheckProps & Pick<BrowserCheckProps, 'playwrightConfig'>> (props: T): T {
const config = super.applyConfigDefaults(props)
const defaults = this.configDefaultsGetter(props)
config.playwrightConfig ??= defaults('playwrightConfig')
return config
}
async validate (diagnostics: Diagnostics): Promise<void> {
await super.validate(diagnostics)
if (!isEntrypoint(this.code) && !isContent(this.code)) {
diagnostics.add(new InvalidPropertyValueDiagnostic(
'code',
new Error(`Either "entrypoint" or "content" is required.`),
))
} else if (isEntrypoint(this.code) && isContent(this.code)) {
diagnostics.add(new InvalidPropertyValueDiagnostic(
'code',
new Error(`Provide exactly one of "entrypoint" or "content", but not both.`),
))
} else if (isEntrypoint(this.code)) {
const entrypoint = this.resolveContentFilePath(this.code.entrypoint)
try {
const stats = await fs.stat(entrypoint)
if (stats.size === 0) {
diagnostics.add(new InvalidPropertyValueDiagnostic(
'code',
new Error(`The entrypoint file "${entrypoint}" must not be empty.`),
))
}
} catch (err: any) {
diagnostics.add(new InvalidPropertyValueDiagnostic(
'code',
new Error(`Unable to access entrypoint file "${entrypoint}": ${err.message}`, { cause: err }),
))
}
}
}
static async bundle (entry: string, runtimeId?: string) {
const runtime = Session.getRuntime(runtimeId)
if (!runtime) {
throw new Error(`${runtimeId} is not supported`)
}
const parser = Session.getParser(runtime)
const parsed = await parser.parse(entry)
// Maybe we can get the parsed deps with the content immediately
const deps: SharedFileRef[] = []
for (const { filePath, content } of parsed.dependencies) {
deps.push(Session.registerSharedFile({
path: pathToPosix(path.relative(Session.basePath!, filePath)),
content,
}))
}
return {
script: parsed.entrypoint.content,
scriptPath: Session.relativePosixPath(parsed.entrypoint.filePath),
dependencies: deps,
snapshots: detectSnapshots(Session.basePath!, parsed.entrypoint.filePath),
}
}
getSourceFile () {
return this.__checkFilePath
}
async bundle (): Promise<BrowserCheckBundle> {
return new BrowserCheckBundle(this, await (async () => {
if (isEntrypoint(this.code)) {
const bundle = await BrowserCheck.bundle(
this.resolveContentFilePath(this.code.entrypoint),
this.runtimeId,
)
if (!bundle.script) {
throw new Error(`The "code" property points to an empty file.`)
}
return {
script: bundle.script,
scriptPath: bundle.scriptPath,
dependencies: bundle.dependencies,
rawSnapshots: bundle.snapshots,
}
}
const script = this.code.content
return {
script,
}
})())
}
synthesize () {
return {
...super.synthesize(),
checkType: 'BROWSER',
sslCheckDomain: this.sslCheckDomain || null, // empty string is converted to null
playwrightConfig: this.playwrightConfig,
}
}
}