Best JavaScript code snippet using wpt
close.js
Source:close.js
1'use strict';2if (self.importScripts) {3 self.importScripts('/resources/testharness.js');4 self.importScripts('../resources/test-utils.js');5 self.importScripts('../resources/recording-streams.js');6}7const error1 = new Error('error1');8error1.name = 'error1';9const error2 = new Error('error2');10error2.name = 'error2';11promise_test(() => {12 const ws = new WritableStream({13 close() {14 return 'Hello';15 }16 });17 const writer = ws.getWriter();18 const closePromise = writer.close();19 return closePromise.then(value => assert_equals(value, undefined, 'fulfillment value must be undefined'));20}, 'fulfillment value of ws.close() call must be undefined even if the underlying sink returns a non-undefined ' +21 'value');22promise_test(() => {23 let controller;24 let resolveClose;25 const ws = new WritableStream({26 start(c) {27 controller = c;28 },29 close() {30 return new Promise(resolve => {31 resolveClose = resolve;32 });33 }34 });35 const writer = ws.getWriter();36 const closePromise = writer.close();37 return flushAsyncEvents().then(() => {38 controller.error(error1);39 return flushAsyncEvents();40 }).then(() => {41 resolveClose();42 return Promise.all([43 closePromise,44 writer.closed,45 flushAsyncEvents().then(() => writer.closed)]);46 });47}, 'when sink calls error asynchronously while sink close is in-flight, the stream should not become errored');48promise_test(() => {49 let controller;50 const passedError = new Error('error me');51 const ws = new WritableStream({52 start(c) {53 controller = c;54 },55 close() {56 controller.error(passedError);57 }58 });59 const writer = ws.getWriter();60 return writer.close().then(() => writer.closed);61}, 'when sink calls error synchronously while closing, the stream should not become errored');62promise_test(t => {63 const ws = new WritableStream({64 close() {65 throw error1;66 }67 });68 const writer = ws.getWriter();69 return Promise.all([70 writer.write('y'),71 promise_rejects(t, error1, writer.close(), 'close() must reject with the error'),72 promise_rejects(t, error1, writer.closed, 'closed must reject with the error')73 ]);74}, 'when the sink throws during close, and the close is requested while a write is still in-flight, the stream should ' +75 'become errored during the close');76promise_test(() => {77 const ws = new WritableStream({78 write(chunk, controller) {79 controller.error(error1);80 return new Promise(() => {});81 }82 });83 const writer = ws.getWriter();84 writer.write('a');85 return delay(0).then(() => {86 writer.releaseLock();87 });88}, 'releaseLock on a stream with a pending write in which the stream has been errored');89promise_test(() => {90 let controller;91 const ws = new WritableStream({92 start(c) {93 controller = c;94 },95 close() {96 controller.error(error1);97 return new Promise(() => {});98 }99 });100 const writer = ws.getWriter();101 writer.close();102 return delay(0).then(() => {103 writer.releaseLock();104 });105}, 'releaseLock on a stream with a pending close in which controller.error() was called');106promise_test(() => {107 const ws = recordingWritableStream();108 const writer = ws.getWriter();109 return writer.ready.then(() => {110 assert_equals(writer.desiredSize, 1, 'desiredSize should be 1');111 writer.close();112 assert_equals(writer.desiredSize, 1, 'desiredSize should be still 1');113 return writer.ready.then(v => {114 assert_equals(v, undefined, 'ready promise should be fulfilled with undefined');115 assert_array_equals(ws.events, ['close'], 'write and abort should not be called');116 });117 });118}, 'when close is called on a WritableStream in writable state, ready should return a fulfilled promise');119promise_test(() => {120 const ws = recordingWritableStream({121 write() {122 return new Promise(() => {});123 }124 });125 const writer = ws.getWriter();126 return writer.ready.then(() => {127 writer.write('a');128 assert_equals(writer.desiredSize, 0, 'desiredSize should be 0');129 let calledClose = false;130 return Promise.all([131 writer.ready.then(v => {132 assert_equals(v, undefined, 'ready promise should be fulfilled with undefined');133 assert_true(calledClose, 'ready should not be fulfilled before writer.close() is called');134 assert_array_equals(ws.events, ['write', 'a'], 'sink abort() should not be called');135 }),136 flushAsyncEvents().then(() => {137 writer.close();138 calledClose = true;139 })140 ]);141 });142}, 'when close is called on a WritableStream in waiting state, ready promise should be fulfilled');143promise_test(() => {144 let asyncCloseFinished = false;145 const ws = recordingWritableStream({146 close() {147 return flushAsyncEvents().then(() => {148 asyncCloseFinished = true;149 });150 }151 });152 const writer = ws.getWriter();153 return writer.ready.then(() => {154 writer.write('a');155 writer.close();156 return writer.ready.then(v => {157 assert_false(asyncCloseFinished, 'ready promise should be fulfilled before async close completes');158 assert_equals(v, undefined, 'ready promise should be fulfilled with undefined');159 assert_array_equals(ws.events, ['write', 'a', 'close'], 'sink abort() should not be called');160 });161 });162}, 'when close is called on a WritableStream in waiting state, ready should be fulfilled immediately even if close ' +163 'takes a long time');164promise_test(t => {165 const rejection = { name: 'letter' };166 const ws = new WritableStream({167 close() {168 return {169 then(onFulfilled, onRejected) { onRejected(rejection); }170 };171 }172 });173 return promise_rejects(t, rejection, ws.getWriter().close(), 'close() should return a rejection');174}, 'returning a thenable from close() should work');175promise_test(t => {176 const ws = new WritableStream();177 const writer = ws.getWriter();178 return writer.ready.then(() => {179 const closePromise = writer.close();180 const closedPromise = writer.closed;181 writer.releaseLock();182 return Promise.all([183 closePromise,184 promise_rejects(t, new TypeError(), closedPromise, '.closed promise should be rejected')185 ]);186 });187}, 'releaseLock() should not change the result of sync close()');188promise_test(t => {189 const ws = new WritableStream({190 close() {191 return flushAsyncEvents();192 }193 });194 const writer = ws.getWriter();195 return writer.ready.then(() => {196 const closePromise = writer.close();197 const closedPromise = writer.closed;198 writer.releaseLock();199 return Promise.all([200 closePromise,201 promise_rejects(t, new TypeError(), closedPromise, '.closed promise should be rejected')202 ]);203 });204}, 'releaseLock() should not change the result of async close()');205promise_test(() => {206 let resolveClose;207 const ws = new WritableStream({208 close() {209 const promise = new Promise(resolve => {210 resolveClose = resolve;211 });212 return promise;213 }214 });215 const writer = ws.getWriter();216 const closePromise = writer.close();217 writer.releaseLock();218 return delay(0).then(() => {219 resolveClose();220 return closePromise.then(() => {221 assert_equals(ws.getWriter().desiredSize, 0, 'desiredSize should be 0');222 });223 });224}, 'close() should set state to CLOSED even if writer has detached');225promise_test(() => {226 let resolveClose;227 const ws = new WritableStream({228 close() {229 const promise = new Promise(resolve => {230 resolveClose = resolve;231 });232 return promise;233 }234 });235 const writer = ws.getWriter();236 writer.close();237 writer.releaseLock();238 return delay(0).then(() => {239 const abortingWriter = ws.getWriter();240 const abortPromise = abortingWriter.abort();241 abortingWriter.releaseLock();242 resolveClose();243 return abortPromise;244 });245}, 'the promise returned by async abort during close should resolve');246// Though the order in which the promises are fulfilled or rejected is arbitrary, we're checking it for247// interoperability. We can change the order as long as we file bugs on all implementers to update to the latest tests248// to keep them interoperable.249promise_test(() => {250 const ws = new WritableStream({});251 const writer = ws.getWriter();252 const closePromise = writer.close();253 const events = [];254 return Promise.all([255 closePromise.then(() => {256 events.push('closePromise');257 }),258 writer.closed.then(() => {259 events.push('closed');260 })261 ]).then(() => {262 assert_array_equals(events, ['closePromise', 'closed'],263 'promises must fulfill/reject in the expected order');264 });265}, 'promises must fulfill/reject in the expected order on closure');266promise_test(() => {267 const ws = new WritableStream({});268 // Wait until the WritableStream starts so that the close() call gets processed. Otherwise, abort() will be269 // processed without waiting for completion of the close().270 return delay(0).then(() => {271 const writer = ws.getWriter();272 const closePromise = writer.close();273 const abortPromise = writer.abort(error1);274 const events = [];275 return Promise.all([276 closePromise.then(() => {277 events.push('closePromise');278 }),279 abortPromise.then(() => {280 events.push('abortPromise');281 }),282 writer.closed.then(() => {283 events.push('closed');284 })285 ]).then(() => {286 assert_array_equals(events, ['closePromise', 'abortPromise', 'closed'],287 'promises must fulfill/reject in the expected order');288 });289 });290}, 'promises must fulfill/reject in the expected order on aborted closure');291promise_test(t => {292 const ws = new WritableStream({293 close() {294 return Promise.reject(error1);295 }296 });297 // Wait until the WritableStream starts so that the close() call gets processed.298 return delay(0).then(() => {299 const writer = ws.getWriter();300 const closePromise = writer.close();301 const abortPromise = writer.abort(error2);302 const events = [];303 closePromise.catch(() => events.push('closePromise'));304 abortPromise.catch(() => events.push('abortPromise'));305 writer.closed.catch(() => events.push('closed'));306 return Promise.all([307 promise_rejects(t, error1, closePromise,308 'closePromise must reject with the error returned from the sink\'s close method'),309 promise_rejects(t, error1, abortPromise,310 'abortPromise must reject with the error returned from the sink\'s close method'),311 promise_rejects(t, new TypeError(), writer.closed,312 'writer.closed must reject with a TypeError indicating the stream was aborted')313 ]).then(() => {314 assert_array_equals(events, ['closePromise', 'abortPromise', 'closed'],315 'promises must fulfill/reject in the expected order');316 });317 });318}, 'promises must fulfill/reject in the expected order on aborted and errored closure');319promise_test(t => {320 let resolveWrite;321 let controller;322 const ws = new WritableStream({323 write(chunk, c) {324 controller = c;325 return new Promise(resolve => {326 resolveWrite = resolve;327 });328 }329 });330 const writer = ws.getWriter();331 return writer.ready.then(() => {332 const writePromise = writer.write('c');333 controller.error(error1);334 const closePromise = writer.close();335 let closeRejected = false;336 closePromise.catch(() => {337 closeRejected = true;338 });339 return flushAsyncEvents().then(() => {340 assert_false(closeRejected);341 resolveWrite();342 return Promise.all([343 writePromise,344 promise_rejects(t, error1, closePromise, 'close() should reject')345 ]).then(() => {346 assert_true(closeRejected);347 });348 });349 });350}, 'close() should not reject until no sink methods are in flight');...
Using AI Code Generation
1const wpt = require('wpt-api');2const wptApi = new wpt(process.env.WPT_API_KEY);3const options = {4};5wptApi.runTest(url, options, (err, data) => {6 if (err) {7 console.log(err);8 } else {9 wptApi.closePromise(data.data.testId)10 .then((data) => {11 console.log(data);12 })13 .catch((err) => {14 console.log(err);15 });16 }17});18### `wptApi.runTest(url, options, callback)`19- `runs` - Number of runs to perform (default: 3)20- `location` - Location to test from (default: 'Dulles:Chrome')21- `firstViewOnly` - Only test first view (default: false)22- `connectivity` - Connection type (default: 'Cable')23- `pollResults` - Poll the results every X seconds (default: 5)24- `timeout` - Timeout the test after X seconds (default: 300)25- `video` - Capture video of the test (default: true)26- `private` - Mark the test as private (default: false)27- `web10` - Enable Web10 (default: false)28- `spofDelay` - Delay the SPOF by X seconds (default: 0)
Using AI Code Generation
1var wptool = require('./wptool.js');2 if (err) {3 console.log(err);4 return;5 }6 console.log(data);7});8var wptool = require('./wptool.js');9 if (err) {10 console.log(err);11 return;12 }13 console.log(data);14});15var request = require('request');16var cheerio = require('cheerio');17var Promise = require('promise');18var wptool = {};19wptool.closePromise = function(url, callback) {20 var options = {21 headers: {22 }23 };24 request(options, function(err, res, body) {25 if (err) {26 callback(err);27 return;28 }29 var id = JSON.parse(body).data.testId;30 var result = {31 };32 var options = {33 headers: {34 }35 };36 var promise = new Promise(function(resolve, reject) {37 request(options, function(err, res, body) {38 if (err) {39 reject(err);40 return;41 }42 var status = JSON.parse(body).data.statusText;43 if (status === 'Test Complete') {44 resolve(result);45 } else {46 reject(result);47 }48 });49 });50 promise.then(function(result) {51 var options = {52 headers: {53 }54 };55 request(options, function(err, res, body) {56 if (err) {57 callback(err);58 return;59 }60 var $ = cheerio.load(body);61 var loadTime = $('firstView').attr('loadTime');62 result.loadTime = loadTime;
Using AI Code Generation
1var wpt = require('webpagetest');2var test = wpt('www.webpagetest.org');3test.runTest('www.google.com', {4}, function(err, data) {5 if (err) return console.error(err);6 test.closePromise(data.data.testId).then(function(har) {7 console.log(har);8 });9});10{11 "scripts": {12 },13 "dependencies": {14 },15 "devDependencies": {16 }17}18var request = require('request');19function WPT(apiKey, options) {20 if (!(this instanceof WPT)) {21 return new WPT(apiKey, options);22 }23 if (!options) options = {};24 this._apiKey = apiKey;25 this._apiUrl = options.apiUrl || WPT_API_URL;26 this._apiVersion = options.apiVersion || 2;27}28WPT.prototype.runTest = function(url, options, callback) {29 if (!options) options = {};30 options.url = url;31 options.f = 'json';32 if (this._apiKey) options.k = this._apiKey;33 if (this._apiVersion) options.v = this._apiVersion;34 request({35 }, function(err, response, body) {36 if (err) return callback(err);37 try {38 body = JSON.parse(body);39 } catch (e) {40 return callback(e);41 }42 callback(null, body);43 });44};45WPT.prototype.getTestStatus = function(testId, callback) {46 request({47 qs: {
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q');3wpt.runTest('www.example.com', { location: 'Dulles:Chrome', pollResults: 5 }, function(err, data) {4 if (err) return console.log('Test failed:', err);5 console.log('Test submitted:', data);6 wpt.closePromise(data.data.testId).then(function(result) {7 console.log('Test results:', result);8 }, function(err) {9 console.log('Test failed:', err);10 });11});
Using AI Code Generation
1var wpt = require('webpagetest');2var webPageTest = new wpt('www.webpagetest.org');3var testId = '150102_5W_1R';4webPageTest.closePromise(testId)5.then(function(data) {6 console.log('Test is closed');7 console.log(data);8})9.catch(function(err) {10 console.log('Test is not closed');11 console.log(err);12});
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3api.closePromise('20131109_Z6_9R6', function(err, data) {4 if (err) {5 console.log('Error: ' + err);6 } else {7 console.log(data);8 }9});10var wpt = require('webpagetest');11var api = new wpt('www.webpagetest.org');12api.getLocations(function(err, data) {13 if (err) {14 console.log('Error: ' + err);15 } else {16 console.log(data);17 }18});19var wpt = require('webpagetest');20var api = new wpt('www.webpagetest.org');21api.getLocations(function(err, data) {22 if (err) {23 console.log('Error: ' + err);24 } else {25 console.log(data);26 }27});28var wpt = require('webpagetest');29var api = new wpt('www.webpagetest.org');30api.getTesters(function(err, data) {31 if (err) {32 console.log('Error: ' + err);33 } else {34 console.log(data);35 }36});37var wpt = require('webpagetest');38var api = new wpt('www.webpagetest.org');39api.getTesters(function(err, data) {40 if (err) {41 console.log('Error: ' + err);42 } else {43 console.log(data);44 }45});46var wpt = require('webpagetest');47var api = new wpt('www.webpagetest.org');48api.getLocations(function(err, data) {49 if (err) {50 console.log('Error: ' + err);51 } else {52 console.log(data);53 }54});55var wpt = require('webpagetest');56var api = new wpt('www.webpagetest
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!!