Best JavaScript code snippet using taiko
MeanTelemetryProviderSpec.js
Source:MeanTelemetryProviderSpec.js
...57 return expectObjectWasSubscribedTo(associatedObject);58 });59 it("returns a function that unsubscribes from the associated object", function () {60 var unsubscribe = meanTelemetryProvider.subscribe(mockDomainObject);61 return waitForPromises()62 .then(unsubscribe)63 .then(waitForPromises)64 .then(function () {65 expect(mockApi.telemetry.unsubscribe).toHaveBeenCalled();66 });67 });68 it("returns an average only when the sample size is reached", function () {69 var inputTelemetry = [70 {'utc': 1, 'defaultRange': 123.1231},71 {'utc': 2, 'defaultRange': 321.3223},72 {'utc': 3, 'defaultRange': 111.4446},73 {'utc': 4, 'defaultRange': 555.2313}74 ];75 setSampleSize(5);76 meanTelemetryProvider.subscribe(mockDomainObject, subscriptionCallback);77 return waitForPromises()78 .then(feedInputTelemetry.bind(this, inputTelemetry))79 .then(function () {80 expect(subscriptionCallback).not.toHaveBeenCalled();81 });82 });83 it("correctly averages a sample of five values", function () {84 var inputTelemetry = [85 {'utc': 1, 'defaultRange': 123.1231},86 {'utc': 2, 'defaultRange': 321.3223},87 {'utc': 3, 'defaultRange': 111.4446},88 {'utc': 4, 'defaultRange': 555.2313},89 {'utc': 5, 'defaultRange': 1.1231}90 ];91 var expectedAverages = [{92 'utc': 5, 'value': 222.4488893 }];94 setSampleSize(5);95 meanTelemetryProvider.subscribe(mockDomainObject, subscriptionCallback);96 return waitForPromises()97 .then(feedInputTelemetry.bind(this, inputTelemetry))98 .then(expectAveragesForTelemetry.bind(this, expectedAverages));99 });100 it("correctly averages a sample of ten values", function () {101 var inputTelemetry = [102 {'utc': 1, 'defaultRange': 123.1231},103 {'utc': 2, 'defaultRange': 321.3223},104 {'utc': 3, 'defaultRange': 111.4446},105 {'utc': 4, 'defaultRange': 555.2313},106 {'utc': 5, 'defaultRange': 1.1231},107 {'utc': 6, 'defaultRange': 2323.12},108 {'utc': 7, 'defaultRange': 532.12},109 {'utc': 8, 'defaultRange': 453.543},110 {'utc': 9, 'defaultRange': 89.2111},111 {'utc': 10, 'defaultRange': 0.543}112 ];113 var expectedAverages = [{114 'utc': 10, 'value': 451.07815115 }];116 setSampleSize(10);117 meanTelemetryProvider.subscribe(mockDomainObject, subscriptionCallback);118 return waitForPromises()119 .then(feedInputTelemetry.bind(this, inputTelemetry))120 .then(expectAveragesForTelemetry.bind(this, expectedAverages));121 });122 it("only averages values within its sample window", function () {123 var inputTelemetry = [124 {'utc': 1, 'defaultRange': 123.1231},125 {'utc': 2, 'defaultRange': 321.3223},126 {'utc': 3, 'defaultRange': 111.4446},127 {'utc': 4, 'defaultRange': 555.2313},128 {'utc': 5, 'defaultRange': 1.1231},129 {'utc': 6, 'defaultRange': 2323.12},130 {'utc': 7, 'defaultRange': 532.12},131 {'utc': 8, 'defaultRange': 453.543},132 {'utc': 9, 'defaultRange': 89.2111},133 {'utc': 10, 'defaultRange': 0.543}134 ];135 var expectedAverages = [136 {'utc': 5, 'value': 222.44888},137 {'utc': 6, 'value': 662.4482599999999},138 {'utc': 7, 'value': 704.6078},139 {'utc': 8, 'value': 773.02748},140 {'utc': 9, 'value': 679.8234399999999},141 {'utc': 10, 'value': 679.70742}142 ];143 setSampleSize(5);144 meanTelemetryProvider.subscribe(mockDomainObject, subscriptionCallback);145 return waitForPromises()146 .then(feedInputTelemetry.bind(this, inputTelemetry))147 .then(expectAveragesForTelemetry.bind(this, expectedAverages));148 });149 describe("given telemetry input with range values", function () {150 var inputTelemetry;151 beforeEach(function () {152 inputTelemetry = [{153 'utc': 1,154 'rangeKey': 5678,155 'otherKey': 9999156 }];157 setSampleSize(1);158 });159 it("uses the 'rangeKey' input range, when it is the default, to calculate the average", function () {160 var averageTelemetryForRangeKey = [{161 'utc': 1,162 'value': 5678163 }];164 meanTelemetryProvider.subscribe(mockDomainObject, subscriptionCallback);165 mockApi.telemetry.setDefaultRangeTo('rangeKey');166 return waitForPromises()167 .then(feedInputTelemetry.bind(this, inputTelemetry))168 .then(expectAveragesForTelemetry.bind(this, averageTelemetryForRangeKey));169 });170 it("uses the 'otherKey' input range, when it is the default, to calculate the average", function () {171 var averageTelemetryForOtherKey = [{172 'utc': 1,173 'value': 9999174 }];175 meanTelemetryProvider.subscribe(mockDomainObject, subscriptionCallback);176 mockApi.telemetry.setDefaultRangeTo('otherKey');177 return waitForPromises()178 .then(feedInputTelemetry.bind(this, inputTelemetry))179 .then(expectAveragesForTelemetry.bind(this, averageTelemetryForOtherKey));180 });181 });182 describe("given telemetry input with range values", function () {183 var inputTelemetry;184 beforeEach(function () {185 inputTelemetry = [{186 'utc': 1,187 'rangeKey': 5678,188 'otherKey': 9999189 }];190 setSampleSize(1);191 });192 it("uses the 'rangeKey' input range, when it is the default, to calculate the average", function () {193 var averageTelemetryForRangeKey = [{194 'utc': 1,195 'value': 5678196 }];197 meanTelemetryProvider.subscribe(mockDomainObject, subscriptionCallback);198 mockApi.telemetry.setDefaultRangeTo('rangeKey');199 return waitForPromises()200 .then(feedInputTelemetry.bind(this, inputTelemetry))201 .then(expectAveragesForTelemetry.bind(this, averageTelemetryForRangeKey));202 });203 it("uses the 'otherKey' input range, when it is the default, to calculate the average", function () {204 var averageTelemetryForOtherKey = [{205 'utc': 1,206 'value': 9999207 }];208 meanTelemetryProvider.subscribe(mockDomainObject, subscriptionCallback);209 mockApi.telemetry.setDefaultRangeTo('otherKey');210 return waitForPromises()211 .then(feedInputTelemetry.bind(this, inputTelemetry))212 .then(expectAveragesForTelemetry.bind(this, averageTelemetryForOtherKey));213 });214 });215 function feedInputTelemetry(inputTelemetry) {216 inputTelemetry.forEach(mockApi.telemetry.mockReceiveTelemetry);217 }218 function expectAveragesForTelemetry(expectedAverages) {219 return waitForPromises().then(function () {220 expectedAverages.forEach(function (averageDatum) {221 expect(subscriptionCallback).toHaveBeenCalledWith(averageDatum);222 });223 });224 }225 function expectObjectWasSubscribedTo(object) {226 return waitForPromises().then(function () {227 expect(mockApi.telemetry.subscribe).toHaveBeenCalledWith(object, jasmine.any(Function));228 });229 }230 });231 describe("the request function", function () {232 it("requests telemetry for the associated object", function () {233 whenTelemetryRequestedReturn([]);234 return meanTelemetryProvider.request(mockDomainObject).then(function () {235 expect(mockApi.telemetry.request).toHaveBeenCalledWith(associatedObject, undefined);236 });237 });238 it("returns an average only when the sample size is reached", function () {239 var inputTelemetry = [240 {'utc': 1, 'defaultRange': 123.1231},241 {'utc': 2, 'defaultRange': 321.3223},242 {'utc': 3, 'defaultRange': 111.4446},243 {'utc': 4, 'defaultRange': 555.2313}244 ];245 setSampleSize(5);246 whenTelemetryRequestedReturn(inputTelemetry);247 return meanTelemetryProvider.request(mockDomainObject).then(function (averageData) {248 expect(averageData.length).toBe(0);249 });250 });251 it("correctly averages a sample of five values", function () {252 var inputTelemetry = [253 {'utc': 1, 'defaultRange': 123.1231},254 {'utc': 2, 'defaultRange': 321.3223},255 {'utc': 3, 'defaultRange': 111.4446},256 {'utc': 4, 'defaultRange': 555.2313},257 {'utc': 5, 'defaultRange': 1.1231}258 ];259 setSampleSize(5);260 whenTelemetryRequestedReturn(inputTelemetry);261 return meanTelemetryProvider.request(mockDomainObject)262 .then(function (averageData) {263 expectAverageToBe(222.44888, averageData);264 });265 });266 it("correctly averages a sample of ten values", function () {267 var inputTelemetry = [268 {'utc': 1, 'defaultRange': 123.1231},269 {'utc': 2, 'defaultRange': 321.3223},270 {'utc': 3, 'defaultRange': 111.4446},271 {'utc': 4, 'defaultRange': 555.2313},272 {'utc': 5, 'defaultRange': 1.1231},273 {'utc': 6, 'defaultRange': 2323.12},274 {'utc': 7, 'defaultRange': 532.12},275 {'utc': 8, 'defaultRange': 453.543},276 {'utc': 9, 'defaultRange': 89.2111},277 {'utc': 10, 'defaultRange': 0.543}278 ];279 setSampleSize(10);280 whenTelemetryRequestedReturn(inputTelemetry);281 return meanTelemetryProvider.request(mockDomainObject)282 .then(function (averageData) {283 expectAverageToBe(451.07815, averageData);284 });285 });286 it("only averages values within its sample window", function () {287 var inputTelemetry = [288 {'utc': 1, 'defaultRange': 123.1231},289 {'utc': 2, 'defaultRange': 321.3223},290 {'utc': 3, 'defaultRange': 111.4446},291 {'utc': 4, 'defaultRange': 555.2313},292 {'utc': 5, 'defaultRange': 1.1231},293 {'utc': 6, 'defaultRange': 2323.12},294 {'utc': 7, 'defaultRange': 532.12},295 {'utc': 8, 'defaultRange': 453.543},296 {'utc': 9, 'defaultRange': 89.2111},297 {'utc': 10, 'defaultRange': 0.543}298 ];299 setSampleSize(5);300 whenTelemetryRequestedReturn(inputTelemetry);301 return meanTelemetryProvider.request(mockDomainObject)302 .then(function (averageData) {303 expectAverageToBe(679.70742, averageData);304 });305 });306 function expectAverageToBe(expectedValue, averageData) {307 var averageDatum = averageData[averageData.length - 1];308 expect(averageDatum[RANGE_KEY]).toBe(expectedValue);309 }310 function whenTelemetryRequestedReturn(telemetry) {311 mockApi.telemetry.request.and.returnValue(resolvePromiseWith(telemetry));312 }313 });314 function createMockObjects() {315 mockDomainObject = {316 telemetryPoint: 'someTelemetryPoint'317 };318 associatedObject = {};319 mockApi.objects.get.and.returnValue(resolvePromiseWith(associatedObject));320 }321 function setSampleSize(sampleSize) {322 mockDomainObject.samples = sampleSize;323 }324 function createMockApi() {325 mockApi = {326 telemetry: new MockTelemetryApi(),327 objects: createMockObjectApi(),328 time: createMockTimeApi()329 };330 }331 function createMockObjectApi() {332 return jasmine.createSpyObj('ObjectAPI', [333 'get'334 ]);335 }336 function mockObjectWithType(type) {337 return {338 type: type339 };340 }341 function resolvePromiseWith(value) {342 var promise = Promise.resolve(value);343 allPromises.push(promise);344 return promise;345 }346 function waitForPromises() {347 return Promise.all(allPromises);348 }349 function createMockTimeApi() {350 return jasmine.createSpyObj("timeApi", ['timeSystem']);351 }352 function setTimeSystemTo(timeSystemKey) {353 mockApi.time.timeSystem.and.returnValue({354 key: timeSystemKey355 });356 }357 });...
async.ts
Source:async.ts
1import { ready } from 'utils';2/**3 * @description Sleep function.4 */5export const wait = (timeout: number): Promise<void> => new Promise((resolve) => setTimeout(() => resolve(), timeout));6/**7 * @description Awaits a promise until it is timed out or resolved.8 */9export const waitUntilTimeout = <T1, T2>(promise: Promise<T1>, timeout: number, defaultValue: T2 = undefined): Promise<T1 | T2> =>10 Promise.race([wait(timeout).then(() => defaultValue), promise]);11let waitForPromises: Promise<any>[];12/**13 * @description Wait until all promises are resolved before leaving the page.14 */15export const waitUntilBeforeUnload = <T>(promise: Promise<T>) => {16 if (!Array.isArray(waitForPromises)) {17 waitForPromises = [];18 ready(() =>19 document.querySelectorAll<HTMLAnchorElement>('a[href]').forEach((anchor) =>20 anchor.addEventListener('click', function (e) {21 e.preventDefault();22 if (waitForPromises.length > 0) {23 const newLocation = this.href;24 if (newLocation) {25 Promise.all(waitForPromises).then(() => (location.href = newLocation));26 return false;27 }28 }29 })30 )31 );32 }33 waitForPromises.push(promise);...
Using AI Code Generation
1const { openBrowser, goto, closeBrowser, waitFor } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await goto("google.com");6 await waitFor(2000);7 await closeBrowser();8 } catch (e) {9 console.error(e);10 } finally {11 }12})();
Using AI Code Generation
1const { openBrowser, goto, click, closeBrowser, waitFor } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await goto("google.com");6 await click("Sign in");7 await waitFor(5000);8 await click("Sign in");9 } catch (e) {10 console.error(e);11 } finally {12 await closeBrowser();13 }14})();15const { openBrowser, goto, click, closeBrowser, waitFor } = require('taiko');16(async () => {17 try {18 await openBrowser();19 await goto("google.com");20 await click("Sign in");21 await waitFor(5000);22 await click("Sign in");23 } catch (e) {24 console.error(e);25 } finally {26 await closeBrowser();27 }28})();29const { openBrowser, goto, click, closeBrowser, waitForNavigation } = require('taiko');30(async () => {31 try {32 await openBrowser();33 await goto("google.com");34 await click("Sign in");35 await waitForNavigation({timeout:5000});36 await click("Sign in");37 } catch (e) {38 console.error(e);39 } finally {40 await closeBrowser();41 }42})();43const { openBrowser, goto, click, closeBrowser, waitForNavigation } = require('taiko');44(async () => {45 try {46 await openBrowser();47 await goto("google.com");48 await click("Sign in");49 await waitForNavigation({timeout:5000});50 await click("Sign in");51 } catch (e) {52 console.error(e);53 } finally {54 await closeBrowser();55 }56})();57const { openBrowser, goto, click, closeBrowser, waitForNavigation } = require('taiko');58(async () => {59 try {60 await openBrowser();61 await goto("google.com");62 await click("Sign in");63 await waitForNavigation({timeout:5000});64 await click("Sign in");65 } catch (e) {66 console.error(e);67 } finally {68 await closeBrowser();69 }70})();71const { openBrowser, goto, click, closeBrowser,
Using AI Code Generation
1const { openBrowser, goto, click, closeBrowser, waitForPromises } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await click("Sign in");6 await waitForPromises();7 } catch (e) {8 console.error(e);9 } finally {10 await closeBrowser();11 }12})();13const { openBrowser, goto, click, closeBrowser, waitFor } = require('taiko');14(async () => {15 try {16 await openBrowser();17 await click("Sign in");18 await waitFor(10000);19 } catch (e) {20 console.error(e);21 } finally {22 await closeBrowser();23 }24})();
Using AI Code Generation
1const { openBrowser, goto, button, waitForPromises, closeBrowser } = require('taiko');2(async () => {3 try {4 await openBrowser({ headless: false });5 await button('Google Search').exists();6 await waitForPromises();7 } catch (e) {8 console.error(e);9 } finally {10 await closeBrowser();11 }12})();13const { openBrowser, goto, button, waitFor, closeBrowser } = require('taiko');14(async () => {15 try {16 await openBrowser({ headless: false });17 await button('Google Search').exists();18 await waitFor(2000);19 } catch (e) {20 console.error(e);21 } finally {22 await closeBrowser();23 }24})();25const { openBrowser, goto, button, waitForEvent, closeBrowser } = require('taiko');26(async () => {27 try {28 await openBrowser({ headless: false });29 await button('Google Search').exists();30 await waitForEvent('DOMContentLoaded');31 } catch (e) {32 console.error(e);33 } finally {34 await closeBrowser();35 }36})();37const { openBrowser, goto, button, waitForNavigation, closeBrowser } = require('taiko');38(async () => {39 try {40 await openBrowser({ headless: false });41 await button('Google Search').exists();42 await waitForNavigation();43 } catch (e) {44 console.error(e);45 } finally {46 await closeBrowser();47 }48})();49const { openBrowser, goto, button, waitForSelector, closeBrowser } = require('taiko');50(async () => {51 try {52 await openBrowser({ headless: false });53 await button('Google Search').exists();54 await waitForSelector('button');55 } catch (e) {56 console.error(e);57 } finally {58 await closeBrowser();59 }60})();61const { openBrowser, goto,
Using AI Code Generation
1const { openBrowser, goto, click, closeBrowser, waitForPromises, write } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await write("Taiko");6 await click("Google Search");7 await waitForPromises();8 } catch (e) {9 console.error(e);10 } finally {11 await closeBrowser();12 }13})();14> openBrowser()15> write("Taiko")16> click("Google Search")17> waitForPromises()18> closeBrowser()19waitForNavigation(options, [timeout])20const { openBrowser, goto, click, closeBrowser, waitForNavigation } = require('taiko');21(async () => {22 try {23 await openBrowser();24 await click("Taiko");25 await waitForNavigation();26 } catch (e) {27 console.error(e);28 } finally {29 await closeBrowser();30 }31})();32> openBrowser()33> click("Taiko")34> waitForNavigation()35> closeBrowser()36waitForEvent(event, [options], [timeout])
Using AI Code Generation
1const { openBrowser, goto, click, textBox, write, closeBrowser, waitFor, waitForPromises } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await write("Taiko");6 await click("Google Search");7 await waitFor(3000);8 await waitForPromises();9 await click("Taiko");10 await waitFor(3000);11 await waitForPromises();12 } catch (e) {13 console.error(e);14 } finally {15 await closeBrowser();16 }17})();18const { openBrowser, goto, click, textBox, write, closeBrowser, waitFor, waitForNavigation } = require('taiko');19(async () => {20 try {21 await openBrowser();22 await write("Taiko");23 await click("Google Search");24 await waitFor(3000);25 await waitForNavigation();26 await click("Taiko");27 await waitFor(3000);28 await waitForNavigation();29 } catch (e) {30 console.error(e);31 } finally {32 await closeBrowser();33 }34})();35const { openBrowser, goto, click, textBox, write, closeBrowser, waitFor, waitForNavigation } = require('taiko');36(async () => {37 try {38 await openBrowser();39 await write("Taiko");40 await click("Google Search");41 await waitFor(3000);42 await waitForNavigation();43 await click("Taiko");44 await waitFor(3000);45 await waitForNavigation();46 } catch (e) {47 console.error(e);48 } finally {49 await closeBrowser();50 }51})();52const { openBrowser, goto, click, textBox, write, closeBrowser, waitFor, waitForNavigation } = require('taiko');53(async () => {54 try {55 await openBrowser();56 await write("Taiko");57 await click("Google Search");58 await waitFor(3000);59 await waitForNavigation();60 await click("Taiko");61 await waitFor(3000);
Using AI Code Generation
1const { openBrowser, goto, closeBrowser, waitFor,waitForPromises } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await waitFor(10000);6 await waitForPromises();7 await closeBrowser();8 } catch (e) {9 console.error(e);10 } finally {11 }12})();13const { openBrowser, goto, closeBrowser, waitFor,waitForPromises } = require('taiko');14(async () => {15 try {16 await openBrowser();17 await waitFor(10000);18 await waitFor(10000);19 await closeBrowser();20 } catch (e) {21 console.error(e);22 } finally {23 }24})();25const { openBrowser, goto, closeBrowser, waitFor,waitForPromises } = require('taiko');26(async () => {27 try {28 await openBrowser();29 await waitFor(10000);30 await waitFor(10000);31 await waitFor(10000);32 await closeBrowser();33 } catch (e) {34 console.error(e);35 } finally {36 }37})();38const { openBrowser, goto, closeBrowser, waitFor,waitForPromises } = require('taiko');39(async () => {40 try {41 await openBrowser();42 await waitFor(10000);43 await waitFor(10000);44 await waitFor(10000);45 await waitFor(10000);46 await closeBrowser();47 } catch (e) {48 console.error(e);49 } finally {50 }51})();52const { openBrowser, goto, closeBrowser, waitFor,waitForPromises } = require('taiko');53(async () => {54 try {55 await openBrowser();56 await waitFor(10000);57 await waitFor(10000);58 await waitFor(10000);59 await waitFor(10000);60 await waitFor(10000);61 await closeBrowser();62 }
Using AI Code Generation
1const { openBrowser, goto, closeBrowser, waitFor, text, $, click, waitForPromises } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await click("Sign in");6 await waitFor(2000);7 await waitForPromises();8 await click("Create account");9 await waitFor(2000);10 await waitForPromises();11 await click("Next");12 await waitFor(2000);13 await waitForPromises();14 await click("No thanks");15 await waitFor(2000);16 await waitForPromises();17 await click("Use my current email address instead");18 await waitFor(2000);19 await waitForPromises();
Using AI Code Generation
1const taiko = require('taiko');2(async () => {3 try {4 await openBrowser();5 await write("Taiko");6 await waitFor(5000);7 await click("Google Search");8 await waitFor(5000);9 await click("Taiko - Test Automation Framework");10 await waitFor(5000);11 await click("Documentation");12 await waitFor(5000);13 await click("API");14 await waitFor(5000);15 await click("waitFor");16 await waitFor(5000);17 await click("Examples");18 await waitFor(
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!!