From 6cd58583b219fe16d88fc4250d0ff8e495a988ba Mon Sep 17 00:00:00 2001 From: Xinyi Feng Date: Fri, 16 Aug 2024 12:57:57 -0700 Subject: [PATCH 1/8] add test for redirect --- tests/audiences.test.js | 12 +++++++++++ tests/campaigns.test.js | 12 +++++++++++ tests/experiments.test.js | 12 +++++++++++ .../audiences/page-level--redirect.html | 20 +++++++++++++++++++ .../campaigns/page-level--redirect.html | 19 ++++++++++++++++++ .../experiments/page-level--redirect.html | 12 +++++++++++ 6 files changed, 87 insertions(+) create mode 100644 tests/fixtures/audiences/page-level--redirect.html create mode 100644 tests/fixtures/campaigns/page-level--redirect.html create mode 100644 tests/fixtures/experiments/page-level--redirect.html diff --git a/tests/audiences.test.js b/tests/audiences.test.js index 62b0ef4..acc41f6 100644 --- a/tests/audiences.test.js +++ b/tests/audiences.test.js @@ -65,6 +65,18 @@ test.describe('Page-level audiences', () => { ]); }); + test("Track page is redirected.", async ({ page }) => { + await page.goto( + "/tests/fixtures/audiences/page-level--redirect" + ); + const targetUrl = [ + "/tests/fixtures/audiences/page-level--redirect", + "/tests/fixtures/audiences/page-level-v1", + "/tests/fixtures/audiences/page-level-v2", + ]; + expect(targetUrl).toContain(new URL(page.url()).pathname); + }); + test('Exposes the audiences in a JS API.', async ({ page }) => { await goToAndRunAudience(page, '/tests/fixtures/audiences/page-level'); expect(await page.evaluate(() => window.hlx.audiences)).toContainEqual( diff --git a/tests/campaigns.test.js b/tests/campaigns.test.js index 45277f2..d186638 100644 --- a/tests/campaigns.test.js +++ b/tests/campaigns.test.js @@ -75,6 +75,18 @@ test.describe('Page-level campaigns', () => { ]); }); + test("Track page is redirected.", async ({ page }) => { + await page.goto( + "/tests/fixtures/campaigns/page-level--redirect" + ); + const targetUrl = [ + "/tests/fixtures/campaigns/page-level--redirect", + "/tests/fixtures/campaigns/page-level-v1", + "/tests/fixtures/campaigns/page-level-v2", + ]; + expect(targetUrl).toContain(new URL(page.url()).pathname); + }); + test('Exposes the campaign in a JS API.', async ({ page }) => { await goToAndRunCampaign(page, '/tests/fixtures/campaigns/page-level?campaign=bar'); expect(await page.evaluate(() => window.hlx.campaigns)).toContainEqual( diff --git a/tests/experiments.test.js b/tests/experiments.test.js index 8405c7b..2e26914 100644 --- a/tests/experiments.test.js +++ b/tests/experiments.test.js @@ -117,6 +117,18 @@ test.describe('Page-level experiments', () => { ]); }); + test("Track page is redirected.", async ({ page }) => { + await page.goto( + "/tests/fixtures/experiments/page-level--redirect" + ); + const targetUrl = [ + "/tests/fixtures/experiments/page-level--redirect", + "/tests/fixtures/experiments/page-level-v1", + "/tests/fixtures/experiments/page-level-v2", + ]; + expect(targetUrl).toContain(new URL(page.url()).pathname); + }); + test('Exposes the experiment in a JS API.', async ({ page }) => { await goToAndRunExperiment(page, '/tests/fixtures/experiments/page-level'); expect(await page.evaluate(() => window.hlx.experiments)).toContainEqual( diff --git a/tests/fixtures/audiences/page-level--redirect.html b/tests/fixtures/audiences/page-level--redirect.html new file mode 100644 index 0000000..fc061f7 --- /dev/null +++ b/tests/fixtures/audiences/page-level--redirect.html @@ -0,0 +1,20 @@ + + + + + + + + + + +
+
Hello World!
+
+ + \ No newline at end of file diff --git a/tests/fixtures/campaigns/page-level--redirect.html b/tests/fixtures/campaigns/page-level--redirect.html new file mode 100644 index 0000000..79714b2 --- /dev/null +++ b/tests/fixtures/campaigns/page-level--redirect.html @@ -0,0 +1,19 @@ + + + + + + + + + +
+
Hello World!
+
+ + \ No newline at end of file diff --git a/tests/fixtures/experiments/page-level--redirect.html b/tests/fixtures/experiments/page-level--redirect.html new file mode 100644 index 0000000..392d598 --- /dev/null +++ b/tests/fixtures/experiments/page-level--redirect.html @@ -0,0 +1,12 @@ + + + + + + + + + +
Hello World!
+ + \ No newline at end of file From 23b4bcb2035622005b0a4c7401cd97c1eb547784 Mon Sep 17 00:00:00 2001 From: Xinyi Feng Date: Fri, 16 Aug 2024 21:27:09 -0700 Subject: [PATCH 2/8] fix audience, campaigns tests to test redirect --- tests/audiences.test.js | 20 +++++++++++--------- tests/campaigns.test.js | 12 +++--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/tests/audiences.test.js b/tests/audiences.test.js index acc41f6..142bdb2 100644 --- a/tests/audiences.test.js +++ b/tests/audiences.test.js @@ -66,16 +66,18 @@ test.describe('Page-level audiences', () => { }); test("Track page is redirected.", async ({ page }) => { - await page.goto( - "/tests/fixtures/audiences/page-level--redirect" - ); - const targetUrl = [ - "/tests/fixtures/audiences/page-level--redirect", - "/tests/fixtures/audiences/page-level-v1", - "/tests/fixtures/audiences/page-level-v2", + await page.goto('/tests/fixtures/audiences/page-level--redirect'); + const targetUrls = [ + '/tests/fixtures/audiences/variant-1', + '/tests/fixtures/audiences/variant-2', ]; - expect(targetUrl).toContain(new URL(page.url()).pathname); - }); + await page.waitForFunction( + (targetUrls) => targetUrls.includes(window.location.pathname), + targetUrls + ); + const currentPath = new URL(page.url()).pathname; + expect(targetUrls).toContain(currentPath); +}); test('Exposes the audiences in a JS API.', async ({ page }) => { await goToAndRunAudience(page, '/tests/fixtures/audiences/page-level'); diff --git a/tests/campaigns.test.js b/tests/campaigns.test.js index d186638..278abf9 100644 --- a/tests/campaigns.test.js +++ b/tests/campaigns.test.js @@ -76,15 +76,9 @@ test.describe('Page-level campaigns', () => { }); test("Track page is redirected.", async ({ page }) => { - await page.goto( - "/tests/fixtures/campaigns/page-level--redirect" - ); - const targetUrl = [ - "/tests/fixtures/campaigns/page-level--redirect", - "/tests/fixtures/campaigns/page-level-v1", - "/tests/fixtures/campaigns/page-level-v2", - ]; - expect(targetUrl).toContain(new URL(page.url()).pathname); + await page.goto('/tests/fixtures/campaigns/page-level--redirect?campaign=bar'); + await page.waitForURL('/tests/fixtures/campaigns/variant-2'); + expect(new URL(page.url()).pathname).toBe('/tests/fixtures/campaigns/variant-2'); }); test('Exposes the campaign in a JS API.', async ({ page }) => { From caa1f7f3959d91d1889a96009efd084dba779fa4 Mon Sep 17 00:00:00 2001 From: Xinyi Feng Date: Fri, 16 Aug 2024 21:39:25 -0700 Subject: [PATCH 3/8] update readme --- documentation/experiments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/experiments.md b/documentation/experiments.md index 38c5be5..bf2b0ac 100644 --- a/documentation/experiments.md +++ b/documentation/experiments.md @@ -136,7 +136,7 @@ For the use case that fully redirect to the target URL instead of just replacing | Experiment Variants | [https://{ref}--{repo}--{org}.hlx.page/my-page-variant-1](), [https://{ref}--{repo}--{org}.hlx.page/my-page-variant-2](), [https://{ref}--{repo}--{org}.hlx.page/my-page-variant-3]() | | Experiment Resolution | redirect -In this example, the Hero Test experiment will redirect to one of the specified URLs when you simulate that variant. +In this example, the Hero Test experiment will redirect to one of the specified URLs based on the selected variant. Similarly, the redirects for audience personalization and campaign personalization could be enabled by adding: From 5c2873c1e6584b191038a78364e312c41a9397d8 Mon Sep 17 00:00:00 2001 From: Xinyi Feng Date: Sun, 18 Aug 2024 21:07:17 -0700 Subject: [PATCH 4/8] add test to verify RUM is fired before redirection --- tests/audiences.test.js | 32 ++++++++++++++++++++++++++++++++ tests/campaigns.test.js | 32 ++++++++++++++++++++++++++++++++ tests/experiments.test.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/tests/audiences.test.js b/tests/audiences.test.js index 142bdb2..9555a0d 100644 --- a/tests/audiences.test.js +++ b/tests/audiences.test.js @@ -65,6 +65,38 @@ test.describe('Page-level audiences', () => { ]); }); + test('Track RUM is fired before redirect.', async ({ page }) => { + await page.addInitScript(() => { + window.rumCalls = []; + window.hlx = { rum: { sampleRUM: (...args) => window.rumCalls.push(args) } }; + }); + // Intercept the script + await page.route('src/index.js', async (route) => { + const response = await route.fetch(); + let body = await response.text(); + if (body.includes('window.location.replace')) { + // Comment out the redirect + body = body.replace(/window\.location\.replace\s*\(/g, '//window.location.replace('); + } + await route.fulfill({ + response, + body, + headers: { + ...response.headers(), + 'content-length': String(body.length), + }, + }); + }); + await goToAndRunAudience(page, '/tests/fixtures/audiences/page-level--redirect'); + expect(await page.evaluate(() => window.rumCalls)).toContainEqual([ + 'audience', + expect.objectContaining({ + source: 'foo', + target: 'foo:bar', + }), + ]); + }); + test("Track page is redirected.", async ({ page }) => { await page.goto('/tests/fixtures/audiences/page-level--redirect'); const targetUrls = [ diff --git a/tests/campaigns.test.js b/tests/campaigns.test.js index 278abf9..14434bd 100644 --- a/tests/campaigns.test.js +++ b/tests/campaigns.test.js @@ -75,6 +75,38 @@ test.describe('Page-level campaigns', () => { ]); }); + test('Tracks RUM is fired before redirect.', async ({ page }) => { + await page.addInitScript(() => { + window.rumCalls = []; + window.hlx = { rum: { sampleRUM: (...args) => window.rumCalls.push(args) } }; + }); + // Intercept script + await page.route('src/index.js', async (route) => { + const response = await route.fetch(); + let body = await response.text(); + if (body.includes('window.location.replace')) { + // Comment out the redirect + body = body.replace(/window\.location\.replace\s*\(/g, '//window.location.replace('); + } + await route.fulfill({ + response, + body, + headers: { + ...response.headers(), + 'content-length': String(body.length), + }, + }); + }); + await goToAndRunCampaign(page, '/tests/fixtures/campaigns/page-level--redirect?campaign=foo'); + expect(await page.evaluate(() => window.rumCalls)).toContainEqual([ + 'audience', + expect.objectContaining({ + source: 'foo', + target: 'foo:bar', + }), + ]); + }); + test("Track page is redirected.", async ({ page }) => { await page.goto('/tests/fixtures/campaigns/page-level--redirect?campaign=bar'); await page.waitForURL('/tests/fixtures/campaigns/variant-2'); diff --git a/tests/experiments.test.js b/tests/experiments.test.js index 2e26914..78715eb 100644 --- a/tests/experiments.test.js +++ b/tests/experiments.test.js @@ -117,6 +117,38 @@ test.describe('Page-level experiments', () => { ]); }); + test('Track RUM is fired before redirect.', async ({ page }) => { + await page.addInitScript(() => { + window.rumCalls = []; + window.hlx = { rum: { sampleRUM: (...args) => window.rumCalls.push(args) } }; + }); + // Intercept script + await page.route('src/index.js', async (route) => { + const response = await route.fetch(); + let body = await response.text(); + if (body.includes('window.location.replace')) { + // Commnet out the call of window.location.replace + body = body.replace(/window\.location\.replace\s*\(/g, '//window.location.replace('); + } + await route.fulfill({ + response, + body, + headers: { + ...response.headers(), + 'content-length': String(body.length), + }, + }); + }); + await goToAndRunExperiment(page, '/tests/fixtures/experiments/page-level--redirect'); + expect(await page.evaluate(() => window.rumCalls)).toContainEqual([ + 'experiment', + expect.objectContaining({ + source: 'foo', + target: expect.stringMatching(/control|challenger-1|challenger-2/), + }), + ]); + }); + test("Track page is redirected.", async ({ page }) => { await page.goto( "/tests/fixtures/experiments/page-level--redirect" From 2a01864ef288fd3bece461048baec4f7b126e9c0 Mon Sep 17 00:00:00 2001 From: Xinyi Feng Date: Mon, 19 Aug 2024 16:27:53 -0700 Subject: [PATCH 5/8] rewrite track RUM fired test --- tests/audiences.test.js | 60 +++++++++++---------------------------- tests/campaigns.test.js | 42 +++++++-------------------- tests/experiments.test.js | 58 +++++++++++-------------------------- 3 files changed, 43 insertions(+), 117 deletions(-) diff --git a/tests/audiences.test.js b/tests/audiences.test.js index 9555a0d..1e5e1e0 100644 --- a/tests/audiences.test.js +++ b/tests/audiences.test.js @@ -65,50 +65,22 @@ test.describe('Page-level audiences', () => { ]); }); - test('Track RUM is fired before redirect.', async ({ page }) => { - await page.addInitScript(() => { - window.rumCalls = []; - window.hlx = { rum: { sampleRUM: (...args) => window.rumCalls.push(args) } }; - }); - // Intercept the script - await page.route('src/index.js', async (route) => { - const response = await route.fetch(); - let body = await response.text(); - if (body.includes('window.location.replace')) { - // Comment out the redirect - body = body.replace(/window\.location\.replace\s*\(/g, '//window.location.replace('); - } - await route.fulfill({ - response, - body, - headers: { - ...response.headers(), - 'content-length': String(body.length), - }, - }); - }); - await goToAndRunAudience(page, '/tests/fixtures/audiences/page-level--redirect'); - expect(await page.evaluate(() => window.rumCalls)).toContainEqual([ - 'audience', - expect.objectContaining({ - source: 'foo', - target: 'foo:bar', - }), - ]); - }); - - test("Track page is redirected.", async ({ page }) => { - await page.goto('/tests/fixtures/audiences/page-level--redirect'); - const targetUrls = [ - '/tests/fixtures/audiences/variant-1', - '/tests/fixtures/audiences/variant-2', - ]; - await page.waitForFunction( - (targetUrls) => targetUrls.includes(window.location.pathname), - targetUrls - ); - const currentPath = new URL(page.url()).pathname; - expect(targetUrls).toContain(currentPath); +test('Track RUM is fired before redirect.', async ({ page }) => { + const rumCalls = []; + await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args)); + await page.addInitScript(() => { + window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } }; + }); + await page.goto('/tests/fixtures/audiences/page-level--redirect'); + await page.waitForURL('/tests/fixtures/audiences/variant-1'); + expect(await page.evaluate(() => window.document.body.innerText)).toEqual('Hello v1!'); + expect(rumCalls[0]).toContainEqual([ + 'audience', + { + source: 'foo', + target: 'foo:bar', + }, + ]); }); test('Exposes the audiences in a JS API.', async ({ page }) => { diff --git a/tests/campaigns.test.js b/tests/campaigns.test.js index 14434bd..1c4fef3 100644 --- a/tests/campaigns.test.js +++ b/tests/campaigns.test.js @@ -75,44 +75,24 @@ test.describe('Page-level campaigns', () => { ]); }); - test('Tracks RUM is fired before redirect.', async ({ page }) => { + test('Track RUM is fired before redirect.', async ({ page }) => { + const rumCalls = []; + await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args)); await page.addInitScript(() => { - window.rumCalls = []; - window.hlx = { rum: { sampleRUM: (...args) => window.rumCalls.push(args) } }; + window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } }; }); - // Intercept script - await page.route('src/index.js', async (route) => { - const response = await route.fetch(); - let body = await response.text(); - if (body.includes('window.location.replace')) { - // Comment out the redirect - body = body.replace(/window\.location\.replace\s*\(/g, '//window.location.replace('); - } - await route.fulfill({ - response, - body, - headers: { - ...response.headers(), - 'content-length': String(body.length), - }, - }); - }); - await goToAndRunCampaign(page, '/tests/fixtures/campaigns/page-level--redirect?campaign=foo'); - expect(await page.evaluate(() => window.rumCalls)).toContainEqual([ + await page.goto('/tests/fixtures/campaigns/page-level--redirect?campaign=bar'); + await page.waitForURL('/tests/fixtures/campaigns/variant-2'); + expect(await page.evaluate(() => window.document.body.innerText)).toEqual('Hello v2!'); + expect(rumCalls[0]).toContainEqual([ 'audience', - expect.objectContaining({ - source: 'foo', + { + source: 'bar', target: 'foo:bar', - }), + }, ]); }); - test("Track page is redirected.", async ({ page }) => { - await page.goto('/tests/fixtures/campaigns/page-level--redirect?campaign=bar'); - await page.waitForURL('/tests/fixtures/campaigns/variant-2'); - expect(new URL(page.url()).pathname).toBe('/tests/fixtures/campaigns/variant-2'); - }); - test('Exposes the campaign in a JS API.', async ({ page }) => { await goToAndRunCampaign(page, '/tests/fixtures/campaigns/page-level?campaign=bar'); expect(await page.evaluate(() => window.hlx.campaigns)).toContainEqual( diff --git a/tests/experiments.test.js b/tests/experiments.test.js index 78715eb..dabb926 100644 --- a/tests/experiments.test.js +++ b/tests/experiments.test.js @@ -117,50 +117,24 @@ test.describe('Page-level experiments', () => { ]); }); - test('Track RUM is fired before redirect.', async ({ page }) => { - await page.addInitScript(() => { - window.rumCalls = []; - window.hlx = { rum: { sampleRUM: (...args) => window.rumCalls.push(args) } }; - }); - // Intercept script - await page.route('src/index.js', async (route) => { - const response = await route.fetch(); - let body = await response.text(); - if (body.includes('window.location.replace')) { - // Commnet out the call of window.location.replace - body = body.replace(/window\.location\.replace\s*\(/g, '//window.location.replace('); - } - await route.fulfill({ - response, - body, - headers: { - ...response.headers(), - 'content-length': String(body.length), - }, + test('Track RUM is fired before redirect.', async ({ page }) => { + const rumCalls = []; + await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args)); + await page.addInitScript(() => { + window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } }; }); + await page.goto('/tests/fixtures/experiments/page-level--redirect'); + await page.waitForURL(/\/tests\/fixtures\/experiments\/page-level(--redirect|-v[12])$/); + expect(await page.evaluate(() => window.document.body.innerText)).toMatch(/Hello (v[12]|World)!/); + expect(rumCalls[0]).toContainEqual([ + 'experiment', + { + source: 'foo', + target: expect.stringMatching(/control|challenger-1|challenger-2/), + }, + ]); }); - await goToAndRunExperiment(page, '/tests/fixtures/experiments/page-level--redirect'); - expect(await page.evaluate(() => window.rumCalls)).toContainEqual([ - 'experiment', - expect.objectContaining({ - source: 'foo', - target: expect.stringMatching(/control|challenger-1|challenger-2/), - }), - ]); - }); - - test("Track page is redirected.", async ({ page }) => { - await page.goto( - "/tests/fixtures/experiments/page-level--redirect" - ); - const targetUrl = [ - "/tests/fixtures/experiments/page-level--redirect", - "/tests/fixtures/experiments/page-level-v1", - "/tests/fixtures/experiments/page-level-v2", - ]; - expect(targetUrl).toContain(new URL(page.url()).pathname); - }); - + test('Exposes the experiment in a JS API.', async ({ page }) => { await goToAndRunExperiment(page, '/tests/fixtures/experiments/page-level'); expect(await page.evaluate(() => window.hlx.experiments)).toContainEqual( From 318ae875a41ae903e5b4dc311553081b9eeb9db1 Mon Sep 17 00:00:00 2001 From: Julien Ramboz Date: Mon, 19 Aug 2024 16:59:10 -0700 Subject: [PATCH 6/8] test: stabilize experiment redirect test by enforcing a variant --- tests/experiments.test.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/experiments.test.js b/tests/experiments.test.js index dabb926..73116d4 100644 --- a/tests/experiments.test.js +++ b/tests/experiments.test.js @@ -117,24 +117,24 @@ test.describe('Page-level experiments', () => { ]); }); - test('Track RUM is fired before redirect.', async ({ page }) => { - const rumCalls = []; - await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args)); - await page.addInitScript(() => { - window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } }; - }); - await page.goto('/tests/fixtures/experiments/page-level--redirect'); - await page.waitForURL(/\/tests\/fixtures\/experiments\/page-level(--redirect|-v[12])$/); - expect(await page.evaluate(() => window.document.body.innerText)).toMatch(/Hello (v[12]|World)!/); - expect(rumCalls[0]).toContainEqual([ - 'experiment', - { - source: 'foo', - target: expect.stringMatching(/control|challenger-1|challenger-2/), - }, - ]); + test('Track RUM is fired before redirect.', async ({ page }) => { + const rumCalls = []; + await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args)); + await page.addInitScript(() => { + window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } }; }); - + await page.goto('/tests/fixtures/experiments/page-level--redirect?experiment=foo/challenger-1'); + await page.waitForURL('/tests/fixtures/experiments/page-level-v1'); + expect(await page.evaluate(() => window.document.body.innerText)).toMatch(/Hello v1!/); + expect(rumCalls[0]).toContainEqual([ + 'experiment', + { + source: 'foo', + target: 'challenger-1', + }, + ]); + }); + test('Exposes the experiment in a JS API.', async ({ page }) => { await goToAndRunExperiment(page, '/tests/fixtures/experiments/page-level'); expect(await page.evaluate(() => window.hlx.experiments)).toContainEqual( From e4e22571ce3b2d63814b790391c6ecdd513fdb78 Mon Sep 17 00:00:00 2001 From: Julien Ramboz Date: Mon, 19 Aug 2024 17:00:33 -0700 Subject: [PATCH 7/8] chore: properly lint test files --- package.json | 2 +- tests/audiences.test.js | 34 +++++++++++++++++----------------- tests/utils.js | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 6b8a714..d8bf5ce 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.1", "main": "src/index.js", "scripts": { - "lint:js": "eslint src", + "lint:js": "eslint src tests", "lint:css": "stylelint src/**/*.css --allow-empty-input", "lint": "npm run lint:js && npm run lint:css", "start": "http-server . -p 3000", diff --git a/tests/audiences.test.js b/tests/audiences.test.js index 1e5e1e0..8ce57cc 100644 --- a/tests/audiences.test.js +++ b/tests/audiences.test.js @@ -65,23 +65,23 @@ test.describe('Page-level audiences', () => { ]); }); -test('Track RUM is fired before redirect.', async ({ page }) => { - const rumCalls = []; - await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args)); - await page.addInitScript(() => { - window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } }; - }); - await page.goto('/tests/fixtures/audiences/page-level--redirect'); - await page.waitForURL('/tests/fixtures/audiences/variant-1'); - expect(await page.evaluate(() => window.document.body.innerText)).toEqual('Hello v1!'); - expect(rumCalls[0]).toContainEqual([ - 'audience', - { - source: 'foo', - target: 'foo:bar', - }, - ]); -}); + test('Track RUM is fired before redirect.', async ({ page }) => { + const rumCalls = []; + await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args)); + await page.addInitScript(() => { + window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } }; + }); + await page.goto('/tests/fixtures/audiences/page-level--redirect'); + await page.waitForURL('/tests/fixtures/audiences/variant-1'); + expect(await page.evaluate(() => window.document.body.innerText)).toEqual('Hello v1!'); + expect(rumCalls[0]).toContainEqual([ + 'audience', + { + source: 'foo', + target: 'foo:bar', + }, + ]); + }); test('Exposes the audiences in a JS API.', async ({ page }) => { await goToAndRunAudience(page, '/tests/fixtures/audiences/page-level'); diff --git a/tests/utils.js b/tests/utils.js index 1df0890..38656fb 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -30,5 +30,5 @@ export async function waitForDomEvent(page, eventName) { document.addEventListener(name, (ev) => resolve(ev.detail)); }); }, eventName); - return async () => await window.eventPromise; + return async () => window.eventPromise; } From a949eb3a73027b61b417aac13c31e219b8a0f72f Mon Sep 17 00:00:00 2001 From: Xinyi Feng Date: Tue, 20 Aug 2024 09:20:51 -0700 Subject: [PATCH 8/8] update: test RUM is fired before redirect by using waitForFunction --- tests/experiments.test.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/tests/experiments.test.js b/tests/experiments.test.js index 73116d4..e6380ad 100644 --- a/tests/experiments.test.js +++ b/tests/experiments.test.js @@ -117,22 +117,38 @@ test.describe('Page-level experiments', () => { ]); }); - test('Track RUM is fired before redirect.', async ({ page }) => { + test('Track RUM is fired before redirect', async ({ page }) => { const rumCalls = []; await page.exposeFunction('logRumCall', (...args) => rumCalls.push(args)); await page.addInitScript(() => { window.hlx = { rum: { sampleRUM: (...args) => window.logRumCall(args) } }; }); - await page.goto('/tests/fixtures/experiments/page-level--redirect?experiment=foo/challenger-1'); - await page.waitForURL('/tests/fixtures/experiments/page-level-v1'); - expect(await page.evaluate(() => window.document.body.innerText)).toMatch(/Hello v1!/); + await page.goto('/tests/fixtures/experiments/page-level--redirect'); + await page.waitForFunction(() => window.hlx.rum.sampleRUM); expect(rumCalls[0]).toContainEqual([ 'experiment', { source: 'foo', - target: 'challenger-1', + target: expect.stringMatching(/control|challenger-1|challenger-2/), }, ]); + + const expectedContent = { + v1: 'Hello v1!', + v2: 'Hello v2!', + redirect: 'Hello World!', + }; + const expectedUrlPath = { + v1: '/tests/fixtures/experiments/page-level-v1', + v2: '/tests/fixtures/experiments/page-level-v2', + redirect: '/tests/fixtures/experiments/page-level--redirect', + }; + const url = new URL(page.url()); + const variant = Object.keys(expectedUrlPath).find((k) => url.pathname.endsWith(k)); + expect(await page.evaluate(() => window.document.body.innerText)).toMatch( + new RegExp(expectedContent[variant]), + ); + expect(expectedUrlPath[variant]).toBe(url.pathname); }); test('Exposes the experiment in a JS API.', async ({ page }) => {