Best JavaScript code snippet using wpt
keepalive.spec.js
Source:keepalive.spec.js
1'use strict';2describe('ngIdle', function() {3 // helpers4 beforeEach(function() {5 this.addMatchers({6 toEqualData: function(expected) {7 return angular.equals(this.actual, expected);8 }9 });10 });11 describe('keepalive', function() {12 var KeepaliveProvider, $rootScope, $log, $httpBackend, $interval, $http, $injector;13 beforeEach(module('ngIdle.keepalive'));14 beforeEach(function() {15 angular16 .module('app', [])17 .config(['KeepaliveProvider',18 function(_KeepaliveProvider_) {19 KeepaliveProvider = _KeepaliveProvider_;20 }21 ]);22 module('app');23 inject(function(_$rootScope_, _$log_, _$httpBackend_, _$interval_, _$http_, _$injector_) {24 $rootScope = _$rootScope_;25 $log = _$log_;26 $httpBackend = _$httpBackend_;27 $interval = _$interval_;28 $http = _$http_;29 $injector = _$injector_;30 });31 });32 var create = function(http) {33 if (http) KeepaliveProvider.http(http);34 return $injector.invoke(KeepaliveProvider.$get, null, {35 $rootScope: $rootScope,36 $log: $log,37 $interval: $interval,38 $http: $http39 });40 };41 describe('KeepaliveProvider', function() {42 it('http() should update options with simple GET', function() {43 KeepaliveProvider.http('/path/to/keepalive');44 expect(create()._options().http).toEqualData({45 url: '/path/to/keepalive',46 method: 'GET',47 cache: false48 });49 });50 it('http() should update options with http options object', function() {51 KeepaliveProvider.http({52 url: '/path/to/keepalive',53 method: 'POST',54 cache: true55 });56 expect(create()._options().http).toEqualData({57 url: '/path/to/keepalive',58 method: 'POST',59 cache: false60 });61 });62 it('http() should throw if passed null or undefined argument', function() {63 expect(function() {64 KeepaliveProvider.http();65 }).toThrow(new Error('Argument must be a string containing a URL, or an object containing the HTTP request configuration.'));66 });67 it('interval() should update options', function() {68 KeepaliveProvider.interval(10);69 expect(create()._options().interval).toBe(10);70 });71 it('interval() should throw if nan or less than or equal to 0', function() {72 expect(function() {73 KeepaliveProvider.interval('asdsad');74 }).toThrow(new Error('Interval must be expressed in seconds and be greater than 0.'));75 expect(function() {76 KeepaliveProvider.interval(0);77 }).toThrow(new Error('Interval must be expressed in seconds and be greater than 0.'));78 expect(function() {79 KeepaliveProvider.interval(-1);80 }).toThrow(new Error('Interval must be expressed in seconds and be greater than 0.'));81 });82 });83 describe('Keepalive', function() {84 var Keepalive, DEFAULTKEEPALIVEINTERVAL = 10 * 60 * 1000;85 beforeEach(function() {86 Keepalive = create();87 });88 afterEach(function() {89 $httpBackend.verifyNoOutstandingExpectation();90 $httpBackend.verifyNoOutstandingRequest();91 });92 it('setInterval should update an interval option', function() {93 Keepalive.setInterval(100);94 expect(create()._options().interval).toBe(100);95 });96 it('start() after a new LONGER timeout should NOT broadcast Keepalive when the default timeout expires', function() {97 spyOn($rootScope, '$broadcast');98 Keepalive.setInterval(100 * 60);99 Keepalive.start();100 $interval.flush(DEFAULTKEEPALIVEINTERVAL);101 expect($rootScope.$broadcast).not.toHaveBeenCalledWith('Keepalive');102 });103 it('start() after a new LONGER timeout should broadcast Keepalive when the new LONGER expires', function() {104 spyOn($rootScope, '$broadcast');105 Keepalive.setInterval(100);106 Keepalive.start();107 $interval.flush(100 * 1000);108 expect($rootScope.$broadcast).toHaveBeenCalledWith('Keepalive');109 });110 it('start() should schedule ping timeout that broadcasts Keepalive event when it expires.', function() {111 spyOn($rootScope, '$broadcast');112 Keepalive.start();113 $interval.flush(DEFAULTKEEPALIVEINTERVAL);114 expect($rootScope.$broadcast).toHaveBeenCalledWith('Keepalive');115 });116 it('stop() should cancel ping timeout.', function() {117 spyOn($rootScope, '$broadcast');118 Keepalive.start();119 Keepalive.stop();120 $interval.flush(DEFAULTKEEPALIVEINTERVAL);121 expect($rootScope.$broadcast).not.toHaveBeenCalledWith('Keepalive');122 });123 it('ping() should immediately broadcast Keepalive event', function() {124 spyOn($rootScope, '$broadcast');125 Keepalive.ping();126 $interval.flush(DEFAULTKEEPALIVEINTERVAL);127 expect($rootScope.$broadcast).toHaveBeenCalledWith('Keepalive');128 });129 it('should invoke a URL when pinged and broadcast KeepaliveResponse on success.', function() {130 spyOn($rootScope, '$broadcast');131 Keepalive = create('/path/to/keepalive');132 Keepalive.start();133 $httpBackend.expectGET('/path/to/keepalive')134 .respond(200);135 $interval.flush(DEFAULTKEEPALIVEINTERVAL);136 $httpBackend.flush();137 expect($rootScope.$broadcast).toHaveBeenCalledWith('KeepaliveResponse', undefined, 200);138 });139 it('should invoke a URL when pinged and broadcast KeepaliveResponse on error.', function() {140 spyOn($rootScope, '$broadcast');141 Keepalive = create('/path/to/keepalive');142 Keepalive.start();143 $httpBackend.expectGET('/path/to/keepalive')144 .respond(404);145 $interval.flush(DEFAULTKEEPALIVEINTERVAL);146 $httpBackend.flush();147 expect($rootScope.$broadcast).toHaveBeenCalledWith('KeepaliveResponse', undefined, 404);148 });149 });150 });...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3}, function(err, data) {4 if (err) return console.error(err);5 console.log('Test Created: %s', data.data.testId);6 console.log('View Test: %s', data.data.userUrl);7 console.log('View Raw Results: %s', data.data.jsonUrl);8});9### WebPageTest([host], [key])10### .runTest(url, [options], callback)
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3var options = {4};5client.runTest(url, options, function(err, data) {6 if (err) return console.error(err);7 console.log('Test ID: %s', data.data.testId);8 client.keepAlive(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log('Keep Alive: %s', data.data);11 });12});13var wpt = require('webpagetest');14var client = wpt('www.webpagetest.org');15client.getLocations(function(err, data) {16 if (err) return console.error(err);17 console.log('Locations: %j', data);18});19var wpt = require('webpagetest');20var client = wpt('www.webpagetest.org');21client.getTesters(function(err, data) {22 if (err) return console.error(err);23 console.log('Testers: %j', data);24});25var wpt = require('webpagetest');26var client = wpt('www.webpagetest.org');27var options = {28};29 if (err) return console.error(err);30 console.log('Test ID: %s', data.data.testId);31 client.getTestStatus(data.data.testId, function(err, data) {32 if (err) return console.error(err);33 console.log('Test Status: %j', data);34 });35});
Using AI Code Generation
1var wpt = require('wpt-api');2var wptOptions = {3 videoParams: {4 }5};6wpt.runTest(wptOptions, function(err, data) {7 if (err) {8 console.log(err);9 } else {10 var testId = data.data.testId;11 console.log('Test ID: ' + testId);12 wpt.keepAlive(testId, function(err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log('Keep alive successful!');17 }18 });19 }20});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.8d9c9b6e1d6ba1b6e8f6d8f6d8f6d8f6');3wpt.runTest('www.webpagetest.org', function(err, data) {4 if (err) return console.log(err);5 var testId = data.data.testId;6 console.log('Test ID: ' + testId);7 wpt.keepTestAlive(testId, 60, function(err, data) {8 if (err) return console.log(err);9 console.log('Test kept alive for 60 seconds');10 wpt.stopTest(testId, function(err, data) {11 if (err) return console.log(err);12 console.log('Test stopped');13 });14 });15});
Using AI Code Generation
1var WebPageTest = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.8e8a2b2d3b3c9c9f3d8b3c1d1d1d1d1d1');3 if (err) return console.error(err);4 console.log('Test submitted. Polling for results...');5 wpt.getTestStatus(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log('Test completed. View your test at:');8 console.log(data.data.userUrl);9 });10});
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org', 'A.9c9e7f7a0e3a7e8b1f1a7a1b3f2d3f8c');3 videoParams: {4 },5 timelineParams: {6 },7 captureVideoParams: {8 }9 }, function(err, data) {10 if (err) return console.error(err);11 console.log('Test Status: ' + data.statusText);12 console.log('Test ID: ' + data.data.testId);13 console.log('Test URL: ' + data.data.userUrl);14 console.log('Test Results: ' + data.data.summary);15 console.log('Test Location: ' + data.data.location);16 console.log('Test from: ' + data.data.from);17 console.log('Test Runs: ' + data.data.runs);18 console.log('Test Completed: ' + data.data.completed);19 console.log('Test First View: ' + data.data.average.firstView);20 console.log('Test Repeat View: ' + data.data.average.repeatView);21 });
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!