Best JavaScript code snippet using testcafe
stringtemplate_basic.js
Source:stringtemplate_basic.js
1//-------------------------------------------------------------------------------------------------------2// Copyright (C) Microsoft. All rights reserved.3// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.4//-------------------------------------------------------------------------------------------------------5function echo(str) {6 WScript.Echo(str);7}8function printArr(arr, name) {9 for(var i in arr) {10 echo(`${name}[${i}]=${arr[i]}`);11 }12}13function tag(callSiteObj, sub) {14 printArr(callSiteObj, "callsiteObj");15 printArr(callSiteObj.raw, "callsiteObj.raw");16 echo(sub);17}18function simpleTag(callSiteObj, sub1, sub2, sub3, sub4) {19 var str = '';20 for(var i in callSiteObj) {21 if(i == 'raw') {22 continue;23 }24 str += callSiteObj[i];25 if(i==0 && sub1) {26 str += sub1;27 } else if(i==1 && sub2) {28 str += sub2;29 } else if(i==2 && sub3) {30 str += sub3;31 } else if(i==3 && sub4) {32 str += sub4;33 }34 }35 return str;36}37function simpleRawTag(callSiteObj, sub1, sub2, sub3, sub4) {38 var str = '';39 for(var i in callSiteObj.raw) {40 str += callSiteObj.raw[i];41 if(i==0 && sub1) {42 str += sub1;43 } else if(i==1 && sub2) {44 str += sub2;45 } else if(i==2 && sub3) {46 str += sub3;47 } else if(i==3 && sub4) {48 str += sub4;49 }50 }51 return str;52}53echo("Tag function takes less formal arguments than string template produces:");54tag`str1 ${'str2'} str3 ${'str4'} str5`;55tag`str1 ${'str2'} str3 ${'str4'} str5 ${'str6'} str7`;56echo("\nSimple tag function which assembles arguments:");57echo(simpleTag`str1 ${'str2'} str3 ${'str4'} str5 ${'str6'} str7 ${'str8'} str9`);58echo("\nSimple tag function which assembles raw arguments:");59echo(simpleRawTag`str1 ${'str2'} str3 ${'str4'} str5 ${'str6'} str7 ${'str8'} str9`);60function timesThirty(arg) {61 return arg * 30;62}63echo("\nSubstitution which calls a function:");64echo(`thirty = ${timesThirty(1)}. sixty = ${timesThirty(2)}.`);65echo("\nFunction which returns a template literal:");66function add(i, j) {67 return `${i + j}`;68}69echo(add(5,3));70echo(add(500, 100));71echo(add(`${add('hello'," world ")}`,1));72echo("\nSubstitution calls a built-in operator:");73echo(`typeof 1 == ${typeof 1}.`);74echo("\nNested templates with substitutions:");75function getString() { return `funcreturn`; }76echo(`this is a nested ${`(nested template ${getString()}!)`} string template... ${getString()}s!`);77echo("");78// Make sure we don't get spurious assignment tracking when incrementing a tagged template79try {80 Function('function x(){}--++ x`${x--}`;')();81}82catch(e) {83}84function getCallSite(siteObj) {85 return siteObj;86}87function verifyCallSite(siteObj, cookedVals, rawVals) {88 var success = true;89 echo("Verifying siteObj against a known set of cooked and raw values...");90 for(var i = 0; i < siteObj.length; i++) {91 if(siteObj[i] !== cookedVals[i]) {92 echo(`FAILED! (expected = ${cookedVals[i]})`);93 success = false;94 }95 }96 for(var i = 0; i < siteObj.raw.length; i++) {97 if(siteObj.raw[i] !== rawVals[i]) {98 echo(`FAILED! (expected = ${rawVals[i]})`);99 success = false;100 }101 }102 if(success) {103 echo(`PASSED104`);105 } else {106 echo(`FAILED107`);108 }109}110verifyCallSite(getCallSite`somestring`, ["somestring"], ["somestring"]);111verifyCallSite(getCallSite`\\`, ["\\"], ["\\\\"]);112verifyCallSite(getCallSite``, [""], [""]);113verifyCallSite(getCallSite`\u00b1`, ["\u00b1"], ["\\u00b1"]);114verifyCallSite(getCallSite`\ua48c`, ["\ua48c"], ["\\ua48c"]);115verifyCallSite(getCallSite`\u0023`, ["\u0023"], ["\\u0023"]);116verifyCallSite(getCallSite`\xF8`, ["\xF8"], ["\\xF8"]);117verifyCallSite(getCallSite`\h`, ["\h"], ["\\h"]);118verifyCallSite(getCallSite`\"`, ["\""], ["\\\""]);119verifyCallSite(getCallSite`\n`, ["\n"], ["\\n"]);120verifyCallSite(getCallSite`\r`, ["\r"], ["\\r"]);121verifyCallSite(getCallSite`\r\n`, ["\r\n"], ["\\r\\n"]);122verifyCallSite(getCallSite`\r\n\r\n`, ["\r\n\r\n"], ["\\r\\n\\r\\n"]);123verifyCallSite(getCallSite`\n\n\n\n\n\n\n\n\n\n`, ["\n\n\n\n\n\n\n\n\n\n"], ["\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n"]);124verifyCallSite(getCallSite`$`, ["$"], ["$"]);125verifyCallSite(getCallSite`\"bunch of escape chars \v\t\n\b\a\"`,126["\"bunch of escape chars \v\t\n\b\a\""],127["\\\"bunch of escape chars \\v\\t\\n\\b\\a\\\""]);128verifyCallSite(getCallSite`rest of escape chars \b\t\f\r`,129["rest of escape chars \b\t\f\r"],130["rest of escape chars \\b\\t\\f\\r"]);131verifyCallSite(getCallSite`\132`, [""], ["\\\n"]);133verifyCallSite(getCallSite`134`, ["\n"], ["\n"]);135verifyCallSite(getCallSite`136`, ["\n"], ["\n"]);137verifyCallSite(getCallSite`\138`, [""], ["\\\n"]);139verifyCallSite(getCallSite`140`, ["\n\n"], ["\n\n"]);141verifyCallSite(getCallSite`â¨`, ["\u2028"], ["\u2028"]);142verifyCallSite(getCallSite`â©`, ["\u2029"], ["\u2029"]);143verifyCallSite(getCallSite`$ $ $ {} } { }} {{`, ["$ $ $ {} } { }} {{"], ["$ $ $ {} } { }} {{"]);144// String.raw must handle embedded null characters145function nullCharTest() {146 function escapeNullCharacters(s) {147 return (s+'').replace('\0', '\\0');148 }149 var s = String.raw`string literal pre ${'string expression pre \0 string expression post'} string literal post`;150 return escapeNullCharacters(s);151}...
get_callsite.test.js
Source:get_callsite.test.js
...3import getCallsite from '../get_callsite';4jest.mock('fs');5describe('getCallsite', () => {6 test('without source map', () => {7 const site = getCallsite(0);8 expect(site.getFileName()).toEqual(__filename);9 expect(site.getColumnNumber()).toEqual(expect.any(Number));10 expect(site.getLineNumber()).toEqual(expect.any(Number));11 expect(fs.readFileSync).not.toHaveBeenCalled();12 });13 test('ignores errors when fs throws', () => {14 fs.readFileSync.mockImplementation(() => {15 throw new Error('Mock error');16 });17 const site = getCallsite(0, {[__filename]: 'mockedSourceMapFile'});18 expect(site.getFileName()).toEqual(__filename);19 expect(site.getColumnNumber()).toEqual(expect.any(Number));20 expect(site.getLineNumber()).toEqual(expect.any(Number));21 expect(fs.readFileSync).toHaveBeenCalledWith('mockedSourceMapFile', 'utf8');22 });23 test('reads source map file to determine line and column', () => {24 fs.readFileSync.mockImplementation(() => 'file data');25 const sourceMapColumn = 1;26 const sourceMapLine = 2;27 SourceMap.SourceMapConsumer = class {28 originalPositionFor(params) {29 expect(params).toMatchObject({30 column: expect.any(Number),31 line: expect.any(Number),32 });33 return {34 column: sourceMapColumn,35 line: sourceMapLine,36 };37 }38 };39 const site = getCallsite(0, {[__filename]: 'mockedSourceMapFile'});40 expect(site.getFileName()).toEqual(__filename);41 expect(site.getColumnNumber()).toEqual(sourceMapColumn);42 expect(site.getLineNumber()).toEqual(sourceMapLine);43 expect(fs.readFileSync).toHaveBeenCalledWith('mockedSourceMapFile', 'utf8');44 });...
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My test', async t => {3 .click(Selector('input').withAttribute('type', 'checkbox'));4});5import { Selector } from 'testcafe';6test('My test', async t => {7 .click(Selector('input').withAttribute('type', 'checkbox'));8});9import { Selector } from 'testcafe';10test('My test', async t => {11 .click(Selector('input').withAttribute('type', 'checkbox'));12});13import { Selector } from 'testcafe';14test('My test', async t => {15 .click(Selector('input').withAttribute('type', 'checkbox'));16});17import { Selector } from 'testcafe';18test('My test', async t => {19 .click(Selector('input').withAttribute('type', 'checkbox'));20});21import { Selector } from 'testcafe';22test('My test', async t => {23 .click(Selector('input').withAttribute('type', 'checkbox'));24});25import { Selector } from 'testcafe';26test('My test', async t => {27 .click(Selector('input').withAttribute
Using AI Code Generation
1import { getCallsite } from 'testcafe';2test('My Test', async t => {3 const callsite = getCallsite();4 console.log(callsite.getFileName());5 console.log(callsite.getLineNumber());6});7import { getCallsite } from 'testcafe';8test('My Test', async t => {9 const callsite = getCallsite();10 console.log(callsite.getFileName());11 console.log(callsite.getLineNumber());12});13import { getCallsite } from 'testcafe';14test('My Test', async t => {15 const callsite = getCallsite();16 console.log(callsite.getFileName());17 console.log(callsite.getLineNumber());18});19import { getCallsite } from 'testcafe';20test('My Test', async t => {21 const callsite = getCallsite();22 console.log(callsite.getFileName());23 console.log(callsite.getLineNumber());24});25import { getCallsite } from 'testcafe';26test('My Test', async t => {27 const callsite = getCallsite();28 console.log(callsite.getFileName());29 console.log(callsite.getLineNumber());30});31import { getCallsite } from 'testcafe';32test('My Test', async t => {33 const callsite = getCallsite();
Using AI Code Generation
1import { getCallsite } from 'testcafe';2import { Selector } from 'testcafe';3test('test', async t => {4 const callsite = getCallsite();5 console.log(callsite);6 .click(Selector('#populate'))7 .click(Selector('#submit-button'));8});9import { Selector } from 'testcafe';10test('test', async t => {11 .click(Selector('#populate'))12 .click(Selector('#submit-button'));13});
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My Test', async t => {3 await t.click(Selector('input').withAttribute('value', 'I have tried TestCafe'));4 console.log(Selector('input').withAttribute('value', 'I have tried TestCafe').getCallsite());5});6import { Selector } from 'testcafe';7test('My Test', async t => {8 await t.click(Selector('input').withAttribute('value', 'I have tried TestCafe'));9 console.log(Selector('input').withAttribute('value', 'I have tried TestCafe').getCallsite());10});11import { Selector } from 'testcafe';12test('My Test', async t => {13 await t.click(Selector('input').withAttribute('value', 'I have tried TestCafe'));14 console.log(Selector('input').withAttribute('value', 'I have tried TestCafe').getCallsite());15});16import { Selector } from 'testcafe';17test('My Test', async t => {18 await t.click(Selector('input').withAttribute('value', 'I have tried TestCafe'));19 console.log(Selector('input').withAttribute('value', 'I have tried TestCafe').getCallsite());20});21import { Selector } from '
Using AI Code Generation
1import { Selector } from 'testcafe';2import { getCallsite } from 'testcafe/lib/errors/test-run/templates';3test('Getting Callsite', async t => {4 const callsite = getCallsite();5 console.log(callsite);6});7const stackTrace = require('stack-trace');8const callsite = stackTrace.get();9console.log(callsite);10import { Selector } from 'testcafe';11const stackTrace = require('stack-trace');12test('Getting Callsite', async t => {13 const callsite = stackTrace.get();14 console.log(callsite);15});16import { Selector } from 'testcafe';17const stackTrace = require('stack-trace');18test('Getting Callsite', async t => {19 Error.stackTraceLimit = 100;20 const callsite = stackTrace.get();21 console.log(callsite);22});23import { Selector } from 'testcafe';24const stackTrace = require('stack-trace');25test('Getting Callsite', async t => {26 Error.stackTraceLimit = 100;27 const callsite = stackTrace.get();28 console.log(callsite);29});30import { Selector } from 'testcafe';31const stackTrace = require('stack-trace');
Using AI Code Generation
1import { Selector, t } from 'testcafe';2import { getCallsite } from './get-callsite.js';3test('My Test', async t => {4 const callsite = getCallsite();5 .typeText('#developer-name', 'John Smith')6 .click('#windows')7 .click('#tried-test-cafe')8 .click(Selector('label').withText('JavaScript API'))9 .click('#submit-button')10 .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!', callsite);11});12import { CallsiteRecord } from 'testcafe';13export function getCallsite (): CallsiteRecord {14 const err = new Error();15 Error.prepareStackTrace = (err, stack) => stack;16 const stack = err.stack as CallsiteRecord[];17 return stack[2];18}19import { Selector, t } from 'testcafe';20import { getCallsite } from '/Users/username/get-callsite.js';21test('My Test', async t => {22 const callsite = getCallsite();
Using AI Code Generation
1import { ReporterPluginHost } from 'testcafe';2const host = new ReporterPluginHost();3const callsite = host.getCallsite();4import { Runner } from 'testcafe';5const runner = new Runner();6const callsite = runner.getCallsite();7import { Selector } from 'testcafe';8test('Check property', async t => {9 const developerNameInput = Selector('#developer-name');10 .typeText(developerNameInput, 'Peter Parker')11 .expect(developerNameInput.value).eql('Peter Parker');12});
Using AI Code Generation
1import {t} from 'testcafe';2import {getCallsite} from 'testcafe';3const test = getCallsite();4console.log(test);5import {t} from 'testcafe';6import {getCallsite} from 'testcafe';7const test = getCallsite();8console.log(test);9import {t} from 'testcafe';10import {getCallsite} from 'testcafe';11const test = getCallsite();12console.log(test);13import {t} from 'testcafe';14import {getCallsite} from 'testcafe';15const test = getCallsite();16console.log(test);17import {t} from 'testcafe';18import {getCallsite} from 'testcafe';19const test = getCallsite();20console.log(test);21import {t} from 'testcafe';22import {getCallsite} from 'testcafe';23const test = getCallsite();24console.log(test);25import {t} from 'testcafe';26import {getCallsite} from 'testcafe';27const test = getCallsite();28console.log(test);29import {t} from 'testcafe';30import {getCallsite} from 'testcafe';31const test = getCallsite();32console.log(test);33import {t} from 'testcafe';34import {getCallsite} from 'testcafe';35const test = getCallsite();36console.log(test);37import {t} from 'testcafe';38import {getCallsite} from 'testcafe';39const test = getCallsite();40console.log(test);41import {t} from 'testcafe';42import {getCallsite} from 'testcafe';43const test = getCallsite();44console.log(test);
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!!