Best JavaScript code snippet using testcafe
shortcuts.js
Source:shortcuts.js
...30 var inverseSelection = textSelection.hasInverseSelection(textarea);31 var textareaValue = domUtils.getTextAreaValue(textarea);32 var cursorPosition = inverseSelection ?33 textSelection.getSelectionStart(textarea) :34 textSelection.getSelectionEnd(textarea);35 if (!textareaValue || !cursorPosition)36 return 0;37 return domUtils.getTextareaIndentInLine(textarea, cursorPosition);38}39function moveTextAreaCursorUp (element, withSelection) {40 var textareaValue = domUtils.getTextAreaValue(element);41 if (!textareaValue)42 return;43 var startPos = textSelection.getSelectionStart(element);44 var endPos = textSelection.getSelectionEnd(element);45 var hasInverseSelection = textSelection.hasInverseSelection(element);46 var partBeforeCursor = textareaValue.substring(0, hasInverseSelection ? startPos : endPos);47 var lastLineBreakIndex = partBeforeCursor.lastIndexOf('\n');48 var partBeforeLastLineBreak = partBeforeCursor.substring(0, lastLineBreakIndex);49 if (currentTextareaCursorIndent === null || currentTextarea !== element)50 updateTextAreaIndent(element);51 lastLineBreakIndex = partBeforeLastLineBreak.lastIndexOf('\n');52 var newPosition = Math.min(lastLineBreakIndex + 1 + currentTextareaCursorIndent, partBeforeLastLineBreak.length);53 moveTextAreaCursor(element, startPos, endPos, hasInverseSelection, newPosition, withSelection);54}55function moveTextAreaCursorDown (element, withSelection) {56 var textareaValue = domUtils.getTextAreaValue(element);57 if (!textareaValue)58 return;59 var startPos = textSelection.getSelectionStart(element);60 var endPos = textSelection.getSelectionEnd(element);61 var hasInverseSelection = textSelection.hasInverseSelection(element);62 var cursorPosition = hasInverseSelection ? startPos : endPos;63 var partAfterCursor = textareaValue.substring(cursorPosition);64 var firstLineBreakIndex = partAfterCursor.indexOf('\n');65 var nextLineStartIndex = firstLineBreakIndex === -1 ? partAfterCursor.length : firstLineBreakIndex + 1;66 var partAfterNewIndent = partAfterCursor.substring(nextLineStartIndex);67 var newPosition = cursorPosition + nextLineStartIndex;68 firstLineBreakIndex = partAfterNewIndent.indexOf('\n');69 var maxIndent = firstLineBreakIndex === -1 ? partAfterNewIndent.length : firstLineBreakIndex;70 if (currentTextareaCursorIndent === null || currentTextarea !== element)71 updateTextAreaIndent(element);72 newPosition = Math.min(newPosition + currentTextareaCursorIndent, newPosition + maxIndent);73 moveTextAreaCursor(element, startPos, endPos, hasInverseSelection, newPosition, withSelection);74}75function moveTextAreaCursor (element, startPos, endPos, hasInverseSelection, newPosition, withSelection) {76 var newStart = null;77 var newEnd = null;78 if (withSelection) {79 if (startPos === endPos) {80 newStart = startPos;81 newEnd = newPosition;82 }83 else if (!hasInverseSelection) {84 newStart = startPos;85 newEnd = newPosition;86 }87 else {88 newStart = endPos;89 newEnd = newPosition;90 }91 }92 else93 newEnd = newStart = newPosition;94 textSelection.select(element, newStart, newEnd);95}96function setElementValue (element, value, position) {97 if (domUtils.isInputElement(element) && element.type === 'number') {98 if (value.charAt(0) === '-' && value.charAt(1) === '.')99 value = value.substring(1);100 if (value.charAt(value.length - 1) === '.')101 value = value.substring(0, value.length - 1);102 }103 domUtils.setElementValue(element, value);104 textSelection.select(element, position, position);105 eventSimulator.input(element);106}107function submitFormOnEnterPressInInput (form, inputElement) {108 var buttons = form.querySelectorAll('input, button');109 var submitButton = null;110 var i = null;111 for (i = 0; i < buttons.length; i++) {112 if (!submitButton && buttons[i].type === 'submit' && !buttons[i].disabled) {113 submitButton = buttons[i];114 break;115 }116 }117 if (submitButton)118 eventSimulator.click(submitButton);119 else if (domUtils.blocksImplicitSubmission(inputElement)) {120 var formInputs = form.getElementsByTagName('input');121 var textInputs = [];122 for (i = 0; i < formInputs.length; i++) {123 if (domUtils.blocksImplicitSubmission(formInputs[i]))124 textInputs.push(formInputs[i]);125 }126 // NOTE: the form is submitted on enter press if there is only one input of the following types on it127 // and this input is focused (http://www.w3.org/TR/html5/forms.html#implicit-submission)128 if (textInputs.length === 1 && textInputs[0] === inputElement) {129 var isInputValid = inputElement.validity.valid;130 if (isInputValid && eventSimulator.submit(form))131 form.submit();132 }133 }134}135//shortcuts136function selectAll (element) {137 if (domUtils.isEditableElement(element))138 textSelection.select(element);139 return Promise.resolve();140}141function backspace (element) {142 if (domUtils.isTextEditableElementAndEditingAllowed(element)) {143 var startPos = textSelection.getSelectionStart(element);144 var endPos = textSelection.getSelectionEnd(element);145 var value = domUtils.getElementValue(element).replace(/\r\n/g, '\n');146 if (endPos === startPos) {147 if (startPos > 0) {148 setElementValue(element, value.substring(0, startPos - 1) +149 value.substring(endPos, value.length), startPos - 1);150 }151 }152 else153 setElementValue(element, value.substring(0, startPos) + value.substring(endPos, value.length), startPos);154 }155 if (domUtils.isContentEditableElement(element))156 textSelection.deleteSelectionContents(element);157 return Promise.resolve();158}159function del (element) {160 if (domUtils.isTextEditableElementAndEditingAllowed(element)) {161 var startPos = textSelection.getSelectionStart(element);162 var endPos = textSelection.getSelectionEnd(element);163 var value = domUtils.getElementValue(element).replace(/\r\n/g, '\n');164 if (endPos === startPos) {165 if (startPos < value.length) {166 setElementValue(element, value.substring(0, startPos) +167 value.substring(endPos + 1, value.length), startPos);168 }169 }170 else {171 setElementValue(element, value.substring(0, startPos) +172 value.substring(endPos, value.length), startPos);173 }174 }175 if (domUtils.isContentEditableElement(element))176 textSelection.deleteSelectionContents(element);177 return Promise.resolve();178}179function left (element) {180 var startPosition = null;181 var endPosition = null;182 if (domUtils.isSelectElement(element))183 selectElement.switchOptionsByKeys(element, 'left');184 if (domUtils.isTextEditableElement(element)) {185 startPosition = textSelection.getSelectionStart(element) || 0;186 endPosition = textSelection.getSelectionEnd(element);187 var newPosition = startPosition === endPosition ? startPosition - 1 : startPosition;188 textSelection.select(element, newPosition, newPosition);189 updateTextAreaIndent(element);190 }191 if (domUtils.isContentEditableElement(element)) {192 startPosition = textSelection.getSelectionStart(element);193 endPosition = textSelection.getSelectionEnd(element);194 // NOTE: we only remove selection195 if (startPosition !== endPosition) {196 var selection = textSelection.getSelectionByElement(element);197 var inverseSelection = textSelection.hasInverseSelectionContentEditable(element);198 var startNode = inverseSelection ? selection.focusNode : selection.anchorNode;199 var startOffset = inverseSelection ? selection.focusOffset : selection.anchorOffset;200 var startPos = { node: startNode, offset: startOffset };201 textSelection.selectByNodesAndOffsets(startPos, startPos, true);202 }203 }204 return Promise.resolve();205}206function right (element) {207 var startPosition = null;208 var endPosition = null;209 if (domUtils.isSelectElement(element))210 selectElement.switchOptionsByKeys(element, 'right');211 if (domUtils.isTextEditableElement(element)) {212 startPosition = textSelection.getSelectionStart(element);213 endPosition = textSelection.getSelectionEnd(element);214 var newPosition = startPosition === endPosition ? endPosition + 1 : endPosition;215 if (startPosition === domUtils.getElementValue(element).length)216 newPosition = startPosition;217 textSelection.select(element, newPosition, newPosition);218 updateTextAreaIndent(element);219 }220 if (domUtils.isContentEditableElement(element)) {221 startPosition = textSelection.getSelectionStart(element);222 endPosition = textSelection.getSelectionEnd(element);223 //NOTE: we only remove selection224 if (startPosition !== endPosition) {225 var selection = textSelection.getSelectionByElement(element);226 var inverseSelection = textSelection.hasInverseSelectionContentEditable(element);227 var endNode = inverseSelection ? selection.anchorNode : selection.focusNode;228 var endOffset = inverseSelection ? selection.anchorOffset : selection.focusOffset;229 var startPos = { node: endNode, offset: endOffset };230 textSelection.selectByNodesAndOffsets(startPos, startPos, true);231 }232 }233 return Promise.resolve();234}235function up (element) {236 if (domUtils.isSelectElement(element))237 selectElement.switchOptionsByKeys(element, 'up');238 if (browserUtils.isWebKit && domUtils.isInputElement(element))239 return home(element);240 if (domUtils.isTextAreaElement(element))241 moveTextAreaCursorUp(element, false);242 return Promise.resolve();243}244function down (element) {245 if (domUtils.isSelectElement(element))246 selectElement.switchOptionsByKeys(element, 'down');247 if (browserUtils.isWebKit && domUtils.isInputElement(element))248 return end(element);249 if (domUtils.isTextAreaElement(element))250 moveTextAreaCursorDown(element, false);251 return Promise.resolve();252}253function home (element, withSelection) {254 if (domUtils.isTextEditableElement(element)) {255 var startPos = textSelection.getSelectionStart(element);256 var endPos = textSelection.getSelectionEnd(element);257 var inverseSelection = textSelection.hasInverseSelection(element);258 var referencePosition = null;259 var isSingleLineSelection = !domUtils.isTextAreaElement(element) ? true :260 domUtils.getTextareaLineNumberByPosition(element, startPos) ===261 domUtils.getTextareaLineNumberByPosition(element, endPos);262 if (isSingleLineSelection)263 referencePosition = inverseSelection ? endPos : startPos;264 else265 referencePosition = inverseSelection ? startPos : endPos;266 var valueBeforeCursor = domUtils.getElementValue(element).substring(0, referencePosition);267 var lastLineBreakIndex = valueBeforeCursor.lastIndexOf('\n');268 var newPosition = lastLineBreakIndex === -1 ? 0 : lastLineBreakIndex + 1;269 var newStartPos = null;270 var newEndPos = null;271 if (isSingleLineSelection) {272 newStartPos = newPosition;273 newEndPos = withSelection ? referencePosition : newPosition;274 textSelection.select(element, newEndPos, newStartPos);275 }276 else if (!inverseSelection)277 textSelection.select(element, startPos, newPosition);278 else279 textSelection.select(element, endPos, newPosition);280 }281 return Promise.resolve();282}283function end (element, withSelection) {284 if (domUtils.isTextEditableElement(element)) {285 var startPos = textSelection.getSelectionStart(element);286 var endPos = textSelection.getSelectionEnd(element);287 var inverseSelection = textSelection.hasInverseSelection(element);288 var referencePosition = null;289 var isSingleLineSelection = !domUtils.isTextAreaElement(element) ? true :290 domUtils.getTextareaLineNumberByPosition(element, startPos) ===291 domUtils.getTextareaLineNumberByPosition(element, endPos);292 if (isSingleLineSelection)293 referencePosition = inverseSelection ? endPos : startPos;294 else295 referencePosition = inverseSelection ? startPos : endPos;296 var valueAsterCursor = domUtils.getElementValue(element).substring(referencePosition);297 var firstLineBreakIndex = valueAsterCursor.indexOf('\n');298 var newPosition = referencePosition;299 var newStartPos = null;300 var newEndPos = null;301 newPosition += firstLineBreakIndex === -1 ? valueAsterCursor.length : firstLineBreakIndex;302 if (isSingleLineSelection) {303 newStartPos = withSelection ? referencePosition : newPosition;304 newEndPos = newPosition;305 textSelection.select(element, newStartPos, newEndPos);306 }307 else if (!inverseSelection)308 textSelection.select(element, startPos, newPosition);309 else310 textSelection.select(element, endPos, newPosition);311 }312 return Promise.resolve();313}314function esc (element) {315 if (domUtils.isSelectElement(element))316 selectElement.collapseOptionList();317 return Promise.resolve();318}319function shiftUp (element) {320 if (browserUtils.isWebKit && domUtils.isInputElement(element))321 return shiftHome(element);322 if (domUtils.isTextAreaElement(element))323 moveTextAreaCursorUp(element, true);324 return Promise.resolve();325}326function shiftDown (element) {327 if (browserUtils.isWebKit && domUtils.isInputElement(element))328 return shiftEnd(element);329 if (domUtils.isTextAreaElement(element))330 moveTextAreaCursorDown(element, true);331 return Promise.resolve();332}333function shiftLeft (element) {334 if (domUtils.isTextEditableElement(element)) {335 var startPos = textSelection.getSelectionStart(element);336 var endPos = textSelection.getSelectionEnd(element);337 if (startPos === endPos || textSelection.hasInverseSelection(element))338 textSelection.select(element, endPos, Math.max(startPos - 1, 0));339 else340 textSelection.select(element, startPos, Math.max(endPos - 1, 0));341 updateTextAreaIndent(element);342 }343 return Promise.resolve();344}345function shiftRight (element) {346 if (domUtils.isTextEditableElement(element)) {347 var startPos = textSelection.getSelectionStart(element);348 var endPos = textSelection.getSelectionEnd(element);349 var valueLength = domUtils.getElementValue(element).length;350 if (startPos === endPos || !textSelection.hasInverseSelection(element))351 textSelection.select(element, startPos, Math.min(endPos + 1, valueLength));352 else353 textSelection.select(element, endPos, Math.min(startPos + 1, valueLength));354 updateTextAreaIndent(element);355 }356 return Promise.resolve();357}358function shiftHome (element) {359 return home(element, true);360}361function shiftEnd (element) {362 return end(element, true);...
Element.Forms.js
Source:Element.Forms.js
...14 return this.get('value').substring(start, end);15 },16 getSelectedText: function() {17 if(Browser.Engine.trident) return document.selection.createRange().text;18 return this.get('value').substring(this.getSelectionStart(), this.getSelectionEnd());19 },20 getBookmarkOffset: function() {21 if(Browser.Engine.trident) {22 var tmp_range = this.createTextRange();23 tmp_range.move("character", 0);24 return tmp_range.getBookmark().charCodeAt(2);25 } else return null;26 },27 getSelectionStart: function() {28 if(Browser.Engine.trident) {29 this.focus();30 var range = document.selection.createRange();31 if (range.compareEndPoints("StartToEnd", range) != 0) range.collapse(true);32 return range.getBookmark().charCodeAt(2) - this.getBookmarkOffset();33 }34 return this.selectionStart;35 },36 getSelectionEnd: function() {37 if(Browser.Engine.trident) {38 var range = document.selection.createRange();39 if (range.compareEndPoints("StartToEnd", range) != 0) range.collapse(false);40 return range.getBookmark().charCodeAt(2) - this.getBookmarkOffset();41 }42 return this.selectionEnd;43 },44 getSelectedRange: function() {45 return {46 start: this.getSelectionStart(),47 end: this.getSelectionEnd()48 }49 },50 setCaretPosition: function(pos) {51 if(pos == 'end') pos = this.get('value').length;52 this.selectRange(pos, pos);53 return this;54 },55 getCaretPosition: function() {56 return this.getSelectedRange().start;57 },58 selectRange: function(start, end) {59 this.focus();60 if(Browser.Engine.trident) {61 var range = this.createTextRange();62 range.collapse(true);63 range.moveStart('character', start);64 range.moveEnd('character', end - start);65 range.select();66 return this;67 }68 this.setSelectionRange(start, end);69 return this;70 },71 insertAtCursor: function(value, select) {72 var start = this.getSelectionStart();73 var end = this.getSelectionEnd();74 this.set('value', this.get('value').substring(0, start) + value + this.get('value').substring(end, this.get('value').length));75 if($pick(select, true)) this.selectRange(start, start + value.length);76 else this.setCaretPosition(start + value.length);77 return this;78 },79 insertAroundCursor: function(options, select) {80 options = $merge({81 before: '',82 defaultMiddle: 'SOMETHING HERE',83 after: ''84 }, options);85 value = this.getSelectedText() || options.defaultMiddle;86 var start = this.getSelectionStart();87 var end = this.getSelectionEnd();88 if(start == end) {89 var text = this.get('value');90 this.set('value', text.substring(0, start) + options.before + value + options.after + text.substring(end, text.length));91 this.selectRange(start + options.before.length, end + options.before.length + value.length);92 text = null;93 } else {94 text = this.get('value').substring(start, end);95 this.set('value', this.get('value').substring(0, start) + options.before + text + options.after + this.get('value').substring(end, this.get('value').length));96 var selStart = start + options.before.length;97 if($pick(select, true)) this.selectRange(selStart, selStart + text.length);98 else this.setCaretPosition(selStart + text.length);99 }100 return this;101 }...
jquery.selection.js
Source:jquery.selection.js
...30 return node.selectionStart = start;31 } else if('getSelection' in document) {32 var selection = node.ownerDocument.getSelection(),33 range = document.createRange(),34 end = getSelectionEnd(node);35 selection.removeAllRanges();36 range.setStart(node.firstChild, start);37 range.setEnd(node.firstChild, end);38 selection.addRange(range);39 } else if('selection' in document) {40 console.log('IE');41 }42 }43 44 // End45 function getSelectionEnd(node) {46 if('selectionStart' in node) {47 return node.selectionEnd;48 } else if('getSelection' in document) {49 if(!node.ownerDocument.getSelection().rangeCount) {50 return node.innerText.length;51 } else {52 return node.ownerDocument.getSelection().getRangeAt(0).endOffset;53 }54 } else if('selection' in document) {55 console.log('IE');56 }57 }58 function setSelectionEnd(node, end) {59 if('selectionStart' in node) {60 return node.selectionEnd = end;61 } else if('getSelection' in document) {62 var selection = node.ownerDocument.getSelection(),63 range = document.createRange(),64 start = getSelectionStart(node);65 selection.removeAllRanges();66 range.setStart(node.firstChild, start);67 range.setEnd(node.firstChild, end);68 selection.addRange(range);69 } else if('selection' in document) {70 console.log('IE');71 }72 }73 74 // length75 function getSelectionLength(node) {76 return getSelectionEnd(node) - getSelectionStart(node);77 }78 function setSelectionLength(node, length) {79 setSelectionEnd(node, getSelectionStart(node) + length);80 }81 82 function set(node, start, n, end) {83 setSelectionStart(node, start);84 if(end) {85 setSelectionEnd(node, n);86 } else {87 setSelectionLength(node, n);88 }89 }90 91 function replace(node, replace) {92 var el = $(node),93 str;94 if(el.is('input,textarea')) {95 str = el.val();96 el.val( str.substring(0, getSelectionStart(node))97 + replace98 + str.substring(getSelectionEnd(node)));99 } else {100 str = el.text();101 console.log(getSelectionStart(node));102 el.text(str.substring(0, getSelectionStart(node))103 + replace104 + str.substring(getSelectionEnd(node)));105 }106 }107 108 $.fn.selection = function(length) {109 var node = this.jquery ? this.get(0) : this;110 return {111 start: function(start) {112 if(typeof start !== 'undefined') {113 setSelectionStart(node, start);114 return this;115 } else {116 return getSelectionStart(node);117 }118 },119 end: function(end) {120 if(typeof end !== 'undefined') {121 setSelectionEnd(node, end);122 return this;123 } else {124 return getSelectionEnd(node);125 }126 },127 length: function(length) {128 if(typeof length !== 'undefined') {129 setSelectionLength(node, length);130 return this;131 } else {132 return getSelectionLength(node);133 }134 },135 set: function(start, n, end) {136 set(node, start, n, end);137 return this;138 },139 clear: function() {140 this.replace('');141 },142 replace: function(string) {143 var start = getSelectionStart(node);144 replace(node, string);145 set(node, start + string.length, 0);146 return this;147 },148 before: function(string) {149 var start = getSelectionStart(node);150 setSelectionLength(node, 0);151 replace(node, string);152 set(node, start + string.length, 0);153 },154 after: function(string) {155 var start = getSelectionEnd(node);156 set(node, start, 0);157 replace(node, string);158 set(node, start + string.length, 0);159 }160 };161 }162
...
limitkeyPress.js
Source:limitkeyPress.js
2 $.fn.limitkeypress = function (options) {3 var defaults = { rexp: /^[-+]?\d*\.?\d*$/ }; var options = $.extend(defaults, options); return this.each(function () {4 var regExpression = options.rexp; $(this).blur(function () { sanitize(this); }); $(this).keypress(function (e) {5 if (e.which == "0" || e.which == "8" || e.which == "13" || e.ctrlKey || e.altKey) { return; }6 sanitizeWithSelection(this); var pressedChar = String.fromCharCode(e.which), updatedInput = this.value.substring(0, getSelectionStart(this)) + pressedChar + this.value.substring(getSelectionEnd(this), this.value.length); if (!regExpression.test(updatedInput)) { e.preventDefault(); return; }7 return;8 }); function sanitizeWithSelection(o) {9 var startCaretPos = getSelectionStart(o), endCaretPos = getSelectionEnd(o), temp = "", testPlusChar = "", selectionCharInfo = []; for (i = 0; i < o.value.length; i++) { if (startCaretPos > i) { selectionCharInfo[i] = 'beforeSelection'; } else if ((startCaretPos <= i) && (endCaretPos > i)) { selectionCharInfo[i] = 'inSelection'; } }10 for (i = 0; i < o.value.length; i++) { var iPlusOne = i + 1; testPlusChar += o.value.substring(i, iPlusOne); if ((!regExpression.test(testPlusChar))) { var lastChar = testPlusChar.length - 1; temp = testPlusChar.substring(0, lastChar); testPlusChar = temp; if (selectionCharInfo[i] == 'beforeSelection') { startCaretPos = startCaretPos - 1; endCaretPos = endCaretPos - 1; } else if (selectionCharInfo[i] == 'inSelection') { endCaretPos = endCaretPos - 1; } } }11 o.value = testPlusChar; setSelectionRange(o, startCaretPos, endCaretPos);12 }13 function sanitize(o) {14 var temp = "", testPlusChar = ""; for (i = 0; i < o.value.length; i++) { var iPlusOne = i + 1; testPlusChar += o.value.substring(i, iPlusOne); if ((!regExpression.test(testPlusChar))) { var lastChar = testPlusChar.length - 1; temp = testPlusChar.substring(0, lastChar); testPlusChar = temp; } }15 o.value = testPlusChar;16 }17 function getSelectionStart(o) {18 if (o.createTextRange) {19 var r = document.selection.createRange().duplicate()20 r.moveEnd('character', o.value.length)21 if (r.text == '') return o.value.length22 return o.value.lastIndexOf(r.text)23 } else return o.selectionStart24 }25 function getSelectionEnd(o) {26 if (o.createTextRange) {27 var r = document.selection.createRange().duplicate()28 r.moveStart('character', -o.value.length)29 return r.text.length30 } else return o.selectionEnd31 }32 function setSelectionRange(input, selectionStart, selectionEnd) {33 if (input.setSelectionRange) { input.focus(); input.setSelectionRange(selectionStart, selectionEnd); }34 else if (input.createTextRange) { var range = input.createTextRange(); range.collapse(true); range.moveEnd('character', selectionEnd); range.moveStart('character', selectionStart); range.select(); }35 }36 });37 };...
select-out-of-floated-editable.js
Source:select-out-of-floated-editable.js
...15} else {16 window.onmouseup = function() {17 window.setTimeout(function() {18 log('Input selection start: ' + getSelectionStart(floatedEditable) + ', end: ' +19 getSelectionEnd(floatedEditable));20 checkSelection();21 }, 0); // Without a timeout the selection is inaccurately printed22 }23}24function getSelectionStart(element) {25 return element.isContentEditable ? window.getSelection().baseOffset : element.selectionStart;26}27function getSelectionEnd(element) {28 return element.isContentEditable ? window.getSelection().extentOffset : element.selectionEnd;29}30function checkSelection() {31 var inputText = floatedEditable.isContentEditable ? floatedEditable.textContent : floatedEditable.value;32 var selectionStart = getSelectionStart(floatedEditable);33 var selectionEnd = getSelectionEnd(floatedEditable);34 var selectionStartsFromMiddle = selectionStart > 0 && selectionStart < inputText.length;35 var selectionGoesToEnd = selectionEnd == inputText.length;36 if (selectionStartsFromMiddle && selectionGoesToEnd)37 result.innerHTML = '<span style="padding: 5px; background-color: green">SUCCESS</span>';38 else39 result.innerHTML = '<span style="padding: 5px; background-color: red">FAIL</span>';...
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3 .typeText('#developer-name', 'John Smith')4 .click('#tried-test-cafe')5 .click('#submit-button')6 .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');7});8const puppeteer = require('puppeteer');9(async () => {10 const browser = await puppeteer.launch();11 const page = await browser.newPage();12 await page.type('#developer-name', 'John Smith');13 await page.click('#tried-test-cafe');14 await page.click('#submit-button');15 await page.waitForSelector('#article-header');16 const headerText = await page.evaluate(() => document.querySelector('#article-header').innerText);17 const inputText = await page.evaluate(() => document.querySelector('#developer-name').value);18 const inputTextLength = await page.evaluate(() => document.querySelector('#developer-name').value.length);19 const inputSelectionEnd = await page.evaluate(() => document.querySelector('#developer-name').selectionEnd);20 console.log(headerText);21 console.log(inputText);22 console.log(inputTextLength);23 console.log(inputSelectionEnd);24 await browser.close();25})();
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3 const developerName = Selector('#developer-name');4 .typeText(developerName, 'Peter')5 .expect(developerName.value).eql('Peter')6 .click('#windows')7 .click('#submit-button')8 .expect(Selector('#article-header').innerText).eql('Thank you, Peter!');9});10import { Selector } from 'testcafe';11test('My first test', async t => {12 const developerName = Selector('#developer-name');13 .typeText(developerName, 'Peter')14 .expect(developerName.value).eql('Peter')15 .click('#windows')16 .click('#submit-button')17 .expect(Selector('#article-header').innerText).eql('Thank you, Peter!');18});19import { Selector } from 'testcafe';20test('My first test', async t => {21 const developerName = Selector('#developer-name');22 .typeText(developerName, 'Peter')23 .expect(developerName.value).eql('Peter')24 .click('#windows')25 .click('#submit-button')26 .expect(Selector('#article-header').innerText).eql('Thank you, Peter!');27});28import { Selector } from 'testcafe';29test('My first test', async t => {30 const developerName = Selector('#developer-name');31 .typeText(developerName, 'Peter')32 .expect(developerName.value).eql('Peter')33 .click('#windows')34 .click('#submit-button')35 .expect(Selector('#article-header').innerText).eql
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3 const developerNameInput = Selector('#developer-name');4 .typeText(developerNameInput, 'Peter')5 .expect(developerNameInput.value).eql('Peter')6 .expect(developerNameInput.value).contains('Pet')7 .expect(developerNameInput.value).notContains('Pet1')8 .expect(developerNameInput.value).match(/Pet/)9 .expect(developerNameInput.value).notMatch(/Pet1/)10 .expect(developerNameInput.value).eql('Peter')11 .expect(developerNameInput.value).notEql('Peter1')12 .expect(developerNameInput.value).ok()13 .expect(developerNameInput.value).notOk()14 .expect(developerNameInput.value).typeOf('string')15 .expect(developerNameInput.value).notTypeOf('number')16 .expect(developerNameInput.value).gt('Pete')17 .expect(developerNameInput.value).gte('Pete')18 .expect(developerNameInput.value).lt('Petf')19 .expect(developerNameInput.value).lte('Petf')20 .expect(developerNameInput.value).within('Pete', 'Petf')21 .expect(developerNameInput.value).notWithin('Pet1', 'Pet2')22 .expect(developerNameInput.value).contains('Pet')23 .expect(developerNameInput.value).notContains('Pet1')24 .expect(developerNameInput.value).match(/Pet/)25 .expect(developerNameInput.value).notMatch(/Pet1/)26 .expect(developerNameInput.value).contains('Pet')27 .expect(developerNameInput.value).notContains('Pet1')28 .expect(developerNameInput.value).match(/Pet/)29 .expect(developerNameInput.value).notMatch(/Pet1/)30 .expect(developerNameInput.value).contains('Pet')31 .expect(developerNameInput.value).notContains('Pet1')32 .expect(developerNameInput.value).match(/Pet/)33 .expect(developerNameInput.value).notMatch(/Pet1/)34 .expect(developerNameInput.value).contains('Pet')35 .expect(de
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3 const text = Selector('#developer-name');4 .typeText(text, 'Peter')5 .expect(text.value).eql('Peter')6 .pressKey('home right . delete delete delete delete delete delete delete')7 .expect(text.value).eql('P. Ch. Miller');8});9import { Selector } from 'testcafe';10test('My first test', async t => {
Using AI Code Generation
1import { Selector } from 'testcafe';2test('Getting selection end', async t => {3 const select = Selector('#preferred-interface');4 const option = select.find('option');5 const selectedOption = option.withText('Both');6 const selectedOptionIndex = await selectedOption.index;7 const selectedOptionText = await selectedOption.textContent;8 const selectedOptionValue = await selectedOption.value;9 const selectedOptionCount = await option.count;10 console.log('Selected option index is', selectedOptionIndex);11 console.log('Selected option text is', selectedOptionText);12 console.log('Selected option value is', selectedOptionValue);13 console.log('Option count is', selectedOptionCount);14 .click(select)15 .click(selectedOption);16});17import { Selector } from 'testcafe';18test('Getting selection start', async t => {19 const select = Selector('#preferred-interface');20 const option = select.find('option');21 const selectedOption = option.withText('Both');22 const selectedOptionIndex = await selectedOption.index;23 const selectedOptionText = await selectedOption.textContent;24 const selectedOptionValue = await selectedOption.value;25 const selectedOptionCount = await option.count;26 console.log('Selected option index is', selectedOptionIndex);27 console.log('Selected option text is', selectedOptionText);28 console.log('Selected option value is', selectedOptionValue);29 console.log('Option count is', selectedOptionCount);30 .click(select)31 .click(selectedOption);32});33import { Selector } from 'testcafe';34test('Getting selection start', async t => {35 const select = Selector('#preferred-interface');36 const option = select.find('option');37 const selectedOption = option.withText('Both');38 const selectedOptionIndex = await selectedOption.index;39 const selectedOptionText = await selectedOption.textContent;
Using AI Code Generation
1import { Selector } from 'testcafe';2test('Getting Selection End', async t => {3 const textInput = Selector('#developer-name');4 .typeText(textInput, 'Peter Parker')5 .selectText(textInput, 5, 11)6 .expect(textInput.getSelectionEnd()).eql(11);7});8await t.expect( Selector().getSelectionStart() ).eql( 10 );9import { Selector } from 'testcafe';10test('Getting Selection Start', async t => {11 const textInput = Selector('#developer-name');12 .typeText(textInput, 'Peter Parker')13 .selectText(textInput, 5, 11)14 .expect(textInput.getSelectionStart()).eql(5);15});16await t.expect( Selector().hasAttribute('attributeName') ).eql( true );17import { Selector } from 'testcafe';18test('Checking for Attribute', async t => {19 const textInput = Selector('#developer-name');
Using AI Code Generation
1import {Selector} from 'testcafe';2test('Get text', async t => {3 .typeText('#developer-name', 'Peter Parker')4 .click('#tried-test-cafe')5 .click(Selector('label').withText('Windows'))6 .click('#submit-button');7 const articleHeader = await Selector('.result-content').find('h1');8 let headerText = await articleHeader.textContent;9 console.log(headerText);10 let innerHTML = await articleHeader.innerHTML;11 console.log(innerHTML);12 let visibleText = await articleHeader.visibleText;13 console.log(visibleText);14});15import {Selector} from 'testcafe';16test('Get text', async t => {17 .typeText('#developer-name', 'Peter Parker')18 .click('#tried-test-cafe')19 .click(Selector('label').withText('Windows'))20 .click('#submit-button');21 const articleHeader = await Selector('.result-content').find('h1');22 let headerText = await articleHeader.textContent;23 console.log(headerText);24 let innerHTML = await articleHeader.innerHTML;25 console.log(innerHTML);26 let visibleText = await articleHeader.visibleText;27 console.log(visibleText);28});29import {Selector} from 'testcafe';30test('Get text', async t => {31 .typeText('#developer-name', 'Peter Parker')32 .click('#tried-test-cafe')33 .click(Selector('label').withText('Windows'))34 .click('#submit-button');35 const articleHeader = await Selector('.result
Using AI Code Generation
1import { Selector } from 'testcafe';2test('Getting the value of the selectionEnd property of an input element', async t => {3 const input = Selector('#developer-name');4 .typeText(input, 'Peter Parker')5 .expect(input.value).eql('Peter Parker')6 .expect(input.getSelectionEnd()).eql(13);7});8import { Selector } from 'testcafe';9test('Getting the value of the selectionEnd property of an input element', async t => {10 const input = Selector('#developer-name');11 .typeText(input, 'Peter Parker')12 .expect(input.value).eql('Peter Parker')13 .expect(input.getSelectionEnd()).eql(13);14});15import { Selector } from 'testcafe';16test('Getting the value of the selectionEnd property of an input element', async t => {17 const input = Selector('#developer-name');18 .typeText(input, 'Peter Parker')19 .expect(input.value).eql('Peter Parker')20 .expect(input.getSelectionEnd()).eql(13);21});22import { Selector } from 'testcafe';23test('Getting the value of the selectionEnd property of an input element', async t => {24 const input = Selector('#developer-name');25 .typeText(input, 'Peter Parker')26 .expect(input.value).eql('Peter Parker')27 .expect(input.getSelectionEnd()).eql(13);28});29import { Selector } from 'testcafe';
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!!