diff --git a/packages/integration-tests/src/pages/record-page.ts b/packages/integration-tests/src/pages/record-page.ts index 47de6a36..8556916d 100644 --- a/packages/integration-tests/src/pages/record-page.ts +++ b/packages/integration-tests/src/pages/record-page.ts @@ -30,6 +30,15 @@ export class RecordPage { private readonly context: BrowserContext, ) {} + changeVisibilityInTab = async (state: 'visible' | 'hidden') => { + await this.page.evaluate((stateInner) => { + Object.defineProperty(document, 'visibilityState', { value: stateInner, writable: true }) + Object.defineProperty(document, 'hidden', { value: Boolean(stateInner === 'hidden'), writable: true }) + + window.dispatchEvent(new Event('visibilitychange')) + }, state) + } + clearReceivedSpans() { this.receivedSpans = [] } diff --git a/packages/web/integration-tests/tests/native/native.ejs b/packages/integration-tests/src/tests/native/native.ejs similarity index 100% rename from packages/web/integration-tests/tests/native/native.ejs rename to packages/integration-tests/src/tests/native/native.ejs diff --git a/packages/web/integration-tests/tests/native/native.spec.js b/packages/integration-tests/src/tests/native/native.spec.ts similarity index 51% rename from packages/web/integration-tests/tests/native/native.spec.js rename to packages/integration-tests/src/tests/native/native.spec.ts index f82c74d9..4173e4e3 100644 --- a/packages/web/integration-tests/tests/native/native.spec.js +++ b/packages/integration-tests/src/tests/native/native.spec.ts @@ -15,17 +15,19 @@ * limitations under the License. * */ +import { expect } from '@playwright/test' +import { test } from '../../utils/test' -module.exports = { - 'native session id integration': async function (browser) { - const url = browser.globals.getUrl('/native/native.ejs') - await browser.url(url) +test.describe('native', () => { + test('native session id integration', async ({ recordPage }) => { + await recordPage.goTo('/native/native.ejs') - const anySpan = await browser.globals.findSpan((span) => span.tags['splunk.rumSessionId'] !== undefined) + await recordPage.waitForSpans((spans) => spans.some((s) => s.name === 'documentFetch')) + const receivedSpans = recordPage.receivedSpans - await browser.assert.ok(anySpan) - - await browser.assert.strictEqual(anySpan.tags['splunk.rumSessionId'], '12341234123412341234123412341234') - await browser.globals.assertNoErrorSpans() - }, -} + expect( + receivedSpans.every((span) => span.tags['splunk.rumSessionId'] === '12341234123412341234123412341234'), + ).toBe(true) + expect(recordPage.receivedErrorSpans).toHaveLength(0) + }) +}) diff --git a/packages/web/integration-tests/tests/visibility/visibility.ejs b/packages/integration-tests/src/tests/visibility/visibility.ejs similarity index 100% rename from packages/web/integration-tests/tests/visibility/visibility.ejs rename to packages/integration-tests/src/tests/visibility/visibility.ejs diff --git a/packages/integration-tests/src/tests/visibility/visibility.spec.ts b/packages/integration-tests/src/tests/visibility/visibility.spec.ts new file mode 100644 index 00000000..33f636bb --- /dev/null +++ b/packages/integration-tests/src/tests/visibility/visibility.spec.ts @@ -0,0 +1,38 @@ +/** + * + * Copyright 2024 Splunk Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +import { test } from '../../utils/test' +import { expect } from '@playwright/test' + +test.describe('visibility', () => { + test('native session id integration', async ({ recordPage }) => { + await recordPage.goTo('/visibility/visibility.ejs') + await recordPage.waitForTimeout(1000) + + await recordPage.changeVisibilityInTab('hidden') + await recordPage.changeVisibilityInTab('visible') + + await recordPage.waitForSpans((spans) => spans.filter((span) => span.name === 'visibility').length >= 2) + + const receivedSpans = recordPage.receivedSpans + const visibilitySpans = receivedSpans.filter((span) => span.name === 'visibility') + + expect(visibilitySpans).toHaveLength(2) + expect(visibilitySpans[0].tags['hidden']).toBe('true') + expect(visibilitySpans[1].tags['hidden']).toBe('false') + }) +}) diff --git a/packages/web/integration-tests/tests/visibility/visibility.spec.js b/packages/web/integration-tests/tests/visibility/visibility.spec.js deleted file mode 100644 index ab9c7c8a..00000000 --- a/packages/web/integration-tests/tests/visibility/visibility.spec.js +++ /dev/null @@ -1,43 +0,0 @@ -/** - * - * Copyright 2024 Splunk Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -module.exports = { - 'visibility events are captured': async function (browser) { - await browser.url(browser.globals.getUrl('/visibility/visibility.ejs')) - browser.globals.emulateTabSwitching(true) - - const tabHiddenSpan = await browser.globals.findSpan( - (span) => span.name === 'visibility' && span.tags['hidden'] === 'true', - ) - const hiddenSpans = browser.globals.getReceivedSpans().filter((span) => span.name === 'visibility') - await browser.assert.ok(!!tabHiddenSpan, 'Visibility event for hidden tab exists') - await browser.assert.strictEqual(hiddenSpans.length, 1, 'There is only one visibility event') - - browser.globals.clearReceivedSpans() - - browser.globals.emulateTabSwitching(false) - const tabVisibileSpan = await browser.globals.findSpan( - (span) => span.name === 'visibility' && span.tags['hidden'] === 'false', - ) - const visibleSpans = browser.globals.getReceivedSpans().filter((span) => span.name === 'visibility') - await browser.assert.ok(!!tabVisibileSpan, 'Visibility event for visible tab exists') - await browser.assert.strictEqual(visibleSpans.length, 1, 'There is only one visibility event') - - await browser.globals.assertNoErrorSpans() - }, -}