|
| 1 | +// enables intelligent code completion for Cypress commands |
| 2 | +// https://on.cypress.io/intelligent-code-completion |
| 3 | +/// <reference types="cypress" /> |
| 4 | + |
| 5 | +/* eslint-disable no-console */ |
| 6 | + |
| 7 | +// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Server-Timing |
| 8 | +// for now assume it is always something like "cache;desc="my example";dur=2002" |
| 9 | +const parseTiming = (singleValue) => { |
| 10 | + const parts = singleValue.split(';') |
| 11 | + |
| 12 | + return { |
| 13 | + name: parts[0], |
| 14 | + description: parts[1].split('"')[1], |
| 15 | + duration: parseFloat(parts[2].split('=')[1]), // in ms |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +const parseServerTimings = (value) => { |
| 20 | + return value.split(',') |
| 21 | + .map((s) => s.trim()) |
| 22 | + .map(parseTiming) |
| 23 | +} |
| 24 | + |
| 25 | +const logTiming = (timing) => { |
| 26 | + return cy.log(`**${timing.name}** ${timing.description} ${timing.duration}ms`) |
| 27 | +} |
| 28 | + |
| 29 | +describe('Server', () => { |
| 30 | + it('reports timings', function () { |
| 31 | + cy.intercept('/').as('document') |
| 32 | + cy.visit('/') |
| 33 | + cy.wait('@document').its('response.headers.server-timing') |
| 34 | + .then(parseServerTimings) |
| 35 | + .then(console.table) |
| 36 | + }) |
| 37 | + |
| 38 | + it('logs timings', function () { |
| 39 | + cy.intercept('/').as('document') |
| 40 | + cy.visit('/') |
| 41 | + cy.wait('@document').its('response.headers.server-timing') |
| 42 | + .then(parseServerTimings) |
| 43 | + .then((timings) => { |
| 44 | + timings.forEach(logTiming) |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + it('reports timings using performance entry', () => { |
| 49 | + cy.visit('/') |
| 50 | + cy.window().its('performance') |
| 51 | + .invoke('getEntriesByType', 'navigation') |
| 52 | + .its('0.serverTiming.0') |
| 53 | + .then(logTiming) |
| 54 | + |
| 55 | + // we can even assert that the duration is below certain limit |
| 56 | + cy.window().its('performance') |
| 57 | + .invoke('getEntriesByType', 'navigation') |
| 58 | + .its('0.serverTiming') |
| 59 | + .then((timings) => { |
| 60 | + // find the cache timing among all server timings |
| 61 | + return Cypress._.find(timings, { name: 'cache' }) |
| 62 | + }) |
| 63 | + .then((cacheTiming) => { |
| 64 | + expect(cacheTiming).property('duration').to.be.lessThan(2100) |
| 65 | + }) |
| 66 | + }) |
| 67 | +}) |
0 commit comments