Best JavaScript code snippet using wpt
parsing-utils.js
Source:parsing-utils.js
...10 div.style.setProperty('shape-outside', value);11 document.body.appendChild(div);12 var style = getComputedStyle(div);13 var actual = style.getPropertyValue('shape-outside');14 actual = roundResultStr(actual);15 document.body.removeChild(div);16 // Some of the tests in this suite have either/or expected results17 // so this check allows for testing that at least one of them passes.18 // Description of the 2 expecteds is below near calcTestValues.19 if(Object.prototype.toString.call( expected ) === '[object Array]' && expected.length == 2) {20 assert_in_array(actual, expected);21 } else {22 assert_equals(actual, typeof expected !== 'undefined' ? expected : value);23 }24}25function testShapeMarginInlineStyle(value, expected) {26 var div = document.createElement('div');27 div.style.setProperty('shape-outside', "border-box inset(10px)");28 div.style.setProperty('shape-margin', value);29 var actual = div.style.getPropertyValue('shape-margin');30 actual = roundResultStr(actual);31 expected = roundResultStr(expected);32 assert_equals(actual, expected);33}34function testShapeMarginComputedStyle(value, expected) {35 var outerDiv = document.createElement('div');36 outerDiv.style.setProperty('width', '100px');37 var innerDiv = document.createElement('div');38 innerDiv.style.setProperty('shape-outside', "border-box inset(10px)");39 innerDiv.style.setProperty('shape-margin', value);40 outerDiv.appendChild(innerDiv);41 document.body.appendChild(outerDiv);42 var style = getComputedStyle(innerDiv);43 var actual = style.getPropertyValue('shape-margin');44 assert_not_equals(actual, null);45 if(actual.indexOf('calc') == -1 )46 actual = roundResultStr(actual);47 document.body.removeChild(outerDiv);48 // See comment above about multiple expected results49 if(Object.prototype.toString.call( expected ) === '[object Array]' && expected.length == 2) {50 assert_in_array(actual, expected);51 } else {52 assert_equals(actual, !expected ? '0px' : expected);53 }54}55function testShapeThresholdInlineStyle(value, expected) {56 var div = document.createElement('div');57 div.style.setProperty('shape-outside', 'url(someimage.png)');58 div.style.setProperty('shape-image-threshold', value);59 var actual = div.style.getPropertyValue('shape-image-threshold');60 assert_equals(actual, expected);61}62function testShapeThresholdComputedStyle(value, expected) {63 var div = document.createElement('div');64 div.style.setProperty('shape-outside', 'url(someimage.png)');65 div.style.setProperty('shape-image-threshold', value);66 document.body.appendChild(div);67 var style = getComputedStyle(div);68 var actual = style.getPropertyValue('shape-image-threshold');69 assert_not_equals(actual, null);70 if(actual.indexOf('calc') == -1 )71 actual = roundResultStr(actual);72 document.body.removeChild(div);73 // See comment above about multiple expected results74 if(Object.prototype.toString.call( expected ) === '[object Array]' && expected.length == 2) {75 assert_in_array(actual, expected);76 } else {77 assert_equals(actual, !expected ? '0' : expected);78 }79}80// Builds an array of test cases to send to testharness.js where one test case is: [name, actual, expected]81// These test cases will verify results from testInlineStyle() or testComputedStyle()82function buildTestCases(testCases, testType) {83 var results = [];84 // If test_type isn't specified, test inline style85 var type = typeof testType == 'undefined' ? 'invalid': testType;86 testCases.forEach(function(test) {87 oneTestCase = [];88 // name - annotated by type (inline vs. computed)89 if ( test.hasOwnProperty('name') ) {90 oneTestCase.push(test['name'] +' - '+ type);91 } else {92 // If test_name isn't specified, use the actual93 oneTestCase.push(test['actual'] +' - '+ type);94 }95 // actual96 oneTestCase.push(test['actual'])97 // expected98 if( type.indexOf('invalid') != -1 ){99 oneTestCase.push("")100 } else if( type == 'inline' ) {101 oneTestCase.push(test['expected_inline']);102 } else if( type == 'computed' ){103 oneTestCase.push( convertToPx(test['expected_computed']) );104 }105 results.push(oneTestCase);106 });107 return results;108}109function buildPositionTests(shape, valid, type, units) {110 var results = new Array();111 var convert = type.indexOf('computed') != -1 ? true : false;112 if(Object.prototype.toString.call( units ) === '[object Array]') {113 units.forEach(function(unit) {114 positionTests = buildPositionTests(shape, valid, type, unit);115 results = results.concat(positionTests);116 });117 } else {118 if (valid) {119 validPositions.forEach(function(test) {120 var testCase = [], testName, actual, expected;121 // skip if this isn't explicitly testing length units122 if( !(type.indexOf('lengthUnit') != -1 && test[0].indexOf("u1") == -1)) {123 // actual124 actual = shape + '(at ' + setUnit(test[0], false, units) +')';125 // expected126 // if(convert && shape == 'circle')127 // expected = shape + '(at ' + setUnit(test[1], convert, units) +')';128 // else if(convert && shape == 'ellipse')129 // expected = shape + '(at ' + setUnit(test[1], convert, units) +')';130 // else131 expected = shape + '(at ' + setUnit(test[1], convert, units) +')';132 // name133 if (type == 'lengthUnit + inline')134 testName = 'test unit (inline): ' + units +' - '+ actual;135 else if (type == 'lengthUnit + computed')136 testName = 'test unit (computed): ' + units +' - '+ actual;137 else138 testName = (actual + ' serializes as ' + expected +' - '+ type);139 testCase.push(testName)140 testCase.push(actual);141 testCase.push(expected);142 results.push(testCase);143 }144 });145 } else {146 invalidPositions.forEach(function(test) {147 var testValue = shape + '(at ' + setUnit(test, false, units) +')';148 testCase = new Array();149 testCase.push(testValue + ' is invalid');150 testCase.push(testValue);151 testCase.push("");152 results.push(testCase);153 });154 }155 }156 return unique(results);157}158function buildRadiiTests(shape, type, units) {159 var results = new Array();160 var testUnits = typeof units == 'undefined' ? 'px': units;161 var convert = type.indexOf('computed') != -1 ? true : false;162 if(Object.prototype.toString.call( testUnits ) === '[object Array]') {163 testUnits.forEach(function(unit) {164 radiiTests = buildRadiiTests(shape, type, unit);165 results = results.concat(radiiTests);166 });167 } else {168 var validRadii = shape == 'circle' ? validCircleRadii : validEllipseRadii;169 validRadii.forEach(function(test) {170 var testCase = [], name, actual, expected;171 // skip if this isn't explicitly testing length units172 if( !(type.indexOf('lengthUnit') != -1 && test[0].indexOf("u1") == -1) ) {173 actual = shape + '(' + setUnit(test[0], false, testUnits) +')';174 // name175 if (type.indexOf('lengthUnit') != -1) {176 name = 'test unit: ' + units +' - '+ actual;177 if(type.indexOf('computed') != -1)178 name = name + ' - computed';179 else180 name = name + ' - inline';181 }182 else183 name = actual +' - '+ type;184 testCase.push(name);185 // actual186 testCase.push(actual);187 // expected188 if(type.indexOf('computed') != -1 && test.length == 3) {189 expected = shape + '(' + setUnit(test[2], convert, testUnits) +')';190 } else {191 expected = shape + '(' + setUnit(test[1], convert, testUnits) +')';192 }193 testCase.push(expected);194 results.push(testCase);195 }196 });197 }198 return unique(results);199}200function buildInsetTests(unit1, unit2, type) {201 var results = new Array();202 var convert = type == 'computed' ? true : false;203 if(Object.prototype.toString.call( unit1 ) === '[object Array]') {204 unit1.forEach(function(unit) {205 insetTests = buildInsetTests(unit, unit2, type);206 results = results.concat(insetTests);207 });208 } else {209 validInsets.forEach(function(test) {210 var testCase = [], name, actual, expected;211 name = setUnit(test[0], false, unit1, unit2) +' - '+ type;212 actual = 'inset(' + setUnit(test[1], convert, unit1, unit2) +')';213 expected = actual;214 testCase.push(name);215 testCase.push(actual);216 testCase.push(expected);217 results.push(testCase);218 });219 }220 return unique(results);221}222function buildPolygonTests(unitSet, type) {223 var results = new Array();224 var convert = type == 'computed' ? true : false;225 unitSet.forEach(function(set) {226 validPolygons.forEach(function(test) {227 var testCase = [];228 // name229 testCase.push(setUnit(test[0], false, set[0], set[1], set[2]) +' - '+ type);230 // actual231 testCase.push('polygon(' + setUnit(test[1], false, set[0], set[1], set[2]) +')');232 // expected233 testCase.push('polygon(' + setUnit(test[1], convert, set[0], set[1], set[2]) +')');234 results.push(testCase);235 });236 });237 return unique(results);238}239function buildCalcTests(testCases, type) {240 var results = new Array();241 testCases.forEach(function(test){242 var testCase = [];243 if(type == 'computed') {244 testCase.push(test[0] + ' - computed style');245 testCase.push(test[0]);246 testCase.push(test[2]);247 }248 else {249 testCase.push(test[0] + ' - inline style');250 testCase.push(test[0]);251 testCase.push(test[1]);252 }253 testCase.push(type);254 results.push(testCase)255 });256 return unique(results);257}258function unique(tests) {259 var list = tests.concat();260 for(var i = 0; i< list.length; ++i) {261 for(var j = i+1; j < list.length; ++j) {262 if(list[i][0] === list[j][0])263 list.splice(j--, 1);264 }265 }266 return list;267}268function setUnit(str, convert, unit1, unit2, unit3) {269 var retStr = str;270 if(typeof unit1 !== 'undefined') {271 retStr = retStr.replace(new RegExp('u1', 'g'), unit1);272 }273 if(typeof unit2 !== 'undefined') {274 retStr = retStr.replace(new RegExp("u2", 'g'), unit2);275 }276 if(typeof unit3 !== 'undefined') {277 retStr = retStr.replace(new RegExp("u3", 'g'), unit3);278 }279 retStr = convert ? convertToPx(retStr) : retStr;280 return retStr;281}282function roundCssNumber(n) {283 // See https://drafts.csswg.org/cssom/#serializing-css-values for numbers.284 return parseFloat(n.toPrecision(6));285}286function convertToPx(origValue) {287 var valuesToConvert = origValue.match(/[0-9]+(\.[0-9]+)?([a-z]{2,4}|%|)/g);288 if(!valuesToConvert)289 return origValue;290 var retStr = origValue;291 for(var i = 0; i < valuesToConvert.length; i++) {292 var unit = (valuesToConvert[i].match(/[a-z]{2,4}|%/) || '').toString();293 var numberStr = valuesToConvert[i].match(/[0-9]+(\.[0-9]+)?/)[0];294 var number = parseFloat(numberStr);295 var convertedUnit = 'px';296 if( typeof number !== 'NaN' )297 {298 if (unit == 'in') {299 number = (96 * number);300 } else if (unit == 'cm') {301 number = (37.795275591 * number);302 } else if (unit == 'mm') {303 number = (3.779527559 * number);304 } else if (unit == 'pt') {305 number = (1.333333333333 * number);306 } else if (unit == 'pc') {307 number = (16 * number);308 } else if (unit == 'em') {309 number = (16 * number);310 } else if (unit == 'ex') {311 number = (12.8 * number);312 } else if (unit == 'ch') {313 number = (16 * number);314 } else if (unit == 'rem') {315 number = (16 * number);316 } else if (unit == 'vw') {317 number = ((.01 * window.innerWidth) * number);318 } else if (unit == 'vh') {319 number = ((.01 * window.innerHeight) * number);320 } else if (unit == 'vmin') {321 number = Math.min( (.01 * window.innerWidth), (.01 * window.innerHeight) ) * number;322 } else if (unit == 'vmax') {323 number = Math.max( (.01 * window.innerWidth), (.01 * window.innerHeight) ) * number;324 }325 else {326 convertedUnit = unit;327 }328 number = roundCssNumber(number);329 var find = valuesToConvert[i];330 var replace = number.toString() + convertedUnit;331 retStr = retStr.replace(valuesToConvert[i], number.toString() + convertedUnit);332 }333 }334 return retStr.replace(',,', ',');335}336function roundResultStr(str) {337 if(Object.prototype.toString.call( str ) !== '[object String]')338 return str;339 var numbersToRound = str.match(/[0-9]+\.[0-9]+/g);340 if(!numbersToRound)341 return str;342 var retStr = str;343 for(var i = 0; i < numbersToRound.length; i++) {344 num = parseFloat(numbersToRound[i]);345 if( !isNaN(num) ) {346 roundedNum = roundCssNumber(num);347 retStr = retStr.replace(numbersToRound[i].toString(), roundedNum.toString());348 }349 }350 return retStr;...
Using AI Code Generation
1var wpt = require('webpagetest');2var test = wpt('www.webpagetest.org');3 if (err) {4 console.error(err);5 } else {6 test.getTestResults(data.data.testId, function(err, data) {7 if (err) {8 console.error(err);9 } else {10 console.log(test.roundResultStr(data.data.median.firstView.SpeedIndex));11 }12 });13 }14});15var wpt = require('webpagetest');16var test = wpt('www.webpagetest.org');17}, function(err, data) {18 if (err) {19 console.error(err);20 } else {21 console.log(data);22 }23});24var wpt = require('webpagetest');25var test = wpt('www.webpagetest.org');26}, function(err, data) {27 if (err) {28 console.error(err);29 } else {30 console.log(data);31 }32});33var wpt = require('webpagetest');34var test = wpt('www.webpagetest.org');35}, function(err, data) {36 if (err) {37 console.error(err);38 } else {39 console.log(data);40 }41});42var wpt = require('webpagetest');43var test = wpt('www.webpagetest.org');
Using AI Code Generation
1var wpt = require('wpt');2var wptObj = new wpt();3wptObj.roundResultStr("hello", function (err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
Using AI Code Generation
1var wpt = require('./wpt.js');2var wptObj = new wpt();3wptObj.roundResultStr("1.23456789", 3, function(err, result) {4 if (err) {5 console.log("Error: "+err);6 } else {7 console.log("Result: "+result);8 }9});10### getLocations(callback)11var wpt = require('./wpt.js');12var wptObj = new wpt();13wptObj.getLocations(function(err, result) {14 if (err) {15 console.log("Error: "+err);16 } else {17 console.log("Result: "+result);18 }19});20### getTesters(callback)21var wpt = require('./wpt.js');22var wptObj = new wpt();23wptObj.getTesters(function(err, result) {24 if (err) {25 console.log("Error: "+err);26 } else {27 console.log("Result: "+result);28 }29});30### getTestersAtLocation(location, callback)
Using AI Code Generation
1var wpt = require('./wpt.js');2var request = require('request');3var fs = require('fs');4var resultStr = wpt.roundResultStr(url);5console.log(resultStr);6var request = require('request');7var fs = require('fs');8function roundResultStr(url){9 var resultStr = '';10 var result = {};11 var options = {12 headers: {13 }14 };15 request(options, function (error, response, body) {16 if (!error && response.statusCode == 200) {17 var resultObj = JSON.parse(body);18 result = resultObj.data.average.firstView;19 resultStr += 'First View:\n';20 resultStr += 'Load Time: ' + result.loadTime + 'ms\n';21 resultStr += 'TTFB: ' + result.TTFB + 'ms\n';22 resultStr += 'Start Render: ' + result.render + 'ms\n';23 resultStr += 'Speed Index: ' + result.SpeedIndex + '\n';24 resultStr += 'Visual Complete: ' + result.visualComplete + 'ms\n';25 resultStr += 'Fully Loaded: ' + result.fullyLoaded + 'ms\n';26 resultStr += 'Bytes In: ' + result.bytesIn + '\n';27 resultStr += 'Bytes Out: ' + result.bytesOut + '\n';28 resultStr += 'Requests: ' + result.requests + '\n';29 resultStr += 'Score Cache: ' + result.score_cache + '\n';30 resultStr += 'Score CDN: ' + result.score_cdn + '\n';31 resultStr += 'Score Gzip: ' + result.score_gzip + '\n';32 resultStr += 'Score Cookies: ' + result.score_cookies + '\n';33 resultStr += 'Score Keep-alive: ' + result.score_keep-alive + '\n';34 resultStr += 'Score Minify: ' + result.score_minify + '\n';35 resultStr += 'Score Combine: ' + result.score_combine + '\n';
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!!