Skip to content

Commit

Permalink
Move puppeteer inject helper out of createTest
Browse files Browse the repository at this point in the history
  • Loading branch information
cy-moi committed Nov 25, 2024
1 parent 42a0085 commit ef5afb3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function createScrollValuesObservable(

const observerTarget = document.scrollingElement || document.documentElement
const resizeObserver = new ResizeObserver(monitor(throttledNotify.throttled))
if (observerTarget instanceof Element) {
if (observerTarget) {
resizeObserver.observe(observerTarget)
}
const eventListener = addEventListener(configuration, window, DOM_EVENT.SCROLL, throttledNotify.throttled, {
Expand Down
32 changes: 0 additions & 32 deletions test/e2e/lib/framework/createTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from 'fs'
import type { LogsInitConfiguration } from '@datadog/browser-logs'
import type { RumInitConfiguration } from '@datadog/browser-rum-core'
import { DefaultPrivacyLevel } from '@datadog/browser-rum'
Expand All @@ -15,7 +14,6 @@ import type { SetupFactory, SetupOptions } from './pageSetups'
import { DEFAULT_SETUPS, npmSetup } from './pageSetups'
import { createIntakeServerApp } from './serverApps/intake'
import { createMockServerApp } from './serverApps/mock'
import { RUM_BUNDLE } from './sdkBuilds'

const DEFAULT_RUM_CONFIGURATION = {
applicationId: APPLICATION_ID,
Expand Down Expand Up @@ -230,33 +228,3 @@ async function tearDownTest({ intakeRegistry }: TestContext) {
})
await deleteAllCookies()
}

export async function injectRumWithPuppeteer() {
const ddRUM = fs.readFileSync(RUM_BUNDLE, 'utf8')
const puppeteerBrowser = await browser.getPuppeteer()
let injected = true

await browser.call(async () => {
const page = await puppeteerBrowser.newPage()
await page.evaluateOnNewDocument(
`
if (location.href !== 'about:blank') {
${ddRUM}
window.DD_RUM._setDebug(true)
window.DD_RUM.init({
applicationId: ${APPLICATION_ID},
clientToken: ${CLIENT_TOKEN},
})
window.DD_RUM.startView()
}
`
)
page.on('console', (msg) => {
if (msg.type() === 'error') {
injected = false
}
})
await page.goto('https://webdriver.io')
})
return injected
}
2 changes: 1 addition & 1 deletion test/e2e/lib/framework/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { createTest, injectRumWithPuppeteer } from './createTest'
export { createTest } from './createTest'
export { bundleSetup, html } from './pageSetups'
export { IntakeRegistry } from './intakeRegistry'
export { getTestServers, waitForServersIdle } from './httpServers'
Expand Down
35 changes: 34 additions & 1 deletion test/e2e/scenario/rum/s8sInject.scenario.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,41 @@
import { injectRumWithPuppeteer } from '../../lib/framework'
import { RUM_BUNDLE } from '../../lib/framework/sdkBuilds'
import { APPLICATION_ID, CLIENT_TOKEN } from '../../lib/helpers/configuration'
import * as fs from 'fs'

describe('Inject RUM with Puppeteer', () => {
// S8s tests inject RUM with puppeteer evaluateOnNewDocument
it('should not throw error in chrome', async () => {
const isInjected = await injectRumWithPuppeteer()
expect(isInjected).toBe(true)
})
})

async function injectRumWithPuppeteer() {
const ddRUM = fs.readFileSync(RUM_BUNDLE, 'utf8')
const puppeteerBrowser = await browser.getPuppeteer()
let injected = true

await browser.call(async () => {
const page = await puppeteerBrowser.newPage()
await page.evaluateOnNewDocument(
`
if (location.href !== 'about:blank') {
${ddRUM}
window.DD_RUM._setDebug(true)
window.DD_RUM.init({
applicationId: ${APPLICATION_ID},
clientToken: ${CLIENT_TOKEN},
})
window.DD_RUM.startView()
}
`
)
page.on('console', (msg) => {
if (msg.type() === 'error') {
injected = false
}
})
await page.goto('https://webdriver.io')
})
return injected
}

0 comments on commit ef5afb3

Please sign in to comment.