Best JavaScript code snippet using wpt
hotpic.js
Source:hotpic.js
1/*2 * hotlink.js3 * 2011-08-244 * 5 * By Eli Grey, http://eligrey.com6 * Licensed under the MIT License7 * See https://github.com/eligrey/hotlink.js/blob/master/LICENSE.md8 */9/*! @source http://purl.eligrey.com/github/hotlink.js/blob/master/hotlink.js */10/*global self, document*/11var hotlink = (function(view, document) {12 "use strict";13 var14 HTML_NS = "http://www.w3.org/1999/xhtml"15 , HOTLINK_NS = "http://purl.eligrey.com/hotlink"16 , doc_elt = document.documentElement17 , user_agent = view.navigator.userAgent18 // Some bugs just can't be solved with feature detection19 , firefox = ~user_agent.indexOf("Gecko/")20 , ie = ~user_agent.indexOf("MSIE")21 , opera = view.opera22 , click = function(elt) {23 if (elt.click) {24 elt.click();25 } else {26 var event = document.createEvent("Event");27 event.initEvent("click", true, true);28 elt.dispatchEvent(event);29 }30 }31 , append = function(node, child) {32 return node.appendChild(child);33 }34 , create_elt = function(ns, name, doc) {35 return (doc || document).createElementNS(ns, name);36 }37 , attr = function(elt, attr, val) {38 var old_val = elt.getAttribute(attr);39 if (arguments.length === 3) {40 if (val === false) {41 elt.removeAttribute(attr);42 } else {43 elt.setAttribute(attr, val);44 }45 }46 return old_val;47 }48 , frame_img = function(img) {49 var50 parent = img.parentNode51 // I make the assumption that you're not using namespaces in your CSS52 , container = parent.insertBefore(create_elt(HOTLINK_NS, "img"), img)53 , frame = append(container, create_elt(HTML_NS, "iframe"))54 , link = create_elt(HTML_NS, "a", frame.contentDocument)55 , src = attr(img, "data-src", false) // img.dataset.src56 , width = +attr(img, "width", false)57 , height = +attr(img, "height", false)58 , attrs, i, attribute59 ;60 if(width==0){61 width = +attr(img, "data-rawwidth", false)62 }63 if(height==0){64 height = +attr(img, "data-rawheight", false)65 } 66 data-rawwidth67 if (firefox || opera) {68 // Firefox & Opera will need 16px padding once they support noreferrer69 // There is no other way to do this without feature detection70 width += 16;71 height += 16;72 }73 attrs = img.attributes;74 i = attrs.length;75 while (i--) {76 // copy attributes77 attribute = attrs[i];78 attr(container, attribute.name, attribute.value);79 attr(img, attribute.name, false);80 }81 parent.removeChild(img);82 frame.style.verticalAlign = "bottom";83 84 frame.style.width = width + "px";85 frame.style.height = height + "px";86 if(width==0){87 frame.style.width = "100%";88 }89 if(height==0){90 frame.style.height = "960px";91 } 92 frame.scrolling = "no";93 frame.style.border = 0;94 link.rel = "noreferrer";95 link.href = src;96 if (firefox || ie) {97 frame.src = src;98 }99 click(link);100 }101 , ready = false102 , ready_queue = []103 , noreferrer_supported = false104 , hotlink = function(img) {105 if (ready) {106 if (noreferrer_supported) {107 frame_img(img);108 } else {109 // remap data-src to src110 attr(img, "src", attr(img, "data-src", false));111 }112 } else {113 ready_queue.push(img);114 }115 }116 , test_frame = append(doc_elt, create_elt(HTML_NS, "iframe"))117 , test_link = create_elt(HTML_NS, "a", test_frame.contentDocument)118 , test_link_url = "about:blank"119 , on_ready = function() {120 ready = true;121 noreferrer_supported = test_frame.contentDocument.referrer === "";122 var123 i = 0124 , len = ready_queue.length125 ;126 for (; i < len; i++) {127 hotlink(ready_queue[i]);128 }129 }130 , style = create_elt(HTML_NS, "style")131 , imgs = document.querySelectorAll("img[data-src]")132 , i = imgs.length133 , img134 ;135 while (i--) {136 img = imgs[i];137 if (img.namespaceURI === HTML_NS) {138 hotlink(img);139 }140 }141 // I'm not using CSSOM insertRule becuase WebKit & Opera don't support @declarations142 append(style, document.createTextNode(143 "@namespace'" + HOTLINK_NS + "';img{display:block;vertical-align:bottom}"144 ));145 append(doc_elt, style);146 test_frame.style.visibility = "hidden";147 test_frame.style.height = test_frame.style.border = 0;148 test_link.rel = "noreferrer";149 // Firefox & IE have a problem with setting document.referrer for about:blank, so150 // I use the /robots.txt instead.151 if (firefox || ie) {152 test_link_url = "/robots.txt";153 }154 test_link.href = test_link_url;155 test_frame.addEventListener("load", on_ready, false);156 // Firefox & IE are buggy with link.click() so you need to set the src manually157 if (firefox || ie) {158 test_frame.src = test_link.href;159 }160 click(test_link);161 return hotlink;...
harness.js
Source:harness.js
1function rr(passed) {2 setTimeout(function() {gTestsuite.record_result(passed);}, 0);3}4var gTestsuite = null;5function TestSuite(tests, timeout, test_done_callback,6 suite_done_callback) {7 if (gTestsuite != null) {8 throw "Tried to create two testsuites";9 }10 gTestsuite = this;11 this.tests = tests;12 this.test_walker = null;13 this.timeout = timeout;14 this.test_done_callback = test_done_callback;15 this.suite_done_callback = suite_done_callback;16 this.harness_window = null;17 this.reset();18}19TestSuite.prototype.toString = function() {20 return "[object TestSuite]";21};22TestSuite.prototype.reset = function() {23 this.tests_run = 0;24 this.test_walker = new TreeWalker(this.tests);25 this.current_test = null;26 this.results = {27 "passed":[],28 "failed":[],29 "timed out":[]30 };31 this.test_timeout_id = null;32 if (this.harness_window !== null) {33 this.close_harness_window();34 }35};36TestSuite.prototype.run = function() {37 this.reset();38 this.open_harness_window();39};40TestSuite.prototype.start_callback = function(test_frame) {41 /*42 * This is called from the harness file when it wants to43 * start running44 */45 this.test_frame = test_frame;46 this.run_next_test();47};48TestSuite.prototype.open_harness_window = function() {49 this.harness_window = window.open("harness.html");50};51TestSuite.prototype.close_harness_window = function() {52 this.harness_window.close();53 this.harness_window = null;54};55TestSuite.prototype.run_next_test = function() {56 while (true) {57 var test_node = this.test_walker.next();58 if (test_node === null) {59 return this.done();60 } else if (test_node.is_leaf() && test_node.cols[0].value) {61 break;62 }63 }64 this.current_test = test_node;65 if (this.timeout > 0) {66 var ts = this;67 this.test_timeout_id = setTimeout(68 function() {69 clearTimeout(ts.test_timeout_id);70 ts.test_timeout_id = null;71 /*72 * If we have a test that times out we want to stop it running73 * Unfortunatley it seems that trying to just set the iframe74 * source doesn't actually do anything; it gets caught in the75 * event loop behind the running script. However deleting the76 * whole iframe and replacing it with a new one works just fine77 */78 var new_frame = ts.test_frame.ownerDocument.createElement("iframe");79 ts.test_frame.parentNode.replaceChild(new_frame, ts.test_frame);80 ts.test_frame = new_frame;81 ts.record_result("timeout");82 }, this.timeout);83 }84 if (!this.test_frame) {85 this.stop();86 } else {87 //Try deleting the iframe on each iteration in case it helps reduce memory usage88 var new_frame = ts.test_frame.ownerDocument.createElement("iframe");89 this.test_frame.parentNode.replaceChild(new_frame, ts.test_frame);90 this.test_frame = new_frame;91 this.test_frame.contentWindow.location = test_node.cols[1]["href"];92 }93};94TestSuite.prototype.stop = function() {95 clearTimeout(this.test_timeout_id);96 this.test_timeout_id = null;97 this.close_harness_window();98 this.test_frame = null;99};100TestSuite.prototype.record_result = function(result, stop) {101 if (stop === undefined) {102 stop = false;103 }104 var test_node = this.current_test;105 if (this.test_timeout_id !== null) {106 clearTimeout(this.test_timeout_id);107 }108 var result_col = {109 "type":"text",110 "value":"",111 "attrib":{}112 };113 if (result === "timeout") {114 result_col.value = "timed out";115 result_col.attrib["class"] = "result timeout";116 this.results["timed out"].push(test_node);117 } else if (result === true) {118 result_col.value = "passed";119 this.results["passed"].push(test_node);120 result_col.attrib["class"] = "result pass";121 } else if(result === false) {122 result_col.value = "failed";123 this.results["failed"].push(test_node);124 result_col.attrib["class"] = "result fail";125 } else {126 throw new HarnessError("Unexpected test result");127 }128 test_node.cols.push(result_col);129 this.tests_run += 1;130 this.test_done_callback(test_node, this);131 var ts = this;132 if (!stop) {133 setTimeout(function () {ts.run_next_test();}, 0);134 }135};136TestSuite.prototype.done = function() {137 return this.suite_done_callback(this);138};139TestSuite.prototype.count_tests = function() {140 var walker = new TreeWalker(this.tests);141 var count = 0;142 while (true) {143 var test_node = walker.next();144 if (test_node === null) {145 break;146 } else if (test_node.is_leaf() && test_node.cols[0].value) {147 count++;148 }149 }150 return count;151};152function HarnessError(message) {153 this.message = message;...
annotate.component.ts
Source:annotate.component.ts
1import { Component, HostListener } from '@angular/core';2import { AnnotateService } from '../services/annotate.service';3@Component({4 selector: 'app-annotate',5 templateUrl: './annotate.component.html',6 styleUrls: ['./annotate.component.css']7})8export class AnnotateComponent {9 constructor(public annotateService: AnnotateService) { }10 public test_frame = {11 "test_person_id": null,12 "position": null,13 "aoi": null14 };15 public timeLeft: number = 5;16 public interval;17 public recording: boolean = false;18 public position_list: Array<Number> = [1,2,3,4,5];19 public aoi_list: Array<Number> = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];20 @HostListener('window:keyup', ['$event'])21 keyEvent(event: KeyboardEvent) { 22 if (event.key == "ArrowRight") {23 this.next();24 } 25 }26 randomize(){27 this.position_list = this.shuffle(this.position_list);28 this.aoi_list = this.shuffle(this.aoi_list);29 }30 startTimer() {31 this.interval = setInterval(() => {32 if(this.timeLeft > 0) {33 this.timeLeft--;34 } else {35 this.pauseTimer();36 }37 },1000)38 }39 pauseTimer() { 40 clearInterval(this.interval);41 this.timeLeft = 5;42 }43 start(){44 if(!this.recording){45 this.test_frame.position = this.position_list[0];46 this.test_frame.aoi = this.aoi_list[0];47 let meta = {48 "test_person_id": this.test_frame.test_person_id,49 "position_list": this.position_list,50 "aoi_list": this.aoi_list51 };52 this.annotateService.sendMeta(meta).subscribe(res => {53 this.annotateService.annotate(this.test_frame, "start").subscribe(res => {54 console.log("Start");55 this.startTimer();56 this.recording = true;57 });58 })59 }60 }61 next(){62 this.annotateService.annotate(this.test_frame, "next").subscribe(res => {63 console.log("Next");64 console.log(Date.now());65 console.log(new Date(Date.now())); 66 this.setAoi();67 this.pauseTimer();68 this.startTimer();69 });70 }71 stop(){72 if(this.recording){73 this.annotateService.annotate(this.test_frame, "stop").subscribe(res => {74 console.log("Stop"); 75 this.recording = false;76 this.resetFrame(); 77 });78 }79 }80 //src: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array81 private shuffle(array) {82 var currentIndex = array.length, temporaryValue, randomIndex;83 84 // While there remain elements to shuffle...85 while (0 !== currentIndex) {86 87 // Pick a remaining element...88 randomIndex = Math.floor(Math.random() * currentIndex);89 currentIndex -= 1;90 91 // And swap it with the current element.92 temporaryValue = array[currentIndex];93 array[currentIndex] = array[randomIndex];94 array[randomIndex] = temporaryValue;95 }96 97 return array;98 }99 100 private resetFrame() {101 this.test_frame.position = null;102 this.test_frame.aoi = null;103 this.pauseTimer();104 }105 private setPosition() { 106 if((this.position_list.length-1 == this.position_list.indexOf(this.test_frame.position)) && (this.aoi_list.length-1 == this.aoi_list.indexOf(this.test_frame.aoi))){107 this.stop();108 }109 else{110 if(this.position_list.length <= this.position_list.indexOf(this.test_frame.position)){111 this.test_frame.position = this.position_list[0];112 this.setPosition();113 }114 else{115 this.test_frame.position = this.position_list[this.position_list.indexOf(this.test_frame.position) + 1];116 }117 }118 }119 private setAoi(){120 if(this.aoi_list.length-1 <= this.aoi_list.indexOf(this.test_frame.aoi)){ 121 this.setPosition();122 this.test_frame.aoi = this.aoi_list[0];123 }124 else{125 this.test_frame.aoi = this.aoi_list[this.aoi_list.indexOf(this.test_frame.aoi) + 1];126 }127 }...
Using AI Code Generation
1var wpt = require('webpagetest')('www.webpagetest.org', 'A.8a1b0e9b9d7b1c8b0e0c0f0a0a0b0c0e');2var location = 'Dulles:Chrome';3var runs = 1;4var mobile = false;5var firstViewOnly = true;6var private = false;7var video = false;8var pollResults = 10;9var timeout = 60;10wpt.test_frame(url, location, runs, mobile, firstViewOnly, private, video, pollResults, timeout, function(err, data) {11 if(err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var wpt = require('webpagetest')('www.webpagetest.org', 'A.8a1b0e9b9d7b1c8b0e0c0f0a0a0b0c0e');18var location = 'Dulles:Chrome';19var runs = 1;20var mobile = false;21var firstViewOnly = true;22var private = false;23var video = true;24var pollResults = 10;25var timeout = 60;26wpt.test_video(url, location, runs, mobile, firstViewOnly, private, video, pollResults, timeout, function(err, data) {27 if(err) {28 console.log(err);29 } else {30 console.log(data);31 }32});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.log(err);4 console.log(data);5});6{ responseCode: 200,7 { firstView: 8 { SpeedIndex: 1826,9 score: 100 },10 { SpeedIndex: 1826,
Using AI Code Generation
1var WPT = require('webpagetest');2var wpt = new WPT('API_KEY');3 if (err) {4 console.log(err);5 } else {6 wpt.testStatus(data.data.testId, function(err, data) {7 if (err) {8 console.log(err);9 } else {10 console.log(data);11 }12 });13 }14});
Using AI Code Generation
1 console.log("callback: " + data);2});3 console.log("callback: " + data);4}, 1);5 console.log("callback: " + data);6}, 1, "my label");7 console.log("callback: " + data);8}, 1, "my label", 1000);9 console.log("callback: " + data);10}, 1, "my label", 1000, true);11 console.log("callback: " + data);12}, 1, "my label", 1000, true, true);13 console.log("callback: " + data);
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!!