Skip to content

Commit 176ad0e

Browse files
committed
Fix tests
1 parent 59fc018 commit 176ad0e

File tree

3 files changed

+112
-22
lines changed

3 files changed

+112
-22
lines changed

test/e2e/onLCP-test.js

Lines changed: 109 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ describe('onLCP()', async function () {
3838
browserSupportsPrerender = await browser.execute(() => {
3939
return 'onprerenderingchange' in document;
4040
});
41+
});
4142

43+
beforeEach(async function () {
4244
await navigateTo('about:blank');
4345
await clearBeacons();
4446
});
@@ -129,7 +131,12 @@ describe('onLCP()', async function () {
129131
await imagesPainted();
130132

131133
await beaconCountIs(2);
132-
const [lcp1, lcp2] = await getBeacons();
134+
const beacons = await getBeacons();
135+
// Firefox sometimes sents <p>, then <h1>
136+
// so grab last two
137+
assert(beacons.length >= 2);
138+
const lcp1 = beacons.at(-2);
139+
const lcp2 = beacons.at(-1);
133140

134141
assert(lcp1.value > 0);
135142
assert(lcp1.id.match(/^v5-\d+-\d+$/));
@@ -157,15 +164,24 @@ describe('onLCP()', async function () {
157164
// Wait until all images are loaded and fully rendered.
158165
await imagesPainted();
159166

167+
// Wait until web-vitals is loaded
168+
await webVitalsLoaded();
169+
170+
// Click on the h1.
171+
const h1 = await $('h1');
172+
await h1.click();
173+
174+
await beaconCountIs(1);
175+
await clearBeacons();
176+
160177
// Wait a bit to allow the prerender to happen
161178
await browser.pause(1000);
162179

163180
const prerenderLink = await $('#prerender-link');
164181
await prerenderLink.click();
165182

166-
// Check the beacon has come in
167-
await beaconCountIs(1);
168-
await clearBeacons();
183+
// Wait a bit for the navigation to start
184+
await browser.pause(500);
169185

170186
// Wait until all images are loaded and fully rendered.
171187
await imagesPainted();
@@ -183,6 +199,7 @@ describe('onLCP()', async function () {
183199
assert.strictEqual(lcp.rating, 'good');
184200
assert.strictEqual(lcp.entries[0].startTime - activationStart, lcp.value);
185201
assert.strictEqual(lcp.navigationType, 'prerender');
202+
await clearBeacons();
186203
});
187204

188205
it('does not report if the browser does not support LCP (including bfcache restores)', async function () {
@@ -223,9 +240,19 @@ describe('onLCP()', async function () {
223240
it('does not report if the document was hidden at page load time', async function () {
224241
if (!browserSupportsLCP) this.skip();
225242

226-
await navigateTo('/test/lcp?hidden=1', {readyState: 'interactive'});
227-
228-
await stubVisibilityChange('visible');
243+
// Can't mock visibility-state entries so load in a new blank tab:
244+
const originalHandle = await browser.getWindowHandle();
245+
await browser.execute(
246+
"window.open('http://localhost:9090/test/lcp', '_blank')",
247+
);
248+
// immediately switch back before page load starts—annoyingly you can't
249+
// open in a hidden tab as ChromeDriver foregrounds it, but this works.
250+
await browser.switchToWindow(originalHandle);
251+
await browser.pause(500);
252+
// Then switch to the new tab to do our tests
253+
const handles = await browser.getWindowHandles();
254+
const newTabHandle = handles.find((h) => h !== originalHandle);
255+
await browser.switchToWindow(newTabHandle);
229256

230257
// Click on the h1.
231258
const h1 = await $('h1');
@@ -236,12 +263,48 @@ describe('onLCP()', async function () {
236263

237264
const beacons = await getBeacons();
238265
assert.strictEqual(beacons.length, 0);
266+
267+
// Reset everything
268+
await browser.closeWindow();
269+
await browser.switchToWindow(originalHandle);
239270
});
240271

241-
it('does not report if the document was hidden before library loaded', async function () {
272+
it('does not report if hidden before library loaded and visibilitystate supported', async function () {
242273
if (!browserSupportsLCP) this.skip();
243274
if (!browserSupportsVisibilityState) this.skip();
244275

276+
// Don't load the library until we click
277+
await navigateTo('/test/lcp?loadAfterInput=1&imgDelay=500');
278+
279+
// Can't mock visibility-state entries so switch to a blank tab and back
280+
// to emit real entries:
281+
const handle1 = await browser.getWindowHandle();
282+
await browser.newWindow('https://example.com');
283+
await browser.pause(500);
284+
await browser.closeWindow();
285+
await browser.switchToWindow(handle1);
286+
287+
// Click on the h1 to load the library
288+
const h1 = await $('h1');
289+
await h1.click();
290+
291+
// Wait until web-vitals is loaded
292+
await webVitalsLoaded();
293+
294+
// Wait a bit to ensure no beacons were sent.
295+
await browser.pause(1000);
296+
297+
// Click on the h1 again now it's loaded to trigger LCP
298+
await h1.click();
299+
300+
const beacons = await getBeacons();
301+
assert.strictEqual(beacons.length, 0);
302+
});
303+
304+
it('does report if hidden before library loaded and visibilitystate not supported', async function () {
305+
if (!browserSupportsLCP) this.skip();
306+
if (browserSupportsVisibilityState) this.skip();
307+
245308
// Don't load the library until we click
246309
await navigateTo('/test/lcp?loadAfterInput=1');
247310

@@ -260,14 +323,14 @@ describe('onLCP()', async function () {
260323
// Wait until web-vitals is loaded
261324
await webVitalsLoaded();
262325

263-
// Click on the h1 again not it's loaded to trigger LCP
326+
// Click on the h1 again now it's loaded to trigger LCP
264327
await h1.click();
265328

266329
// Wait a bit to ensure no beacons were sent.
267330
await browser.pause(1000);
268331

269-
const beacons = await getBeacons();
270-
assert.strictEqual(beacons.length, 0);
332+
await beaconCountIs(1);
333+
assertStandardReportsAreCorrect(await getBeacons());
271334
});
272335

273336
it('does not report if the document changes to hidden before the first render', async function () {
@@ -333,11 +396,7 @@ describe('onLCP()', async function () {
333396
const h1 = await $('h1');
334397
await h1.click();
335398

336-
// Wait a bit to ensure no additional beacons were sent.
337-
await browser.pause(1000);
338-
339399
await beaconCountIs(1);
340-
341400
const [lcp1] = await getBeacons();
342401

343402
assert(lcp1.value > 0);
@@ -355,7 +414,9 @@ describe('onLCP()', async function () {
355414
await navigateTo('/test/lcp?reportAllChanges=1&imgDelay=0&imgHidden=1');
356415

357416
await beaconCountIs(1);
358-
const [lcp] = await getBeacons();
417+
// Firefox sometimes sends a <p> and then <h1> beacon, so grab last one
418+
let beacons = await getBeacons();
419+
const lcp = beacons.at(-1);
359420

360421
assert(lcp.value > 0);
361422
assert.strictEqual(lcp.name, 'LCP');
@@ -376,7 +437,7 @@ describe('onLCP()', async function () {
376437
// Wait a bit to ensure no beacons were sent.
377438
await browser.pause(1000);
378439

379-
const beacons = await getBeacons();
440+
beacons = await getBeacons();
380441
assert.strictEqual(beacons.length, 0);
381442
});
382443

@@ -426,7 +487,19 @@ describe('onLCP()', async function () {
426487
it('reports if the page is restored from bfcache even when the document was hidden at page load time', async function () {
427488
if (!browserSupportsLCP) this.skip();
428489

429-
await navigateTo('/test/lcp?hidden=1', {readyState: 'interactive'});
490+
// Can't mock visibility-state entries so load in a new blank tab:
491+
const originalHandle = await browser.getWindowHandle();
492+
await browser.execute(
493+
"window.open('http://localhost:9090/test/lcp', '_blank')",
494+
);
495+
// immediately switch back before page load starts—annoyingly you can't
496+
// open in a hidden tab as ChromeDriver foregrounds it, but this works.
497+
await browser.switchToWindow(originalHandle);
498+
await browser.pause(500);
499+
// Then switch to the new tab to do our tests
500+
const handles = await browser.getWindowHandles();
501+
const newTabHandle = handles.find((h) => h !== originalHandle);
502+
await browser.switchToWindow(newTabHandle);
430503

431504
await stubVisibilityChange('visible');
432505

@@ -466,6 +539,10 @@ describe('onLCP()', async function () {
466539
assert.strictEqual(lcp2.rating, 'good');
467540
assert.strictEqual(lcp2.entries.length, 0);
468541
assert.strictEqual(lcp2.navigationType, 'back-forward-cache');
542+
543+
// Reset everything
544+
await browser.closeWindow();
545+
await browser.switchToWindow(originalHandle);
469546
});
470547

471548
it('reports restore as nav type for wasDiscarded', async function () {
@@ -669,6 +746,13 @@ describe('onLCP()', async function () {
669746

670747
await navigateTo('/test/lcp?attribution=1&prerender=1');
671748

749+
// Wait until web-vitals is loaded
750+
await webVitalsLoaded();
751+
752+
// Click on the h1.
753+
const h1 = await $('h1');
754+
await h1.click();
755+
672756
await beaconCountIs(1);
673757
await clearBeacons();
674758

@@ -678,7 +762,8 @@ describe('onLCP()', async function () {
678762
const prerenderLink = await $('#prerender-link');
679763
await prerenderLink.click();
680764

681-
await beaconCountIs(1);
765+
// Wait a bit for the navigation to start
766+
await browser.pause(500);
682767

683768
// Wait until all images are loaded and fully rendered.
684769
await imagesPainted();
@@ -837,7 +922,11 @@ const assertStandardReportsAreCorrect = (beacons) => {
837922
};
838923

839924
const assertFullReportsAreCorrect = (beacons) => {
840-
const [lcp1, lcp2] = beacons;
925+
// Firefox sometimes sents <p>, then <h1>
926+
// so grab last two
927+
assert(beacons.length >= 2);
928+
const lcp1 = beacons.at(-2);
929+
const lcp2 = beacons.at(-1);
841930

842931
assert(lcp1.value < 500); // Less than the image load delay.
843932
assert(lcp1.id.match(/^v5-\d+-\d+$/));

test/e2e/onTTFB-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ describe('onTTFB()', async function () {
5959
this.retries(2);
6060

6161
let browserSupportsPrerender;
62-
beforeEach(async function () {
62+
before(async function () {
6363
browserSupportsPrerender = await browser.execute(() => {
6464
return 'onprerenderingchange' in document;
6565
});
66+
});
67+
beforeEach(async function () {
6668
// In Safari when navigating to 'about:blank' between tests the
6769
// Navigation Timing data is consistently negative, so the tests fail.
6870
if (browser.capabilities.browserName !== 'Safari') {

test/views/layout.njk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@
165165
166166
self.__lazyLoad = Boolean(infer('lazyLoad'));
167167
self.__loadAfterInput = Boolean(infer('loadAfterInput'));
168-
self.__prerender = Boolean(infer('loadAfterInput'));
169168
170169
if (params.has('hidden')) {
171170
// Stub the page being loaded in the hidden state, but defer to the

0 commit comments

Comments
 (0)