Best JavaScript code snippet using wpt
test_resource_timing.js
Source:test_resource_timing.js
...82 break;83 case "xmlhttprequest":84 xmlhttp.onload = (function(entry) {85 return function (event) {86 resource_load(entry);87 };88 })(expected_entry);89 break;90 default:91 element.onload = (function(entry) {92 return function (event) {93 resource_load(entry);94 };95 })(expected_entry);96 document.body.appendChild(element);97 }98 }99};100function poll_for_stylesheet_load(expected_entry) {101 function inner() {102 for(var i=0; i<document.styleSheets.length; i++) {103 var sheet = document.styleSheets[i];104 if (sheet.href === expected_entry.name) {105 try {106 // try/catch avoids throwing if sheet object exists before it is loaded,107 // which is a bug, but not what we are trying to test here.108 var hasRules = sheet.cssRules.length > 0;109 } catch(e) {110 hasRules = false;111 }112 if (hasRules) {113 setTimeout(function() {114 resource_load(expected_entry);115 }, 200);116 return;117 }118 }119 }120 setTimeout(inner, 100);121 }122 inner();123}124function resource_load(expected)125{126 var t = tests[expected.initiatorType];127 t["entry"].step(function() {128 var entries_by_name = window.performance.getEntriesByName(expected.name);129 assert_equals(entries_by_name.length, 1, "should have a single entry for each resource (without type)");130 var entries_by_name_type = window.performance.getEntriesByName(expected.name, "resource");131 assert_equals(entries_by_name_type.length, 1, "should have a single entry for each resource (with type)");132 assert_not_equals(entries_by_name, entries_by_name_type, "values should be copies");133 for (p in entries_by_name[0]) {134 assert_equals(entries_by_name[0][p], entries_by_name_type[0][p], "Property " + p + " should match");135 }136 this.done();137 });138 t["simple_attrs"].step(function() {...
reducer.js
Source:reducer.js
1import cloneDeep from 'lodash/cloneDeep'2import get from 'lodash/get'3import {makeInstanceAction} from '#/main/app/store/actions'4import {combineReducers, makeReducer} from '#/main/app/store/reducer'5import {makeFormReducer} from '#/main/app/content/form/store/reducer'6import {makeListReducer} from '#/main/app/content/list/store'7import {constants as listConst} from '#/main/app/content/list/constants'8import {RESOURCE_LOAD} from '#/main/core/resource/store/actions'9import {10 BINDER_TAB_LOAD,11 BINDER_LOAD,12 DOCUMENT_LOAD13} from '~/sidpt/ipip-binder-bundle/plugin/binder/resources/binder/store/actions'14import {selectors} from '~/sidpt/ipip-binder-bundle/plugin/binder/resources/binder/store/selectors'15import {selectors as editorSelectors} from '#/main/core/resources/directory/editor/store/selectors'16import {reducer as editorReducer} from '#/main/core/resources/directory/editor/store/reducer'17import {selectors as playerSelectors} from '#/main/core/resources/directory/player/store/selectors'18import {DIRECTORIES_LOAD, DIRECTORY_TOGGLE_OPEN} from '#/main/core/resources/directory/store/actions'19import {selectors as directorySelectors} from '#/main/core/resources/directory/store/selectors'20/**21 * Replaces a directory data inside the directories tree.22 *23 * @param {Array} directories - the directory tree24 * @param {object} newDir - the new directory data25 *26 * @return {Array} - the updated directories tree27 */28function replaceDirectory(directories, newDir) {29 for (let i = 0; i < directories.length; i++) {30 if (directories[i].id === newDir.id) {31 const updatedDirs = cloneDeep(directories)32 updatedDirs[i] = newDir33 return updatedDirs34 } else if (directories[i].children) {35 const updatedDirs = cloneDeep(directories)36 updatedDirs[i].children = replaceDirectory(directories[i].children, newDir)37 return updatedDirs38 }39 }40 return directories41}42/**43 * Notes : 44 * The binder subobject is generated by the BinderListener object45 * the content of this subobject is created by the serializer46 * 47 */48const reducer = combineReducers({49 // For the binder edition50 binder: makeFormReducer(selectors.FORM_NAME, {}, {51 data: makeReducer({}, {52 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: (state, action) => action.resourceData53 }),54 originalData: makeReducer({}, {55 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: (state, action) => action.resourceData56 })57 }),58 displayedTabs:makeReducer([], {59 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: (state, action) => action.resourceData.binder.tabs,60 [BINDER_LOAD]: (state, action) => {61 return action.response.binder.tabs62 }63 }),64 displayedDocument:makeReducer(null, {65 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: (state, action) => {66 return action.resourceData.displayedDocument67 },68 [DOCUMENT_LOAD]: (state, action) => {69 return action.response70 }71 }),72 // From directory reducer73 directory: makeReducer({}, {74 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: (state, action) => action.resourceData.directory75 }),76 /**77 * The list of available directories.78 *79 * NB. Each level is loaded on demand when the user uses directories nav,80 * so you can not assert this contains the full directories list.81 */82 directories: makeReducer([], {83 [DIRECTORIES_LOAD]: (state, action) => {84 if (!action.parentId) {85 return action.directories86 }87 const updatedParent = cloneDeep(selectors.directory(state, action.parentId))88 if (updatedParent) {89 // set parent children90 updatedParent._loaded = true91 updatedParent.children = action.directories92 return replaceDirectory(state, updatedParent)93 }94 return state95 },96 [DIRECTORY_TOGGLE_OPEN]: (state, action) => {97 const toToggle = cloneDeep(selectors.directory(state, action.directoryId))98 if (toToggle) {99 toToggle._opened = action.opened100 return replaceDirectory(state, toToggle)101 }102 return state103 }104 }),105 /**106 * The list of the resources of the current directory.107 */108 resources: makeListReducer(selectors.LIST_NAME, {}, {109 invalidated: makeReducer(false, {110 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: () => true111 }),112 selected: makeReducer([], {113 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: () => []114 }),115 filters: makeReducer([], {116 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: (state, action) => get(action.resourceData.directory, 'list.filters') || []117 }),118 page: makeReducer([], {119 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: () => 0120 }),121 pageSize: makeReducer([], {122 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: (state, action) => get(action.resourceData.directory, 'list.pageSize') || listConst.DEFAULT_PAGE_SIZE123 }),124 sortBy: makeReducer([], {125 [makeInstanceAction(RESOURCE_LOAD, selectors.STORE_NAME)]: (state, action) => {126 const sorting = get(action.resourceData.directory, 'list.sorting')127 let sortBy = {property: null, direction: 0}128 if (sorting) {129 if (0 === sorting.indexOf('-')) {130 sortBy.property = sorting.replace('-', '') // replace first -131 sortBy.direction = -1132 } else {133 sortBy.property = sorting134 sortBy.direction = 1135 }136 }137 return sortBy138 }139 })140 })141})142export {143 reducer...
resources.js
Source:resources.js
1import {API} from '../../api.js';2const baseUrl = API.url + '/resources';3const TYPE_PREFIX = 'RESOURCE';4const types = {5 RESOURCE_LOAD: '${TYPE_PREFIX}_LOAD',6 RESOURCE_LOAD_SUCCESS: '${TYPE_PREFIX}_LOAD_SUCCESS',7 RESOURCE_LOAD_FAILED: '${TYPE_PREFIX}_LOAD_FAILED',8 RESOURCE_ADD: '${TYPE_PREFIX}_ADD',9 RESOURCE_ADD_SUCCESS: '${TYPE_PREFIX}_ADD_SUCCESS',10 RESOURCE_ADD_FAILED: '${TYPE_PREFIX}_ADD_FAILED',11}12const state = {13 isLoading: false,14 resource: {15 title: ''16 },17 resources: []18};19const getters = {20 isLoadingResources: state => state.isLoading,21 resource: state => state.resource,22 getAllResources: state => state.resources23};24const actions = {25 loadAllResources({ commit }) {26 return new Promise((resolve, reject) => {27 commit(types.RESOURCE_LOAD);28 fetch(baseUrl, {29 method: 'GET'30 })31 .then(data => data.json())32 .then(response => {33 commit(types.RESOURCE_LOAD_SUCCESS, response)34 resolve();35 })36 .catch(err => {37 console.error(err);38 commit(types.RESOURCE_LOAD_FAILED);39 reject();40 });41 });42 },43 getResource({ commit }, id) {44 },45 addResource({ commit, state }, resourceTitle) {46 commit(types.RESOURCE_ADD, resourceTitle);47 fetch(baseUrl, {48 method: 'POST',49 body: JSON.stringify({ title: resourceTitle })50 })51 .then(response => commit(types.RESOURCE_ADD_SUCCESS, response))52 .catch(err => {53 console.error(err);54 commit(types.RESOURCE_ADD_FAILED, state);55 });56 },57 updateResource({ commit }, data) {58 },59 deleteResource({ commit }, id) {60 }61};62const mutations = {63 loadingState(state, { isLoading }) {64 state.isLoading = isLoading;65 },66 [types.RESOURCE_LOAD] (state) {67 state.isLoading = true;68 },69 [types.RESOURCE_LOAD_SUCCESS] (state, resources) {70 state.resources = resources;71 state.isLoading = false;72 },73 [types.RESOURCE_LOAD_FAILED] (state) {74 state.isLoading = false;75 },76 [types.RESOURCE_ADD] (state, resource) {77 state.resources.push(resource);78 },79 [types.RESOURCE_ADD_SUCCESS] (state, response) {80 // console.log(response);81 },82 [types.RESOURCE_ADD_FAILED] (state, oldState) {83 state = oldState;84 },85 updateResource(state, data) {86 },87 deleteResource(state, data) {88 89 }90};91export default {92 state,93 getters,94 actions,95 mutations...
Using AI Code Generation
1var wpt = require('wpt');2var options = {3};4var wptTest = new wpt('www.webpagetest.org', options);5 if (err) return console.log(err);6 console.log(data);7 wptTest.getTestResults(function(err, data) {8 if (err) return console.log(err);9 console.log(data);10 });11});12### wpt.getTestResults(callback)
Using AI Code Generation
1var wpt = require('webpagetest');2var api_key = process.argv[3];3var url = process.argv[2];4var wpt = new WebPageTest('www.webpagetest.org', api_key);5wpt.runTest(url, function(err, data) {6 if (err) return console.log(err);7 var testId = data.data.testId;8 wpt.getTestResults(testId, function(err, data) {9 if (err) return console.log(err);10 console.log(data.data.average.firstView.TTFB);11 console.log(data.data.average.firstView.render);12 console.log(data.data.average.firstView.loadTime);13 console.log(data.data.average.firstView.docTime);14 console.log(data.data.average.firstView.fullyLoaded);15 console.log(data.data.average.firstView.SpeedIndex);16 console.log(data.data.average.firstView.requests);17 console.log(data.data.average.firstView.bytesIn);18 console.log(data.data.average.firstView.bytesOut);19 console.log(data.data.average.firstView.bytesInDoc);20 console.log(data.data.average.firstView.bytesOutDoc);21 console.log(data.data.average.firstView.connections);22 console.log(data.data.average.firstView.requestsDoc);23 console.log(data.data.average.firstView.responses_200);24 console.log(data.data.average.firstView.responses_404);25 console.log(data.data.average.firstView.responses_other);26 console.log(data.data.average.firstView.result);27 console.log(data.data.average.firstView.run);28 console.log(data.data.average.firstView.completed);29 console.log(data.data.average.firstView.testStartOffset);30 console.log(data.data.average.firstView.loadStart);31 console.log(data.data.average.firstView.domContentLoadedEventStart);32 console.log(data.data.average.firstView.domContentLoadedEventEnd);33 console.log(data.data.average.firstView.domInteractive);34 console.log(data.data.average.firstView.domLoading);35 console.log(data.data.average.firstView.domElement);36 console.log(data.data.average.firstView.domComplete);37 console.log(data.data.average.firstView.loadEventStart);38 console.log(data.data.average.firstView.loadEventEnd);39 console.log(data.data.average.firstView.lastVisualChange);40 console.log(data.data.average.firstView.SpeedIndex);
Using AI Code Generation
1var method = "GET";2var headers = "";3var postData = "";4wptdriver.resource_load(url, method, headers, postData, function(data) {5 alert(data);6});
Using AI Code Generation
1var wptdriver = require('./wptdriver.js');2var pageSpeed = require('./pageSpeed.js');3 console.log("resource loaded: " + data);4});5 console.log("resource loaded: " + data);6});7 console.log("resource loaded: " + data);8});9 console.log("resource loaded: " + data);10});11 console.log("resource loaded: " + data);12});13 console.log("resource loaded: " + data);14});15 console.log("resource loaded: " + data);16});17 console.log("resource loaded: " + data);18});19 console.log("resource loaded: " + data);20});21 console.log("resource loaded: " + data);22});23 console.log("resource loaded: " + data);24});25 console.log("resource loaded: " + data);26});27 console.log("resource loaded: " + data);28});29 console.log("resource loaded: " + data);30});31 console.log("resource loaded: " + data);32});
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest('www.webpagetest.org', 'A.9b9e1f4d4e8d4d4e6d4e6d4e6d4e6d4e6d');5}, function(err, data) {6 if (err) return console.error(err);7 console.log('Test submitted. Polling for results...');8 wpt.getTestResults(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log('View your test at %s', data.data.userUrl);11 });12});
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!!