Skip to content

Commit 184c1c3

Browse files
♻️ improve browser extension e2e tests (#3823)
Co-authored-by: beltran.bulbarella <[email protected]>
1 parent fada5ed commit 184c1c3

File tree

20 files changed

+353
-211
lines changed

20 files changed

+353
-211
lines changed

scripts/build/build-test-apps.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@ import { printLog, runMain } from '../lib/executionUtils.ts'
55
import { command } from '../lib/command.ts'
66
import { modifyFile } from '../lib/filesUtils.ts'
77

8-
interface ExtensionConfig {
9-
name: string
10-
initParameter: string
11-
}
12-
13-
const EXTRA_EXTENSIONS: ExtensionConfig[] = [
14-
{ name: 'allowed-tracking-origin', initParameter: 'allowedTrackingOrigins: [/^chrome-extension:\\/\\//],' },
15-
{ name: 'invalid-tracking-origin', initParameter: "allowedTrackingOrigins: ['https://app.example.com']," },
8+
const OTHER_EXTENSIONS: Array<{ name: string; options?: { runAt?: string } }> = [
9+
{ name: 'cdn' },
10+
{ name: 'appendChild', options: { runAt: 'document_start' } },
1611
]
1712

1813
runMain(async () => {
@@ -66,17 +61,20 @@ async function buildExtensions(): Promise<void> {
6661

6762
buildApp(baseExtDir)
6863

69-
for (const { name, initParameter } of EXTRA_EXTENSIONS) {
70-
const targetDir = path.join('test/apps', name)
64+
for (const { name, options } of OTHER_EXTENSIONS) {
65+
const targetDir = path.join('test/apps', `${name}-extension`)
7166

7267
fs.rmSync(targetDir, { recursive: true, force: true })
7368
fs.cpSync(baseExtDir, targetDir, { recursive: true })
7469

75-
const contentScriptPath = path.join(targetDir, 'src/contentScript.ts')
76-
await modifyFile(contentScriptPath, (content: string) =>
77-
content.replace(/\/\* EXTENSION_INIT_PARAMETER \*\//g, initParameter)
78-
)
70+
const manifestPath = path.join(targetDir, 'manifest.json')
71+
await modifyFile(manifestPath, (originalContent: string) => {
72+
let content = originalContent.replace('dist/base.js', `dist/${name}.js`)
7973

80-
buildApp(targetDir)
74+
if (options?.runAt) {
75+
content = content.replace('document_end', options.runAt)
76+
}
77+
return content
78+
})
8179
}
8280
}

test/apps/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
allowed-tracking-origin/
22
invalid-tracking-origin/
33
react-router-v7-app/
4+
cdn-extension/
5+
appendChild-extension/

test/apps/base-extension/manifest.json

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@
55
"description": "Injects Datadog RUM into every page to inspect data being sent.",
66
"permissions": ["scripting", "activeTab"],
77
"host_permissions": ["<all_urls>"],
8-
"background": {
9-
"service_worker": "dist/background.js",
10-
"type": "module"
11-
},
128
"content_scripts": [
139
{
1410
"matches": ["<all_urls>"],
15-
"js": ["dist/content-script.js"],
16-
"run_at": "document_start"
11+
"js": ["dist/base.js"],
12+
"run_at": "document_end",
13+
"world": "MAIN"
1714
}
18-
],
19-
"action": {
20-
"default_title": "Datadog RUM Testing Extension",
21-
"default_popup": "src/popup.html"
22-
}
15+
]
2316
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const originalAppendChild = Node.prototype.appendChild // eslint-disable-line @typescript-eslint/unbound-method
2+
3+
Node.prototype.appendChild = function <T extends Node>(node: T): T {
4+
return originalAppendChild.call(this, node) as T
5+
}

test/apps/base-extension/src/background.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { datadogRum } from '@datadog/browser-rum'
2+
import { datadogLogs } from '@datadog/browser-logs'
3+
import type { RumInitConfiguration } from '@datadog/browser-rum-core'
4+
import type { LogsInitConfiguration } from '@datadog/browser-logs'
5+
import type { Context } from '@datadog/browser-core'
6+
7+
declare global {
8+
interface Window {
9+
EXT_RUM_CONFIGURATION?: RumInitConfiguration
10+
RUM_CONTEXT?: Context
11+
EXT_LOGS_CONFIGURATION?: LogsInitConfiguration
12+
LOGS_CONTEXT?: Context
13+
}
14+
}
15+
16+
if (window.EXT_RUM_CONFIGURATION) {
17+
datadogRum.init(window.EXT_RUM_CONFIGURATION)
18+
19+
if (window.RUM_CONTEXT) {
20+
datadogRum.setGlobalContext(window.RUM_CONTEXT)
21+
}
22+
}
23+
24+
if (window.EXT_LOGS_CONFIGURATION) {
25+
datadogLogs.init(window.EXT_LOGS_CONFIGURATION)
26+
27+
if (window.LOGS_CONTEXT) {
28+
datadogLogs.setGlobalContext(window.LOGS_CONTEXT)
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { RumInitConfiguration, RumPublicApi } from '@datadog/browser-rum-core'
2+
import type { LogsInitConfiguration, LogsGlobal } from '@datadog/browser-logs'
3+
import type { Context } from '@datadog/browser-core'
4+
5+
declare global {
6+
interface Window {
7+
RUM_BUNDLE_URL?: string
8+
LOGS_BUNDLE_URL?: string
9+
EXT_RUM_CONFIGURATION?: RumInitConfiguration
10+
RUM_CONTEXT?: Context
11+
EXT_LOGS_CONFIGURATION?: LogsInitConfiguration
12+
LOGS_CONTEXT?: Context
13+
DD_RUM?: RumPublicApi
14+
DD_LOGS?: LogsGlobal
15+
}
16+
}
17+
18+
function load<T extends 'DD_RUM' | 'DD_LOGS'>(
19+
sdk: T,
20+
url: string,
21+
initConfig: T extends 'DD_RUM' ? RumInitConfiguration : LogsInitConfiguration,
22+
globalContext?: Context
23+
) {
24+
const script = document.createElement('script')
25+
script.src = url
26+
script.onload = () => {
27+
if (!window[sdk]) {
28+
console.error(`${sdk} is not loaded`)
29+
return
30+
}
31+
32+
window[sdk].init(initConfig as any)
33+
if (globalContext) {
34+
window[sdk].setGlobalContext(globalContext)
35+
}
36+
}
37+
38+
document.documentElement.appendChild(script)
39+
}
40+
41+
if (window.RUM_BUNDLE_URL && window.EXT_RUM_CONFIGURATION) {
42+
load('DD_RUM', window.RUM_BUNDLE_URL, window.EXT_RUM_CONFIGURATION, window.RUM_CONTEXT)
43+
}
44+
45+
if (window.LOGS_BUNDLE_URL && window.EXT_LOGS_CONFIGURATION) {
46+
load('DD_LOGS', window.LOGS_BUNDLE_URL, window.EXT_LOGS_CONFIGURATION, window.LOGS_CONTEXT)
47+
}

test/apps/base-extension/src/contentScript.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/apps/base-extension/src/form.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/apps/base-extension/src/popup.html

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)