|
| 1 | +// https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/server-communication__offline/cypress/integration/offline-spec.js |
| 2 | + |
| 3 | +const goOffline = () => { |
| 4 | + cy.log('**go offline**') |
| 5 | + .then(() => { |
| 6 | + return Cypress.automation('remote:debugger:protocol', { |
| 7 | + command: 'Network.enable', |
| 8 | + }); |
| 9 | + }) |
| 10 | + .then(() => { |
| 11 | + return Cypress.automation('remote:debugger:protocol', { |
| 12 | + command: 'Network.emulateNetworkConditions', |
| 13 | + params: { |
| 14 | + offline: true, |
| 15 | + latency: -1, |
| 16 | + downloadThroughput: -1, |
| 17 | + uploadThroughput: -1, |
| 18 | + }, |
| 19 | + }); |
| 20 | + }); |
| 21 | +}; |
| 22 | + |
| 23 | +const goOnline = () => { |
| 24 | + // disable offline mode, otherwise we will break our tests :) |
| 25 | + cy.log('**go online**') |
| 26 | + .then(() => { |
| 27 | + // https://chromedevtools.github.io/devtools-protocol/1-3/Network/#method-emulateNetworkConditions |
| 28 | + return Cypress.automation('remote:debugger:protocol', { |
| 29 | + command: 'Network.emulateNetworkConditions', |
| 30 | + params: { |
| 31 | + offline: false, |
| 32 | + latency: -1, |
| 33 | + downloadThroughput: -1, |
| 34 | + uploadThroughput: -1, |
| 35 | + }, |
| 36 | + }); |
| 37 | + }) |
| 38 | + .then(() => { |
| 39 | + return Cypress.automation('remote:debugger:protocol', { |
| 40 | + command: 'Network.disable', |
| 41 | + }); |
| 42 | + }); |
| 43 | +}; |
| 44 | + |
| 45 | +describe('offline', () => { |
| 46 | + describe('site', { browser: '!firefox' }, () => { |
| 47 | + // make sure we get back online, even if a test fails |
| 48 | + // otherwise the Cypress can lose the browser connection |
| 49 | + beforeEach(goOnline); |
| 50 | + afterEach(goOnline); |
| 51 | + |
| 52 | + it('shows /migrate/ page', () => { |
| 53 | + const url = '/migrate/'; |
| 54 | + const text = 'Migrate'; |
| 55 | + |
| 56 | + cy.visit(url); |
| 57 | + cy.get('h1').contains(text); |
| 58 | + |
| 59 | + // service worker needs some time to cache |
| 60 | + cy.wait(200); |
| 61 | + |
| 62 | + // has a bug https://github.com/cypress-io/cypress/issues/4742 |
| 63 | + cy.window().then((win) => { |
| 64 | + win.caches.keys().then((cacheNames) => { |
| 65 | + const cacheName = 'workbox-precache-v2-http://localhost:4200/'; |
| 66 | + expect(cacheName).to.be.oneOf(cacheNames); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + goOffline(); |
| 71 | + |
| 72 | + cy.visit(url); |
| 73 | + cy.get('h1').contains(text); |
| 74 | + |
| 75 | + // click `guides` link |
| 76 | + cy.get('a[title="guides"]').click(); |
| 77 | + cy.get('h1').contains('Guides'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('open print dialog when accessing /printable url', () => { |
| 81 | + const url = '/migrate/printable'; |
| 82 | + cy.visit(url, { |
| 83 | + onBeforeLoad: (win) => { |
| 84 | + cy.stub(win, 'print'); |
| 85 | + }, |
| 86 | + }); |
| 87 | + cy.window().then((win) => { |
| 88 | + expect(win.print).to.be.calledOnce; |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments