Best JavaScript code snippet using wpt
editor-test-utils.js
Source:editor-test-utils.js
1/**2 * EditorTestUtils is a helper utilities to test HTML editor. This can be3 * instantiated per an editing host. If you test `designMode`, the editing4 * host should be the <body> element.5 */6class EditorTestUtils {7 kShift = "\uE008";8 kMeta = "\uE03d";9 kControl = "\uE009";10 kAlt = "\uE00A";11 editingHost;12 constructor(aEditingHost, aHarnessWindow = window) {13 this.editingHost = aEditingHost;14 if (aHarnessWindow != this.window) {15 this.window.test_driver.set_test_context(aHarnessWindow);16 }17 }18 get document() {19 return this.editingHost.ownerDocument;20 }21 get window() {22 return this.document.defaultView;23 }24 get selection() {25 return this.window.getSelection();26 }27 sendKey(key, modifier) {28 if (!modifier) {29 return new this.window.test_driver.Actions()30 .keyDown(key)31 .keyUp(key)32 .send();33 }34 return new this.window.test_driver.Actions()35 .keyDown(modifier)36 .keyDown(key)37 .keyUp(key)38 .keyUp(modifier)39 .send();40 }41 sendDeleteKey(modifier) {42 const kDeleteKey = "\uE017";43 return this.sendKey(kDeleteKey, modifier);44 }45 sendBackspaceKey(modifier) {46 const kBackspaceKey = "\uE003";47 return this.sendKey(kBackspaceKey, modifier);48 }49 sendArrowLeftKey(modifier) {50 const kArrowLeft = "\uE012";51 return this.sendKey(kArrowLeft, modifier);52 }53 sendArrowRightKey(modifier) {54 const kArrowRight = "\uE014";55 return this.sendKey(kArrowRight, modifier);56 }57 sendHomeKey(modifier) {58 const kHome = "\uE011";59 return this.sendKey(kHome, modifier);60 }61 sendEndKey(modifier) {62 const kEnd = "\uE010";63 return this.sendKey(kEnd, modifier);64 }65 // Similar to `setupDiv` in editing/include/tests.js, this method sets66 // innerHTML value of this.editingHost, and sets multiple selection ranges67 // specified with the markers.68 // - `[` specifies start boundary in a text node69 // - `{` specifies start boundary before a node70 // - `]` specifies end boundary in a text node71 // - `}` specifies end boundary after a node72 setupEditingHost(innerHTMLWithRangeMarkers) {73 const startBoundaries = innerHTMLWithRangeMarkers.match(/\{|\[/g) || [];74 const endBoundaries = innerHTMLWithRangeMarkers.match(/\}|\]/g) || [];75 if (startBoundaries.length !== endBoundaries.length) {76 throw "Should match number of open/close markers";77 }78 this.editingHost.innerHTML = innerHTMLWithRangeMarkers;79 this.editingHost.focus();80 if (startBoundaries.length === 0) {81 // Don't remove the range for now since some tests may assume that82 // setting innerHTML does not remove all selection ranges.83 return;84 }85 let getNextRangeAndDeleteMarker = startNode => {86 let getNextLeafNode = node => {87 let inclusiveDeepestFirstChildNode = container => {88 while (container.firstChild) {89 container = container.firstChild;90 }91 return container;92 };93 if (node.hasChildNodes()) {94 return inclusiveDeepestFirstChildNode(node);95 }96 if (node.nextSibling) {97 return inclusiveDeepestFirstChildNode(node.nextSibling);98 }99 let nextSibling = (child => {100 for (101 let parent = child.parentElement;102 parent && parent != this.editingHost;103 parent = parent.parentElement104 ) {105 if (parent.nextSibling) {106 return parent.nextSibling;107 }108 }109 return null;110 })(node);111 if (!nextSibling) {112 return null;113 }114 return inclusiveDeepestFirstChildNode(nextSibling);115 };116 let scanMarkerInTextNode = (textNode, offset) => {117 return /[\{\[\]\}]/.exec(textNode.data.substr(offset));118 };119 let startMarker = ((startContainer, startOffset) => {120 let scanStartMakerInTextNode = (textNode, offset) => {121 let scanResult = scanMarkerInTextNode(textNode, offset);122 if (scanResult === null) {123 return null;124 }125 if (scanResult[0] === "}" || scanResult[0] === "]") {126 throw "An end marker is found before a start marker";127 }128 return {129 marker: scanResult[0],130 container: textNode,131 offset: scanResult.index + offset132 };133 };134 if (startContainer.nodeType === Node.TEXT_NODE) {135 let scanResult = scanStartMakerInTextNode(136 startContainer,137 startOffset138 );139 if (scanResult !== null) {140 return scanResult;141 }142 }143 let nextNode = startContainer;144 while ((nextNode = getNextLeafNode(nextNode))) {145 if (nextNode.nodeType === Node.TEXT_NODE) {146 let scanResult = scanStartMakerInTextNode(nextNode, 0);147 if (scanResult !== null) {148 return scanResult;149 }150 continue;151 }152 }153 return null;154 })(startNode, 0);155 if (startMarker === null) {156 return null;157 }158 let endMarker = ((startContainer, startOffset) => {159 let scanEndMarkerInTextNode = (textNode, offset) => {160 let scanResult = scanMarkerInTextNode(textNode, offset);161 if (scanResult === null) {162 return null;163 }164 if (scanResult[0] === "{" || scanResult[0] === "[") {165 throw "A start marker is found before an end marker";166 }167 return {168 marker: scanResult[0],169 container: textNode,170 offset: scanResult.index + offset171 };172 };173 if (startContainer.nodeType === Node.TEXT_NODE) {174 let scanResult = scanEndMarkerInTextNode(startContainer, startOffset);175 if (scanResult !== null) {176 return scanResult;177 }178 }179 let nextNode = startContainer;180 while ((nextNode = getNextLeafNode(nextNode))) {181 if (nextNode.nodeType === Node.TEXT_NODE) {182 let scanResult = scanEndMarkerInTextNode(nextNode, 0);183 if (scanResult !== null) {184 return scanResult;185 }186 continue;187 }188 }189 return null;190 })(startMarker.container, startMarker.offset + 1);191 if (endMarker === null) {192 throw "Found an open marker, but not found corresponding close marker";193 }194 let indexOfContainer = (container, child) => {195 let offset = 0;196 for (let node = container.firstChild; node; node = node.nextSibling) {197 if (node == child) {198 return offset;199 }200 offset++;201 }202 throw "child must be a child node of container";203 };204 let deleteFoundMarkers = () => {205 let removeNode = node => {206 let container = node.parentElement;207 let offset = indexOfContainer(container, node);208 node.remove();209 return { container, offset };210 };211 if (startMarker.container == endMarker.container) {212 // If the text node becomes empty, remove it and set collapsed range213 // to the position where there is the text node.214 if (startMarker.container.length === 2) {215 if (!/[\[\{][\]\}]/.test(startMarker.container.data)) {216 throw `Unexpected text node (data: "${startMarker.container.data}")`;217 }218 let { container, offset } = removeNode(startMarker.container);219 startMarker.container = endMarker.container = container;220 startMarker.offset = endMarker.offset = offset;221 startMarker.marker = endMarker.marker = "";222 return;223 }224 startMarker.container.data = `${startMarker.container.data.substring(225 0,226 startMarker.offset227 )}${startMarker.container.data.substring(228 startMarker.offset + 1,229 endMarker.offset230 )}${startMarker.container.data.substring(endMarker.offset + 1)}`;231 if (startMarker.offset >= startMarker.container.length) {232 startMarker.offset = endMarker.offset =233 startMarker.container.length;234 return;235 }236 endMarker.offset--; // remove the start marker's length237 if (endMarker.offset > endMarker.container.length) {238 endMarker.offset = endMarker.container.length;239 }240 return;241 }242 if (startMarker.container.length === 1) {243 let { container, offset } = removeNode(startMarker.container);244 startMarker.container = container;245 startMarker.offset = offset;246 startMarker.marker = "";247 } else {248 startMarker.container.data = `${startMarker.container.data.substring(249 0,250 startMarker.offset251 )}${startMarker.container.data.substring(startMarker.offset + 1)}`;252 }253 if (endMarker.container.length === 1) {254 let { container, offset } = removeNode(endMarker.container);255 endMarker.container = container;256 endMarker.offset = offset;257 endMarker.marker = "";258 } else {259 endMarker.container.data = `${endMarker.container.data.substring(260 0,261 endMarker.offset262 )}${endMarker.container.data.substring(endMarker.offset + 1)}`;263 }264 };265 deleteFoundMarkers();266 let handleNodeSelectMarker = () => {267 if (startMarker.marker === "{") {268 if (startMarker.offset === 0) {269 // The range start with the text node.270 let container = startMarker.container.parentElement;271 startMarker.offset = indexOfContainer(272 container,273 startMarker.container274 );275 startMarker.container = container;276 } else if (startMarker.offset === startMarker.container.data.length) {277 // The range start after the text node.278 let container = startMarker.container.parentElement;279 startMarker.offset =280 indexOfContainer(container, startMarker.container) + 1;281 startMarker.container = container;282 } else {283 throw 'Start marker "{" is allowed start or end of a text node';284 }285 }286 if (endMarker.marker === "}") {287 if (endMarker.offset === 0) {288 // The range ends before the text node.289 let container = endMarker.container.parentElement;290 endMarker.offset = indexOfContainer(container, endMarker.container);291 endMarker.container = container;292 } else if (endMarker.offset === endMarker.container.data.length) {293 // The range ends with the text node.294 let container = endMarker.container.parentElement;295 endMarker.offset =296 indexOfContainer(container, endMarker.container) + 1;297 endMarker.container = container;298 } else {299 throw 'End marker "}" is allowed start or end of a text node';300 }301 }302 };303 handleNodeSelectMarker();304 let range = document.createRange();305 range.setStart(startMarker.container, startMarker.offset);306 range.setEnd(endMarker.container, endMarker.offset);307 return range;308 };309 let ranges = [];310 for (311 let range = getNextRangeAndDeleteMarker(this.editingHost.firstChild);312 range;313 range = getNextRangeAndDeleteMarker(range.endContainer)314 ) {315 ranges.push(range);316 }317 this.selection.removeAllRanges();318 for (let range of ranges) {319 this.selection.addRange(range);320 }321 if (this.selection.rangeCount != ranges.length) {322 throw `Failed to set selection to the given ranges whose length is ${ranges.length}, but only ${this.selection.rangeCount} ranges are added`;323 }324 }...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5 if (err) return console.error(err);6 console.log(data);7});8## `getLocations(callback)`9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org');11wpt.getLocations(function(err, data) {12 if (err) return console.error(err);13 console.log(data);14});15## `getTesters(callback)`16var wpt = require('webpagetest');17var wpt = new WebPageTest('www.webpagetest.org');18wpt.getTesters(function(err, data) {19 if (err) return console.error(err);20 console.log(data);21});22## `getTesters(location, callback)`23var wpt = require('webpagetest');24var wpt = new WebPageTest('www.webpagetest.org');25wpt.getTesters('ec2-us-west-1:Chrome', function(err, data) {26 if (err) return console.error(err);27 console.log(data);28});29## `getTestStatus(testId, callback)`30var wpt = require('webpagetest');
Using AI Code Generation
1var wpt = new WebPageTest('www.webpagetest.org', 'A.8c5f9f7a5c5f9f7a5c5f9f7a5c5f9f7a');2}, function(err, data) {3 if (err) return console.error(err);4 console.log('Test status: ' + data.statusText);5 console.log('Test ID: ' + data.data.testId);6 console.log('Test URL: ' + data.data.summary);7 console.log('View results: ' + data.data.userUrl);8 console.log('json results: ' + data.data.jsonUrl);9});10wpt.getTestStatus('160804_5Z_1cf2e1b0c8d7b0cb5c5f7d5e5f8c7f7b', function(err, data) {11 if (err) return console.error(err);12 console.log('Test status: ' + data.statusText);13 console.log('Test ID: ' + data.data.testId);14 console.log('Test URL: ' + data.data.summary);15 console.log('View results: ' + data.data.userUrl);16 console.log('json results: ' + data.data.jsonUrl);17});18wpt.getLocations(function(err, data) {19 if (err) return console.error(err);20 console.log(data);21});22wpt.getTesters(function(err, data) {23 if (err) return console.error(err);24 console.log(data);25});26wpt.getTesters('Dulles:Chrome', function(err, data) {27 if (err) return console.error(err);28 console.log(data);29});30wpt.getTesters('Dulles:Chrome', 'Cable', function(err, data) {31 if (err) return console.error(err);
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest(options);5 if (err) return console.error(err);6 console.log(data);7});
Using AI Code Generation
1var WebPageTest = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.8c1a1d3c7a3c3c1e8b3e0b3a7c3a3a1');3 if (err) return console.error(err);4 console.log(data);5});6var WebPageTest = require('webpagetest');7var wpt = new WebPageTest('www.webpagetest.org', 'A.8c1a1d3c7a3c3c1e8b3e0b3a7c3a3a1');8wpt.testStatus('150629_1B_1', function(err, data) {9 if (err) return console.error(err);10 console.log(data);11});12var WebPageTest = require('webpagetest');13var wpt = new WebPageTest('www.webpagetest.org', 'A.8c1a1d3c7a3c3c1e8b3e0b3a7c3a3a1');14wpt.getTestResults('150629_1B_1', function(err, data) {15 if (err) return console.error(err);16 console.log(data);17});18var WebPageTest = require('webpagetest');19var wpt = new WebPageTest('www.webpagetest.org', 'A.8c1a1d3c7a3c3c1e8b3e0b3a7c3a3a1');20 if (err) return console.error(err);21 console.log(data);22});
Using AI Code Generation
1var wpt = require('./wpt.js');2var boundaries = require('./boundaries.js');3var boundaryArray = boundaries.getBoundaries();4var testId;5var startTest = function() {6 wpt.startBoundaries(boundaryArray)7 .then(function(result) {8 testId = result.data.testId;9 console.log('testId: ', testId);10 wpt.getResults2(testId);11 });12};13startTest();
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!!