Best JavaScript code snippet using wpt
test-transport.js
Source:test-transport.js
...45 setupStubs(true, true);46 sendRequest(win, 'https://example.com/test', {47 beacon: true, xhrpost: true, image: true,48 });49 expectBeacon('https://example.com/test', '');50 expectNoXhr();51 expectNoImagePixel();52 });53 it('prefers xhrpost over image', () => {54 setupStubs(true, true);55 sendRequest(win, 'https://example.com/test', {56 beacon: false, xhrpost: true, image: true,57 });58 expectNoBeacon();59 expectXhr('https://example.com/test', '');60 expectNoImagePixel();61 });62 it('reluctantly uses image if nothing else is enabled', () => {63 setupStubs(true, true);64 sendRequest(win, 'https://example.com/test', {65 image: true,66 });67 expectNoBeacon();68 expectImagePixel('https://example.com/test');69 expectNoXhr();70 });71 it('falls back to image setting suppressWarnings to true', () => {72 setupStubs(true, true);73 sendRequest(win, 'https://example.com/test', {74 beacon: false, xhrpost: false, image: {suppressWarnings: true},75 });76 expectNoBeacon();77 expectNoXhr();78 expectImagePixel('https://example.com/test');79 });80 it('falls back to image setting referrerPolicy', () => {81 setupStubs(true, true);82 sendRequest(win, 'https://example.com/test', {83 beacon: true, xhrpost: true, image: true, referrerPolicy: 'no-referrer',84 });85 expectNoBeacon();86 expectNoXhr();87 expectImagePixel('https://example.com/test', 'no-referrer');88 });89 it('falls back to xhrpost when enabled and beacon is not available', () => {90 setupStubs(false, true);91 sendRequest(win, 'https://example.com/test', {92 beacon: true, xhrpost: true, image: true,93 });94 expectNoBeacon();95 expectXhr('https://example.com/test', '');96 expectNoImagePixel();97 });98 it('falls back to image when beacon not found and xhr disabled', () => {99 setupStubs(false, true);100 sendRequest(win, 'https://example.com/test', {101 beacon: true, xhrpost: false, image: true,102 });103 expectNoBeacon();104 expectNoXhr();105 expectImagePixel('https://example.com/test');106 });107 it('falls back to image when beacon and xhr are not available', () => {108 setupStubs(false, false);109 sendRequest(win, 'https://example.com/test', {110 beacon: true, xhrpost: true, image: true,111 });112 expectNoBeacon();113 expectNoXhr();114 expectImagePixel('https://example.com/test');115 });116 it('does not send a request when no transport methods are enabled', () => {117 setupStubs(true, true);118 sendRequest(win, 'https://example.com/test', {});119 expectNoBeacon();120 expectNoXhr();121 expectNoImagePixel();122 });123 it('does not send a request when URL is empty', () => {124 setupStubs(true, true);125 sendRequest(win, '', {beacon: true, xhrpost: true, image: true});126 expectNoBeacon();127 expectNoXhr();128 expectNoImagePixel();129 });130 it('send single segment request', () => {131 setupStubs(true, true);132 new Transport(win, {beacon: true}).sendRequest('https://e.com/test', [{133 extraUrlParams: {134 a: 1,135 b: 'hello',136 },137 }], false);138 expectBeacon('https://e.com/test?a=1&b=hello', '');139 expectNoXhr();140 expectNoImagePixel();141 });142 it('send single segment request in batch', () => {143 setupStubs(true, true);144 new Transport(win, {beacon: true}).sendRequest('https://e.com/test', [{145 extraUrlParams: {146 a: 1,147 b: 'hello',148 },149 }], true);150 expectBeacon('https://e.com/test?a=1&b=hello', '');151 expectNoXhr();152 expectNoImagePixel();153 });154 it('send single segment request useBody', () => {155 setupStubs(true, true);156 new Transport(win, {beacon: true, useBody: true}).sendRequest('https://e.com/test', [{157 extraUrlParams: {158 a: 1,159 b: 'hello',160 },161 }], false);162 expectBeacon('https://e.com/test', '{"a":1,"b":"hello"}');163 expectNoXhr();164 expectNoImagePixel();165 });166 it('send single segment request useBody in batch', () => {167 setupStubs(true, true);168 new Transport(win, {beacon: true, useBody: true}).sendRequest('https://e.com/test', [{169 extraUrlParams: {170 a: 1,171 b: 'hello',172 },173 }], true);174 expectBeacon('https://e.com/test', '[{"a":1,"b":"hello"}]');175 expectNoXhr();176 expectNoImagePixel();177 });178 it('send multi-segment request w/o batch (only 1st sent)', () => {179 setupStubs(true, true);180 new Transport(win, {beacon: true}).sendRequest('https://e.com/test', [{181 extraUrlParams: {182 a: 1,183 b: 'hello',184 },185 }, {186 extraUrlParams: {187 a: 2,188 b: 'world',189 },190 }], false);191 expectBeacon('https://e.com/test?a=1&b=hello', '');192 expectNoXhr();193 expectNoImagePixel();194 });195 it('send multi-segment request in batch', () => {196 setupStubs(true, true);197 new Transport(win, {beacon: true}).sendRequest('https://e.com/test', [{198 extraUrlParams: {199 a: 1,200 b: 'hello',201 },202 }, {203 extraUrlParams: {204 a: 1,205 b: 'hello',206 },207 }], true);208 expectBeacon('https://e.com/test?a=1&b=hello&a=1&b=hello', '');209 expectNoXhr();210 expectNoImagePixel();211 });212 it('send multi-segment request useBody in batch', () => {213 setupStubs(true, true);214 new Transport(win, {beacon: true, useBody: true}).sendRequest('https://e.com/test', [{215 extraUrlParams: {216 a: 1,217 b: 'hello',218 },219 }, {220 extraUrlParams: {221 a: 1,222 b: 'hello',223 },224 }], true);225 expectBeacon('https://e.com/test', '[{"a":1,"b":"hello"},{"a":1,"b":"hello"}]');226 expectNoXhr();227 expectNoImagePixel();228 });229 it('asserts that urls are https', () => {230 allowConsoleError(() => { expect(() => {231 sendRequest(win, 'http://example.com/test', {image: true});232 }).to.throw(/https/); });233 });234 it('should NOT allow __amp_source_origin', () => {235 allowConsoleError(() => { expect(() => {236 sendRequest(win, 'https://twitter.com?__amp_source_origin=1', {image: true});237 }).to.throw(/Source origin is not allowed in/); });238 });239 describe('sendRequestUsingIframe', () => {240 const url = 'http://iframe.localhost:9876/test/fixtures/served/iframe.html';241 function sendRequestUsingIframe(win, url) {242 new Transport(win).sendRequestUsingIframe(url, {});243 }244 it('should create and delete an iframe', () => {245 const clock = lolex.install({target: win});246 installTimerService(win);247 sendRequestUsingIframe(win, url);248 const iframe = doc.querySelector('iframe[src="' + url + '"]');249 expect(iframe).to.be.ok;250 expect(iframe.getAttribute('sandbox')).to.equal(251 'allow-scripts allow-same-origin');252 return loadPromise(iframe).then(() => {253 clock.tick(4999);254 expect(doc.querySelector('iframe[src="' + url + '"]')).to.be.ok;255 clock.tick(1);256 expect(doc.querySelector('iframe[src="' + url + '"]')).to.not.be.ok;257 });258 });259 it('iframe asserts that urls are https', () => {260 allowConsoleError(() => { expect(() => {261 sendRequestUsingIframe(win, 'http://example.com/test');262 }).to.throw(/https/); });263 });264 it('forbids same origin', () => {265 const fakeWin = {266 location: {267 href: 'https://example.com/abc',268 },269 };270 allowConsoleError(() => {271 expect(() => {272 sendRequestUsingIframe(fakeWin, 'https://example.com/123');273 }).to.throw(/Origin of iframe request/);274 });275 });276 });277 describe('iframe transport', () => {278 it('does not initialize transport iframe if not used', () => {279 const transport = new Transport(win, {280 image: true,281 xhrpost: true,282 beacon: false,283 });284 const ampAnalyticsEl = null;285 const preconnectSpy = sandbox.spy();286 transport.maybeInitIframeTransport(win, ampAnalyticsEl, {287 preload: preconnectSpy,288 });289 expect(transport.iframeTransport_).to.be.null;290 expect(preconnectSpy).to.not.be.called;291 });292 it('initialize iframe transport when used', () => {293 const transport = new Transport(win, {294 iframe: '//test',295 });296 const ad = doc.createElement('amp-ad');297 ad.getResourceId = () => '123';298 doc.body.appendChild(ad);299 const frame = doc.createElement('iframe');300 ad.appendChild(frame);301 frame.contentWindow.document.write(302 '<amp-analytics type="bg"></amp-analytics>');303 frame.contentWindow.__AMP_TOP = win;304 const ampAnalyticsEl =305 frame.contentWindow.document.querySelector('amp-analytics');306 const preconnectSpy = sandbox.spy();307 transport.maybeInitIframeTransport(win, ampAnalyticsEl, {308 preload: preconnectSpy,309 });310 expect(transport.iframeTransport_).to.be.ok;311 expect(preconnectSpy).to.be.called;312 transport.deleteIframeTransport();313 expect(transport.iframeTransport_).to.be.null;314 });315 it('initialize iframe transport when used with inabox', () => {316 win.AMP_MODE = win.AMP_MODE || {};317 win.AMP_MODE.runtime = 'inabox';318 expect(getMode(win).runtime).to.equal('inabox');319 const transport = new Transport(win, {320 iframe: '//test',321 });322 const frame = doc.createElement('iframe');323 doc.body.appendChild(frame);324 frame.contentWindow.document.write(325 '<amp-analytics type="bg"></amp-analytics>');326 frame.contentWindow.__AMP_TOP = win;327 const ampAnalyticsEl =328 frame.contentWindow.document.querySelector('amp-analytics');329 const preconnectSpy = sandbox.spy();330 transport.maybeInitIframeTransport(win, ampAnalyticsEl, {331 preload: preconnectSpy,332 });333 expect(transport.iframeTransport_).to.be.ok;334 expect(preconnectSpy).to.be.called;335 transport.deleteIframeTransport();336 expect(transport.iframeTransport_).to.be.null;337 });338 it('send via iframe transport', () => {339 setupStubs(true, true);340 const transport = new Transport(win, {341 beacon: true, xhrpost: true, image: true,342 iframe: '//test',343 });344 const iframeTransportSendRequestSpy = sandbox.spy();345 transport.iframeTransport_ = {346 sendRequest: iframeTransportSendRequestSpy,347 };348 transport.sendRequest('test test', [{}], false);349 expectNoBeacon();350 expectNoXhr();351 expectNoImagePixel();352 expect(iframeTransportSendRequestSpy).to.be.calledWith('test test');353 });354 });355 function setupStubs(beacon, xhr) {356 const wi = mockWindowInterface(sandbox);357 wi.getSendBeacon.returns(beacon ? sendBeaconStub : undefined);358 const FakeXMLHttpRequest = () => {359 return {360 withCredentials: false,361 open: openXhrStub,362 send: sendXhrStub,363 setRequestHeader: () => {},364 };365 };366 wi.getXMLHttpRequest.returns(xhr ? FakeXMLHttpRequest : undefined);367 sendBeaconStub.returns(beacon);368 imagePixelVerifier = new ImagePixelVerifier(wi);369 }370 function sendRequest(win, request, options) {371 new Transport(win, options).sendRequest(request, [{}], false);372 }373 function expectBeacon(url, payload) {374 expect(sendBeaconStub).to.be.calledWith(url, payload);375 }376 function expectNoBeacon() {377 expect(sendBeaconStub).to.not.be.called;378 }379 function expectXhr(url, payload) {380 expect(openXhrStub).to.be.calledWith('POST', url, true);381 expect(sendXhrStub).to.be.calledWith(payload);382 }383 function expectNoXhr() {384 expect(openXhrStub).to.not.be.called;385 expect(sendXhrStub).to.not.be.called;386 }387 function expectImagePixel(url, referrerPolicy) {...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wpt = require('wpt');10 if (err) {11 console.log(err);12 } else {13 console.log(data);14 }15});16var wpt = require('wpt');17 if (err) {18 console.log(err);19 } else {20 console.log(data);21 }22});23var wpt = require('wpt');24 if (err) {25 console.log(err);26 } else {27 console.log(data);28 }29});30var wpt = require('wpt');31 if (err) {32 console.log(err);33 } else {34 console.log(data);35 }36});37var wpt = require('wpt');38 if (err) {39 console.log(err);40 } else {41 console.log(data);42 }43});44var wpt = require('wpt');
Using AI Code Generation
1const { expectBeacon } = require('wptagent');2expectBeacon('test', 'test');3const { expectNoBeacon } = require('wptagent');4expectNoBeacon('test', 'test');5const { expectNoBeacon } = require('wptagent');6expectNoBeacon('test', 'test');7const { expectBeacon } = require('wptagent');8expectBeacon('test', 'test');9const { expectNoBeacon } = require('wptagent');10expectNoBeacon('test', 'test');11const { expectNoBeacon } = require('wptagent');12expectNoBeacon('test', 'test');13const { expectBeacon } = require('wptagent');14expectBeacon('test', 'test');15const { expectNoBeacon } = require('wptagent');16expectNoBeacon('test', 'test');17const { expectNoBeacon } = require('wptagent');18expectNoBeacon('test', 'test');19const { expectBeacon } = require('wptagent');20expectBeacon('test', 'test');21const { expectNoBeacon } = require('wptagent');22expectNoBeacon('test', 'test');23const { expectNoBeacon } = require('wptagent');24expectNoBeacon('test', 'test');25const { expectBeacon } = require('wptagent');26expectBeacon('test', 'test');
Using AI Code Generation
1var wpt = require('wpt');2 console.log(result);3});4var wpt = require('wpt');5 console.log(result);6});7var wpt = require('wpt');8 console.log(result);9});10var wpt = require('wpt');11 console.log(result);12});13var wpt = require('wpt');14 console.log(result);15});16var wpt = require('wpt');17 console.log(result);18});19var wpt = require('wpt');20 console.log(result);21});22var wpt = require('
Using AI Code Generation
1this.expectBeacon = function(url, params, timeout) {2 var url = url;3 var params = params;4 var timeout = timeout;5 var beaconData = {6 };7 WPT.expectBeacon(beaconData, timeout);8};9this.expectBeacon = function(url, params, timeout) {10 var url = url;11 var params = params;12 var timeout = timeout;13 var beaconData = {14 };15 WPT.expectBeacon(beaconData, timeout);16};17this.expectBeacon = function(url, params, timeout) {18 var url = url;19 var params = params;20 var timeout = timeout;21 var beaconData = {22 };23 WPT.expectBeacon(beaconData, timeout);24};25this.expectBeacon = function(url, params, timeout) {26 var url = url;27 var params = params;28 var timeout = timeout;29 var beaconData = {30 };31 WPT.expectBeacon(beaconData, timeout);32};33this.expectBeacon = function(url, params, timeout) {34 var url = url;35 var params = params;36 var timeout = timeout;37 var beaconData = {38 };39 WPT.expectBeacon(beaconData, timeout);40};41this.expectBeacon = function(url, params, timeout) {42 var url = url;43 var params = params;44 var timeout = timeout;45 var beaconData = {46 };47 WPT.expectBeacon(beaconData, timeout);48};49this.expectBeacon = function(url, params, timeout) {
Using AI Code Generation
1var wptb = require('wptb');2var expect = wptb.expectBeacon;3});4var wptb = require('wptb');5var expect = wptb.expectBeacon;6});7var wptb = require('wptb');8var expect = wptb.expectBeacon;9});10var wptb = require('wptb');11var expect = wptb.expectBeacon;12});13var wptb = require('wptb');14var expect = wptb.expectBeacon;15});16var wptb = require('wptb');17var expect = wptb.expectBeacon;18});19var wptb = require('wptb');20var expect = wptb.expectBeacon;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!!