Best JavaScript code snippet using taiko
index.js
Source:index.js
1'use strict';2var displayText = '0';3var operator = '';4var firstNumber = 0, secondNumber = 0;5var subsequentOperation = false;6var maxDisplayChars = 10;7var mustClearAfterDivideByZero = false;8var performOperationStaged = false;9$(document).ready(init);10function init() {11 $(document).on('keypress', function(keyCharCode) {12 if(keyCharCode.which === 13) {13 buttonHit($('#lastrowbox'));14 inputHandler('=');15 }16 else {17 keyCharCode = String.fromCharCode(keyCharCode.which).toUpperCase();18 keyCharCode = fixKeyboardMapping(keyCharCode);19 var listItemHit = $('p:contains("' + keyCharCode + '")').closest('li');20 buttonHit(listItemHit);21 inputHandler(keyCharCode);22 }23 });24 $('li').click(function() {25 var valueOfClickedButton = $(this).find('p').text();26 buttonHit(this);27 inputHandler(valueOfClickedButton);28 });29}30function buttonHit(button) {31 if($(button).hasClass('orangebutton')) {32 $(button).toggleClass('orangebuttonhit');33 setTimeout(function() { $(button).toggleClass('orangebuttonhit'); }, 100);34 }35 else {36 $(button).addClass('graybuttonhit');37 setTimeout(function() { $(button).toggleClass('graybuttonhit'); }, 100);38 }39}40function inputHandler(char) {41 if(mustClearAfterDivideByZero) {42 clearDisplay(true);43 mustClearAfterDivideByZero = false;44 }45 if((char === 'C') || (char === 'AC')) {46 clearDisplay(true);47 }48 switch (char) {49 case '.':50 handleDecimal();51 break;52 case '+':53 setOperation(char);54 break;55 case 'â':56 setOperation(char);57 break;58 case '=':59 performOperation();60 break;61 case 'X':62 setOperation(char);63 break;64 case '÷':65 setOperation(char);66 break;67 case 'âºââ':68 negateValue();69 break;70 case '%':71 percentage();72 break;73 default:74 if(char.match(/\d/) !== null) {75 putNumberInBufferAndDisplay(char);76 }77 }78}79function putNumberInBufferAndDisplay(number) {80 if(displayText === '0') {81 displayText = number;82 $('#calcclear').text('C');83 }84 else if(displayText.length >= maxDisplayChars) {85 // do nothing... the display is full86 }87 else {88 displayText = displayText.concat(number);89 }90 $('#displaytext').text(displayText);91}92function handleDecimal () {93 if(displayText.indexOf('.') === -1) {94 displayText = displayText + '.';95 }96 $('#displaytext').text(displayText);97}98function clearDisplay(clearButton) {99 displayText = '0';100 $('#displaytext').text(displayText);101 if (clearButton) {102 $('#calcclear').text('AC');103 firstNumber = 0;104 secondNumber = 0;105 operator = 0;106 mustClearAfterDivideByZero = false;107 }108 subsequentOperation = false;109}110function setOperation(oper) {111 if(performOperationStaged === false) {112 if(displayText.length > 0) {113 if(subsequentOperation) {114 secondNumber = parseFloat(displayText);115 }116 else {117 firstNumber = parseFloat(displayText);118 }119 clearDisplay();120 }121 }122 operator = oper;123 performOperationStaged = true;124}125function fixKeyboardMapping(keyCharCode) {126 switch(keyCharCode) {127 case '*':128 return 'X';129 case '/':130 return '÷'; //divide131 case '-':132 return 'â'; // minus133 default:134 return keyCharCode;135 }136}137function negateValue() {138 if(displayText !== '0') {139 if(displayText.indexOf('-') === -1) {140 displayText = '-'.concat(displayText);141 }142 else {143 displayText = displayText.substr(1, displayText.length);144 }145 $('#displaytext').text(displayText);146 }147 else {148 if(subsequentOperation) {149 firstNumber *= -1;150 $('#displaytext').text(firstNumber);151 }152 }153}154function percentage() {155 if(!subsequentOperation) {156 var pct = parseFloat(displayText);157 pct /= 100;158 displayText = String(pct);159 $('#displaytext').text(displayText);160 }161 else {162 firstNumber /= 100;163 $('#displaytext').text(firstNumber);164 }165}166function performOperation() {167 if(displayText.slice(-1) === '.') {168 displayText = displayText.subst(0, displayText.length - 1);169 }170 if(!subsequentOperation) {171 secondNumber = parseFloat(displayText);172 }173 var result;174 switch (operator) {175 case '+':176 result = firstNumber + secondNumber;177 break;178 case 'â':179 result = firstNumber - secondNumber;180 break;181 case 'X':182 result = firstNumber * secondNumber;183 break;184 case '÷':185 if(secondNumber !== 0) {186 result = firstNumber / secondNumber;187 }188 else {189 result = 'Not a number';190 mustClearAfterDivideByZero = true;191 }192 }193 $('#displaytext').text(result);194 firstNumber = result;195 displayText = '0';196 subsequentOperation = true;197 performOperationStaged = false;...
script.js
Source:script.js
1const defaultDisplay = '0';2let secondNumber = false;3let numOne = '';4let numTwo = '';5let operand = '';6const operate = (a, operator, b) => {7 switch (operator) {8 case '+':9 displayText.textContent = add(a, b);10 break;11 case '-':12 displayText.textContent = subtract(a, b);13 break;14 case '*':15 displayText.textContent = multiply(a, b);16 break;17 case '/':18 displayText.textContent = divide(a, b);19 break;20 }21 numOne = '';22 numTwo = '';23 secondNumber = true;24}25const add = (numOne, numTwo) => {26 return parseFloat(numOne) + parseFloat(numTwo);27}28const subtract = (numOne, numTwo) => {29 return parseFloat(numOne) - parseFloat(numTwo);30}31const multiply = (numOne, numTwo) => {32 return Math.floor((parseFloat(numOne) * parseFloat(numTwo))*10)/10;33}34const divide = (numOne, numTwo) => {35 if (parseFloat(numTwo) === 0) return "Try again...";36 return Math.floor((parseFloat(numOne) / parseFloat(numTwo))*10)/10;37}38const equals = () => {39 numTwo = displayText.textContent;40 operate(numOne,operator,numTwo);41}42const resetNumber = () => {43 displayText.textContent = '';44 secondNumber = false;45}46const enterNumber = (e) => {47 if (secondNumber) resetNumber();48 if (displayText.textContent.length >= 12) {49 overflow.textContent='Maximum of 12 digits';50 } else {51 if (displayText.textContent == '0') {52 displayText.textContent = e.target.id;53 } else {54 displayText.textContent += e.target.id;55 }56 }57}58const enterOperator = (e) => {59 overflow.textContent='';60 operator = e.target.id;61 if (numOne !== '') {62 numTwo = displayText.textContent;63 operate(numOne, operator, numTwo);64 numOne = displayText.textContent;65 } else {66 numOne = displayText.textContent;67 secondNumber = true;68 }69}70const decimalPoint = (e) => {71 if (displayText.textContent.includes('.')) return;72 displayText.textContent += '.';73}74const flipOperate = (e) => {75 displayText.textContent = (parseFloat(displayText.textContent)*-1).toString();76}77const percent = (e) => {78 if (displayText.textContent !== '0') {79 displayText.textContent = (parseFloat(displayText.textContent)/100).toString();80 }81}82const deleteNum = (e) => {83 displayText.textContent = displayText.textContent.slice(0, -1);84}85const clear = (e) => {86 displayText.textContent = defaultDisplay;87 let numOne = '';88 let numTwo = '';89 let operand = '';90}91const numberButtons = Array.from(document.querySelectorAll('.number-btn'));92const operatorButtons = Array.from(document.querySelectorAll('.operator-btn'));93const decimalButton = document.querySelector('.decimal-btn');94const equalsButton = document.querySelector('.equals-btn');95const flipButton = document.querySelector('.flip-btn');96const percentButton = document.querySelector('.percent-btn');97const deleteButton = document.querySelector('.delete-btn');98const clearButton = document.querySelector('.clear-btn');99numberButtons.forEach(button => button.addEventListener('click', enterNumber));100operatorButtons.forEach(button => button.addEventListener('click', enterOperator));101decimalButton.addEventListener('click', decimalPoint);102equalsButton.addEventListener('click', equals);103flipButton.addEventListener('click', flipOperate);104percentButton.addEventListener('click', percent);105deleteButton.addEventListener('click', deleteNum);106clearButton.addEventListener('click', clear);107const displayText = document.querySelector('#display-text');108displayText.textContent = defaultDisplay;...
data.js
Source:data.js
1const questions = [2 {3 id:1,4 marks: 4,5 questionText: "The language spoken by the people by Pakistan is ?",6 answer: 'Sindhi',7 options: [8 {9 value: "Hindi",10 displayText: "Hindi",11 },12 {13 value: "Palauan",14 displayText: "Palaun",15 },16 {17 value: "Sindhi",18 displayText: "Sindhi",19 },20 {21 value: "Nauruan",22 displayText: "Nauruan",23 },24 ],25 },26 {27 id:2,28 marks: 6,29 questionText: "The World Largest desert is ?",30 answer: 'Sahara',31 options: [32 {33 value: "Thar",34 displayText: "Thar",35 },36 {37 value: "Kalahari",38 displayText: "Kalahari",39 },40 {41 value: "Sahara",42 displayText: "Sahara",43 },44 {45 value: "Sonoran",46 displayText: "Sonoran",47 },48 ],49 },50 {51 id:3,52 marks: 4,53 questionText: " The greatest number of five digits which is divisible by 32, 36, 40, 42 and 48 is:",54 answer: '90720',55 options: [56 {57 value: "90730",58 displayText: "90730",59 },60 {61 value: "90725",62 displayText: "90725",63 },64 {65 value: "90715",66 displayText: "90715",67 },68 {69 value: "90720",70 displayText: "90720",71 },72 ],73 },74 {75 id:4,76 marks: 4,77 questionText: " A boat can move upstream at 25 kmph and downstream at 35 kmph, then the speed of the current is?",78 answer: '5kmph',79 options: [80 {81 value: "5kmph",82 displayText: "5kmph",83 },84 {85 value: "10kmph",86 displayText: "10kmph",87 },88 {89 value: "30kmph",90 displayText: "30kmph",91 },92 {93 value: "15kmph",94 displayText: "15kmph",95 },96 ],97 },98 {99 id:5,100 marks: 2,101 questionText: " A and B began business with Rs.3000 and Rs.4000 after 8 months, A withdraws Rs.1000 and B advances Rs.1000 more. At the end of the year, their profits amounted to Rs.630 find the share of A.",102 answer: '240',103 options: [104 {105 value: "240",106 displayText: "240",107 },108 {109 value: "350",110 displayText: "350",111 },112 {113 value: "340",114 displayText: "340",115 },116 {117 value: "390",118 displayText: "390",119 },120 ],121 },122 123 ]...
Using AI Code Generation
1const { openBrowser, goto, write, closeBrowser, displayText } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await goto("google.com");6 await write("Taiko");7 await displayText("Taiko").exists();8 } catch (e) {9 console.error(e);10 } finally {11 await closeBrowser();12 }13})();14const { openBrowser, goto, write, closeBrowser, displayText } = require('taiko');15(async () => {16 try {17 await openBrowser();18 await goto("google.com");19 await write("Taiko");20 await displayText("Taiko").exists();21 } catch (e) {22 console.error(e);23 } finally {24 await closeBrowser();25 }26})();
Using AI Code Generation
1const { openBrowser, goto, closeBrowser, displayText } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await goto("google.com");6 await displayText("Google");7 await closeBrowser();8 } catch (error) {9 console.error(error);10 }11})();12const { getMatchedStylesForNode } = require('./protocol/css');13const { getNodeWithText } = require('./protocol/accessibility');14module.exports = {15 color: async (text) => {16 const node = await getNodeWithText(text);17 const styles = await getMatchedStylesForNode(node.nodeId);18 return styles.matchedCSSRules[0].rule.style.color;19 }20};21const { openBrowser, goto, closeBrowser, color } = require('taiko');22(async () => {23 try {24 await openBrowser();25 await goto("google.com");26 await color("Google");27 await closeBrowser();28 } catch (error) {29 console.error(error);30 }31})();
Using AI Code Generation
1const { openBrowser, goto, closeBrowser } = require('taiko');2(async () => {3 try {4 await openBrowser();5 await goto("google.com");6 await displayText("I'm a software tester!");7 await closeBrowser();8 } catch (error) {9 console.error(error);10 }11})();12const { openBrowser, goto, closeBrowser } = require('taiko');13(async () => {14 try {15 await openBrowser();16 await goto("google.com");17 await displayTexts(["I'm a software tester!"]);18 await closeBrowser();19 } catch (error) {20 console.error(error);21 }22})();23const { openBrowser, goto, closeBrowser } = require('taiko');24(async () => {25 try {26 await openBrowser();27 await goto("google.com");28 await displayTexts(["I'm a software tester!"], { waitFor: 1000 });29 await closeBrowser();30 } catch (error) {31 console.error(error);32 }33})();34const { openBrowser, goto, closeBrowser } = require('taiko');35(async () => {36 try {37 await openBrowser();38 await goto("google.com");39 await displayTexts(["I'm a software tester!"], { waitFor: 1000, timeout: 2000 });40 await closeBrowser();41 } catch (error) {42 console.error(error);43 }44})();45const { openBrowser, goto, closeBrowser } = require('taiko');46(async () => {47 try {48 await openBrowser();49 await goto("google.com");50 await displayTexts(["I'm a software tester!"], { waitFor: 1000, timeout: 2000, interval: 3000 });51 await closeBrowser();52 } catch (error) {53 console.error(error);54 }55})();56const { openBrowser, goto, closeBrowser } = require('taiko');57(async () => {58 try {59 await openBrowser();
Using AI Code Generation
1const { displayText } = require('taiko');2(async () => {3 try {4 await displayText('Hello World');5 } catch (e) {6 console.error(e);7 } finally {8 }9})();10displayText('Hello World');11displayText(new Date());12let myVar = 'Hello World';13displayText(myVar);14let myObj = { name: 'John', age: 30 };15displayText(myObj);16{ name: 'John', age: 30 }17let myArr = ['John', 'Peter', 'Sally'];18displayText(myArr);
Using AI Code Generation
1displayText('hello world');2displayText('hello world');3displayText('hello world');4displayText('hello world');5displayText('hello world');6displayText('hello world');7displayText('hello world');8displayText('hello world');9displayText('hello world');10displayText('hello world');11displayText('hello world');12displayText('hello world');13displayText('hello world');14displayText('hello world');15displayText('hello world');16displayText('hello world');17displayText('hello world');18displayText('hello world');19displayText('hello world');20displayText('hello world');21displayText('hello world');22displayText('hello world');23displayText('hello world');24displayText('hello world');25displayText('hello world');26displayText('hello world');27displayText('hello world
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!!