How to use setOfflineMode method in Puppeteer

Best JavaScript code snippet using puppeteer

backandTest.js

Source:backandTest.js Github

copy

Full Screen

...520 this.timeout(0);521 var response;522 backand.object.getList('offline').then(res => {523 response = res;524 backand.offline.setOfflineMode(true);525 return backand.object.getList('offline');526 }).then(res => {527 expect(res).to.eql(response);528 return backand.object.getList('items');529 }).then(res => {530 expect(res.data).to.eql([]);531 backand.offline.setOfflineMode(false);532 done();533 }).catch(err => {534 done(err);535 });536 });537 it('should cache getOne calls', function (done) {538 this.timeout(0);539 var response;540 backand.object.getOne('offline', 3).then(res => {541 response = res;542 backand.offline.setOfflineMode(true);543 return backand.object.getOne('offline', 3);544 }).then(res => {545 expect(res).to.eql(response);546 backand.offline.setOfflineMode(false);547 done();548 }).catch(err => {549 done(err);550 })551 });552 it('should cache query calls', function (done) {553 this.timeout(0);554 var response;555 var parameters = {param1: 'test'};556 backand.query.post('params', parameters).then(res => {557 response = res;558 backand.offline.setOfflineMode(true);559 return backand.query.post('params', parameters);560 }).then(res => {561 expect(res).to.eql(response);562 backand.offline.setOfflineMode(false);563 done();564 }).catch(err => {565 done(err);566 })567 });568 it('should queue create calls', function (done) {569 this.timeout(0);570 backand.offline.setOfflineMode(true);571 backand.defaults.beforeExecuteOfflineItem = (request) => {572 if (request.data.text !== 'DontCheckMe') return true;573 return false;574 };575 backand.defaults.afterExecuteOfflineItem = (response) => {576 console.log(response);577 if (backand.offline.queue.length === 0) done();578 };579 backand.object.create('offline', {text: 'test'}).then(res => {580 expect(res.status).to.eql(1);581 return backand.object.create('offline', {text: 'DontCheckMe'})582 }).then(res => {583 expect(res.status).to.eql(1);584 return backand.object.create('offline', {text: 'test3'})585 }).then(res => {586 expect(res.status).to.eql(1);587 expect(backand.offline.queue.length).to.eql(3);588 backand.offline.setOfflineMode(false);589 }).catch(err => {590 done(err);591 })592 });593 it('should queue update and remove calls', function (done) {594 this.timeout(0);595 backand.defaults.beforeExecuteOfflineItem = (request) => {596 return true;597 };598 backand.defaults.afterExecuteOfflineItem = (response) => {599 console.log(response);600 if (backand.offline.queue.length === 0) done();601 };602 backand.object.create('offline', {text: 'test'}).then(res => {603 expect(res.status).to.eql(200);604 lastCreatedId = res.data.__metadata.id;605 backand.offline.setOfflineMode(true);606 return backand.object.update('offline', lastCreatedId, {text: 'test1'});607 }).then(res => {608 expect(res.status).to.eql(1);609 return backand.object.update('offline', lastCreatedId, {text: 'test2'});610 }).then(res => {611 expect(res.status).to.eql(1);612 return backand.object.remove('offline', lastCreatedId);613 }).then(res => {614 expect(res.status).to.eql(1);615 expect(backand.offline.queue.length).to.eql(3);616 backand.offline.setOfflineMode(false);617 }).catch(err => {618 done(err);619 })620 });621 });622 describe('backand.invoke', () => {623 it('invoke to get functions', function (done) {624 this.timeout(0);625 var params = {filter: [{fieldName: "actionType", operator: "equals", value: "Function"}], pageSize: 200};626 var json = {627 method: 'GET',628 url: '/1/action/config',629 params: params630 };...

Full Screen

Full Screen

mixpanel-lite-offline-spec.js

Source:mixpanel-lite-offline-spec.js Github

copy

Full Screen

...42 it('should write data to localStorage first', function (done) {43 var now = (new Date()).getTime();44 var token = 'test-token-' + now;45 var eventName = 'test-event-' + now;46 page.setOfflineMode(true).then(function() {47 // execute tracking (pass local vars into dom)48 return page.evaluate(function (t, e) {49 window.mixpanel.init(t);50 window.mixpanel.track(e);51 // return local storage so we can inspect it52 return JSON.parse(localStorage.getItem('mixpanel-lite') || {});53 }, token, eventName);54 })55 .then(function(data) {56 // check we have request info57 expect(data).toBeDefined();58 expect(Array.isArray(data)).toBe(true);59 expect(data.length).toEqual(1);60 expect(data[0].event).toEqual(eventName);61 expect(data[0].properties.token).toEqual(token);62 })63 .catch(done.fail)64 .finally(done);65 });66 it('should send offline events when back online', async function () {67 var numberOfTrackEvents = 5;68 await page.setRequestInterception(true); // allow requests to be intercepted69 await page.setOfflineMode(true); // start in offline mode so tracking is written to local db70 // intercept requests so we can count them71 page.on('request', function(request) {72 var requestUrl = request.url();73 if (requestUrl.startsWith('https://api.mixpanel.com/track')) {74 numberOfTrackEvents--;75 }76 request.continue();77 });78 // fire tracking events79 await page.evaluate(function (num) {80 window.mixpanel.init('token-' + (new Date()).getTime());81 for (var i = 0, l = num; i < l; i++) {82 window.mixpanel.track('event-' + i);83 }84 }, numberOfTrackEvents);85 // get events from storage86 var data = await page.evaluate(function () {87 return JSON.parse(localStorage.getItem('mixpanel-lite') || {});88 });89 // check tracking events were saved to local storage90 expect(data).toBeDefined();91 expect(Array.isArray(data)).toBe(true);92 expect(data.length).toEqual(numberOfTrackEvents);93 // go back online `page.on('request') handler above will execute`94 // once the adequate number of requests have executed, test will complete95 await page.setOfflineMode(false);96 // wait a sec97 await utils.sleep(3000);98 expect(numberOfTrackEvents).toEqual(0);99 return Promise.resolve();100 });101 it('should NOT suppress duplicate events', function (done) {102 var now = (new Date()).getTime();103 var token = 'test-token-' + now;104 var eventName = 'test-event-' + now;105 // go offline106 page.setOfflineMode(true).then(function() {107 // create some tracking events108 return page.evaluate(function (t, e) {109 window.mixpanel.init(t);110 window.mixpanel.track(e);111 window.mixpanel.track(e);112 window.mixpanel.track(e);113 window.mixpanel.track(e);114 }, token, eventName);115 })116 .then(function() {117 // get value of local storage118 return page.evaluate(function () {119 return JSON.parse(localStorage.getItem('mixpanel-lite') || {});120 });121 })122 .then(function(data) {123 // check the tracking data was saved to local storage124 expect(data).toBeDefined();125 expect(Array.isArray(data)).toBe(true);126 expect(data.length).toEqual(4);127 })128 .catch(done.fail)129 .finally(done);130 });131 it('should drop first event when pending transactions exceed 100', function (done) {132 var maxEvents = 100;133 // go offline134 page.setOfflineMode(true).then(function() {135 return page.evaluate(function (max) {136 window.mixpanel.init('test-token');137 // create some tracking events138 for (var i = 0, l = max + 50; i < l; i++) {139 window.mixpanel.track('track-' + i);140 }141 }, maxEvents);142 })143 .then(function() {144 // get value of local storage145 return page.evaluate(function () {146 return JSON.parse(localStorage.getItem('mixpanel-lite') || {});147 });148 })149 .then(function(data) {150 // check the tracking data was saved to local storage151 expect(data).toBeDefined();152 expect(Array.isArray(data)).toBe(true);153 // check the event limit was enforced154 expect(data.length).toEqual(maxEvents);155 // check first event is now 50 (0-49 dropped)156 expect(data.shift().event).toBe('track-50');157 // check last event is the most recent (fifo)158 expect(data.pop().event).toBe('track-149');159 })160 .catch(done.fail)161 .finally(done);162 });163 it('should store correct number of events in order', function(done) {164 var maxEvents = utils.randomInteger(19, 99);165 var eventsToSend = [];166 // create some tracking events167 for (var i = 0; i < maxEvents; i++) {168 eventsToSend.push({169 eventName: 'tracking-event-' + i,170 data: {171 index: i172 }173 });174 }175 // go offline176 page.setOfflineMode(true).then(function() {177 // init178 return page.evaluate(function () {179 window.mixpanel.init('test-token');180 });181 })182 .then(function() {183 // send events184 return page.evaluate(function (events) {185 for (var ii = 0, ll = events.length; ii < ll; ii++) {186 window.mixpanel.track(events[ii].eventName, events[ii].data);187 }188 }, eventsToSend);189 })190 .then(function() {...

Full Screen

Full Screen

app.js

Source:app.js Github

copy

Full Screen

...108 })109}110// check for online status111if (navigator.onLine) {112 setOfflineMode("off");113} else {114 setOfflineMode("on", "offline");115}116window.addEventListener('online', function () {117 setOfflineMode("off");118});119window.addEventListener('offline', function () {120 setOfflineMode("on", "offline");121});122// set up the Slick carousel123$(document).ready(function () {124 restoreCards();125 // restore screensaver126 const screensaverPoster = localStorage.getItem('screensaverPoster');127 const screensaverSrc = localStorage.getItem('screensaverSrc');128 if (screensaverPoster) {129 $('.screensaver video').attr('poster', screensaverPoster);130 }131 $('.screensaver video').attr('src', screensaverSrc);132 $(`input.screensaver-select[data-src='${screensaverSrc}']`).prop('checked', true);133 $(".available-cards input").on("change", function() {134 let cards = getCheckedCards();135 displayCards(cards);136 saveCards(cards);137 })138 slickInit();139 $('input.carouselTiming').on("change", function() {140 localStorage.setItem("slickAutoplaySpeed", $(this).val());141 $("button.browserRefresh").show();142 })143 $("button.browserRefresh").on("click", function() {144 window.location.reload(true);145 })146 $('.carousel').on('beforeChange', function() {147 slickFadeout($('.slick-center').next());148 })149 $(document).on('open.zf.reveal', function () {150 $('.carousel').slick('slickPause');151 }).on('closed.zf.reveal', function () {152 $('.carousel').slick('slickPlay');153 $('video').trigger('pause');154 })155 $('#toggleFullScreen').on('click', function () {156 if ($(this).is(":checked")) {157 openFullscreen();158 } else {159 closeFullscreen();160 }161 })162 $('#toggleOfflineMode').on('click', function () {163 if (!navigator.onLine) { return }164 if ($("body").hasClass("offline")) {165 setOfflineMode("off");166 } else {167 setOfflineMode("on");168 }169 })170 $('#toggleScreensaver').on('click', function () {171 if ($("body").hasClass("screensaver-on")) {172 $("body").removeClass("screensaver-on");173 $('.carousel').slick('slickPlay');174 } else {175 $("body").addClass("screensaver-on");176 $('.carousel').slick('slickPause');177 }178 })179 $('input.screensaver-select').on('click', function () {180 const src = $(this).data('src');181 const poster = $(this).data('poster');...

Full Screen

Full Screen

offline-test.js

Source:offline-test.js Github

copy

Full Screen

...8describe('when offline', function() {9 it('the app loads on the index route', async function() {10 await visit('/', async (page) => {11 await waitForAllServiceWorkers(page);12 await page.setOfflineMode(true);13 await page.reload({ waitUntil: 'networkidle0' });14 let element = await page.waitForSelector('[data-test-breethe]');15 expect(element).to.be.ok;16 });17 });18 it('the app loads on the search route', async function() {19 await visit('/search/Salzburg', async (page) => {20 await waitForAllServiceWorkers(page);21 await page.setOfflineMode(true);22 await page.reload({ waitUntil: 'networkidle0' });23 let element = await page.waitForSelector('[data-test-breethe]');24 expect(element).to.be.ok;25 });26 });27 it('the app loads on the location route', async function() { 28 await visit('/location/2', async (page) => {29 await waitForAllServiceWorkers(page);30 await page.setOfflineMode(true);31 await page.reload();32 let element = await page.waitForSelector('[data-test-measurement="PM10"] [data-test-measurement-value="15"]');33 expect(element).to.be.ok;34 });35 });36 it('disables the location search field', async function() {37 await visit('/', async (page) => {38 await page.setOfflineMode(true);39 let element = await page.waitForSelector('[data-test-search-input]:disabled');40 expect(element).to.be.ok;41 });42 });43 it('disables the location search button', async function() {44 await visit('/', async (page) => {45 await page.setOfflineMode(true);46 let element = await page.waitForSelector('[data-test-search-submit]:disabled');47 expect(element).to.be.ok;48 });49 });50 it('shows an offline warning on the index rotue', async function() {51 await visit('/', async (page) => {52 await page.setOfflineMode(true);53 let element = await page.waitForSelector('[data-test-offline-warning]');54 expect(element).to.be.ok;55 });56 });57 it('shows an offline warning on the search rotue', async function() {58 await visit('/search/Salzburg', async (page) => {59 await page.setOfflineMode(true);60 let element = await page.waitForSelector('[data-test-offline-warning]');61 expect(element).to.be.ok;62 });63 });64 it('does not show an offline warning on the location rotue', async function() {65 await visit('/location/2', async (page) => {66 await page.setOfflineMode(true);67 await page.waitFor(100);68 let element = await page.$('[data-test-offline-warning]');69 expect(element).to.be.null;70 });71 });72 it('the main user flow works', async function() {73 await visit('/', async (page) => {74 // go through the flow online first so we populate IndexedDB75 await page.type('[data-test-search-input]', 'Salzburg');76 await page.click('[data-test-search-submit]');77 await page.waitForSelector('[data-test-search-result="Salzburg"]');78 await page.click('[data-test-search-result="Salzburg"] a');79 await page.waitForSelector('[data-test-measurement="PM10"] [data-test-measurement-value="15"]');80 await page.click('[data-test-home-link]');81 await page.waitForSelector('[data-test-search]');82 await page.setOfflineMode(true);83 // click the recent location84 await page.waitForSelector('[data-test-search-result="Salzburg"]');85 await page.click('[data-test-search-result="Salzburg"] a');86 // check the correct data is still present87 let element = await page.waitForSelector('[data-test-measurement="PM10"] [data-test-measurement-value="15"]');88 expect(page.url()).to.match(/\/location\/2$/);89 expect(element).to.be.ok;90 });91 });92 describe('when coming back online', function() {93 it('enables the location search field', async function() {94 await visit('/', async (page) => {95 await page.setOfflineMode(true); // go offline96 await page.waitForSelector('[data-test-offline-warning]');97 await page.setOfflineMode(false); // …and back online98 await page.waitFor(100);99 let element = await page.waitForSelector('[data-test-search-input]:enabled');100 expect(element).to.be.ok;101 });102 });103 it('enables the location search button', async function() {104 await visit('/', async (page) => {105 await page.setOfflineMode(true); // go offline106 await page.waitForSelector('[data-test-offline-warning]');107 await page.setOfflineMode(false); // …and back online108 await page.waitFor(100);109 let element = await page.waitForSelector('[data-test-search-submit]:enabled');110 expect(element).to.be.ok;111 });112 });113 it('hides the offline warning', async function() {114 await visit('/', async (page) => {115 await page.setOfflineMode(true); // go offline116 await page.waitForSelector('[data-test-offline-warning]');117 await page.setOfflineMode(false); // …and back online118 await page.waitFor(10);119 let element = await page.$('[data-test-offline-warning]');120 expect(element).to.be.null;121 });122 });123 });...

Full Screen

Full Screen

propertyList.js

Source:propertyList.js Github

copy

Full Screen

...60// window.location.href = 'index.html';61// }62// });63 };64// function setOfflineMode() {65// var r = confirm("Are you sure you want to set the Offline Mode?");66// if (r == true) {67// vm.showSettingOffline = true;68// localStorage.setItem('offlineMode', 'true');69// 70// loadResource.getProperties(vm.obj, function (successResult) {71// var result = JSON.stringify(successResult.PropertiesList);72// localStorage.setItem('propertiesList', result);73// vm.showSettingOffline = false;74// }, function (errorResult) {75// vm.errorResult = errorResult.data.message;76// vm.showSettingOffline = false;77// });78// }...

Full Screen

Full Screen

jestTest.js

Source:jestTest.js Github

copy

Full Screen

...11});12// ACTION TESTS13describe('Redux Actions - Unit Tests', () => {14 it('snapshot redux action test', () => {15 expect(setOfflineMode(true)).toMatchSnapshot();16 expect(setOfflineMode(false)).toMatchSnapshot();17 });18 it('snapshot redux action thunk test', () => {19 expect(syncApp()).toMatchSnapshot(); // thunked action tests don't work currently - no useful output20 });21});22// REDUCER & STATE TESTS23describe('Redux Reducers - Unit Tests', () => {24 it('snapshot redux reducer initial state test', () => {25 expect(appReducer(undefined, { type: 'NONE' })).toMatchSnapshot();26 });27 it('snapshot redux reducer action test', () => {28 expect(appReducer(undefined, setAppSynced(true))).toMatchSnapshot();29 });30 it('snapshot redux reducer multiple action test', () => {31 let state = appReducer(undefined, setAppSynced(true));32 expect(state).toMatchSnapshot();33 state = appReducer(state, setOfflineMode(true));34 expect(state).toMatchSnapshot();35 });...

Full Screen

Full Screen

endpointStatusInterceptor.js

Source:endpointStatusInterceptor.js Github

copy

Full Screen

...17 }18 function responseInterceptor(response) {19 var url = response.config.url;20 if (response.status === 200 && canBeOffline(url) && EndpointProvider.offlineMode()) {21 EndpointProvider.setOfflineMode(false);22 }23 return response || $q.when(response);24 }25 function responseErrorInterceptor(rejection) {26 var url = rejection.config.url;27 if ((rejection.status === 502 || rejection.status === 503 || rejection.status === -1) && canBeOffline(url) && !EndpointProvider.offlineMode()) {28 EndpointProvider.setOfflineMode(true);29 }30 return $q.reject(rejection);31 }32 return interceptor;...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1// index.js (ConnectorWidget)2// Binds Redux state and actions to component's props3import Component from "./component";4import { bindActionCreators } from "redux";5import { connect } from "react-redux";6import {7 setAvailableMuses,8 getMuses,9 setConnectedMuseInfo,10 setConnectionStatus,11 setOfflineMode12} from "../../redux/actions";13// Import properties stored in Redux14function mapStateToProps(state) {15 return {16 availableMuses: state.availableMuses,17 museInfo: state.museInfo,18 connectionStatus: state.connectionStatus,19 };20}21// Binds actions to component's props22function mapDispatchToProps(dispatch) {23 return bindActionCreators(24 {25 setAvailableMuses,26 getMuses,27 setConnectedMuseInfo,28 setConnectionStatus,29 setOfflineMode,30 },31 dispatch32 );33}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch({4 });5 const page = await browser.newPage();6 await page.setOfflineMode(true);7 await page.screenshot({path: 'google.png'});8 await browser.close();9})();10const puppeteer = require('puppeteer');11(async () => {12 const browser = await puppeteer.launch({13 });14 const page = await browser.newPage();15 await page.setOfflineMode(true);16 await page.screenshot({path: 'google.png'});17 await browser.close();18})();19const puppeteer = require('puppeteer');20(async () => {21 const browser = await puppeteer.launch({22 });23 const page = await browser.newPage();24 await page.setOfflineMode(true);25 await page.screenshot({path: 'google.png'});26 await browser.close();27})();28const puppeteer = require('puppeteer');29(async () => {30 const browser = await puppeteer.launch({31 });32 const page = await browser.newPage();33 await page.setOfflineMode(true);34 await page.screenshot({path: 'google.png'});35 await browser.close();36})();37const puppeteer = require('puppeteer');38(async () => {39 const browser = await puppeteer.launch({40 });41 const page = await browser.newPage();42 await page.setOfflineMode(true);43 await page.screenshot({path: 'google.png'});44 await browser.close();45})();46const puppeteer = require('puppeteer');47(async () => {48 const browser = await puppeteer.launch({

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2(async () => {3 const browser = await puppeteer.launch();4 const page = await browser.newPage();5 await page.setOfflineMode(true);6 await page.screenshot({path: 'google.png'});7 await browser.close();8})();9const puppeteer = require('puppeteer');10(async () => {11 const browser = await puppeteer.launch();12 const page = await browser.newPage();13 await page.setOfflineMode(false);14 await page.screenshot({path: 'google.png'});15 await browser.close();16})();17const puppeteer = require('puppeteer');18(async () => {19 const browser = await puppeteer.launch();20 const page = await browser.newPage();21 await page.setRequestInterception(true);22 page.on('request', interceptedRequest => {23 if (interceptedRequest.url().endsWith('.png') ||24 interceptedRequest.url().endsWith('.jpg'))25 interceptedRequest.abort();26 interceptedRequest.continue();27 });28 await browser.close();29})();30const puppeteer = require('puppeteer');31(async () => {32 const browser = await puppeteer.launch();33 const page = await browser.newPage();34 await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36');35 await browser.close();36})();37const puppeteer = require('puppeteer');38(async () => {39 const browser = await puppeteer.launch();40 const page = await browser.newPage();41 await page.setViewport({width: 1280, height: 800});

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2const fs = require('fs');3(async () => {4 const browser = await puppeteer.launch({headless: false});5 const page = await browser.newPage();6 await page.setOfflineMode(true);7 await page.screenshot({path: 'example.png'});8 await browser.close();9})();10const puppeteer = require('puppeteer');11(async () => {12 const browser = await puppeteer.launch({headless: false});13 const page = await browser.newPage();14 await page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36');15 await page.screenshot({path: 'example.png'});16 await browser.close();17})();18const puppeteer = require('puppeteer');19(async () => {20 const browser = await puppeteer.launch({headless: false});21 const page = await browser.newPage();22 await page.setViewport({width: 1280, height: 800});23 await page.screenshot({path: 'example.png'});24 await browser.close();25})();26const puppeteer = require('puppeteer');27(async () => {28 const browser = await puppeteer.launch({headless: false});29 const page = await browser.newPage();30 await page.setViewport({width: 1280, height: 800});31 await page.screenshot({path: 'example.png'});32 await browser.close();33})();34const puppeteer = require('puppeteer');35(async () => {36 const browser = await puppeteer.launch({headless: false});37 const page = await browser.newPage();38 await page.tap('#lst-ib');39 await page.type('#lst-ib', 'Hello World!');

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful