How to use _getSelectionInElement method in Testcafe

Best JavaScript code snippet using testcafe

index.js

Source:index.js Github

copy

Full Screen

...3142 var domUtils$7 = testCafeCore__default.domUtils;3143 var contentEditable$1 = testCafeCore__default.contentEditable;3144 var textSelection$1 = testCafeCore__default.textSelection;3145 var WHITE_SPACES_RE = / /g;3146 function _getSelectionInElement(element) {3147 var currentSelection = textSelection$1.getSelectionByElement(element);3148 var isInverseSelection = textSelection$1.hasInverseSelectionContentEditable(element);3149 if (textSelection$1.hasElementContainsSelection(element))3150 return contentEditable$1.getSelection(element, currentSelection, isInverseSelection);3151 // NOTE: if we type text to an element that doesn't contain selection we3152 // assume the selectionStart and selectionEnd positions are null in this3153 // element. So we calculate the necessary start and end nodes and offsets3154 return {3155 startPos: contentEditable$1.calculateNodeAndOffsetByPosition(element, 0),3156 endPos: contentEditable$1.calculateNodeAndOffsetByPosition(element, 0)3157 };3158 }3159 function _updateSelectionAfterDeletionContent(element, selection) {3160 var startNode = selection.startPos.node;3161 var startParent = nativeMethods$6.nodeParentNodeGetter.call(startNode);3162 var hasStartParent = startParent && startNode.parentElement;3163 var browserRequiresSelectionUpdating = browserUtils$8.isChrome && browserUtils$8.version < 58 || browserUtils$8.isSafari;3164 if (browserRequiresSelectionUpdating || !hasStartParent || !domUtils$7.isElementContainsNode(element, startNode)) {3165 selection = _getSelectionInElement(element);3166 if (textSelection$1.hasInverseSelectionContentEditable(element)) {3167 selection = {3168 startPos: selection.endPos,3169 endPos: selection.startPos3170 };3171 }3172 }3173 selection.endPos.offset = selection.startPos.offset;3174 return selection;3175 }3176 function _typeTextInElementNode(elementNode, text, offset) {3177 var nodeForTyping = document.createTextNode(text);3178 var textLength = text.length;3179 var selectPosition = { node: nodeForTyping, offset: textLength };3180 var parent = nativeMethods$6.nodeParentNodeGetter.call(elementNode);3181 if (domUtils$7.getTagName(elementNode) === 'br')3182 parent.insertBefore(nodeForTyping, elementNode);3183 else if (offset > 0)3184 elementNode.insertBefore(nodeForTyping, elementNode.childNodes[offset]);3185 else3186 elementNode.appendChild(nodeForTyping);3187 textSelection$1.selectByNodesAndOffsets(selectPosition, selectPosition);3188 }3189 function _typeTextInChildTextNode(element, selection, text) {3190 var startNode = selection.startPos.node;3191 // NOTE: startNode could be moved or deleted on textInput event. Need ensure startNode.3192 if (!domUtils$7.isElementContainsNode(element, startNode)) {3193 selection = _excludeInvisibleSymbolsFromSelection(_getSelectionInElement(element));3194 startNode = selection.startPos.node;3195 }3196 var startOffset = selection.startPos.offset;3197 var endOffset = selection.endPos.offset;3198 var nodeValue = startNode.nodeValue;3199 var selectPosition = { node: startNode, offset: startOffset + text.length };3200 startNode.nodeValue = nodeValue.substring(0, startOffset) + text +3201 nodeValue.substring(endOffset, nodeValue.length);3202 textSelection$1.selectByNodesAndOffsets(selectPosition, selectPosition);3203 }3204 function _excludeInvisibleSymbolsFromSelection(selection) {3205 var startNode = selection.startPos.node;3206 var startOffset = selection.startPos.offset;3207 var endOffset = selection.endPos.offset;3208 var firstNonWhitespaceSymbolIndex = contentEditable$1.getFirstNonWhitespaceSymbolIndex(startNode.nodeValue);3209 var lastNonWhitespaceSymbolIndex = contentEditable$1.getLastNonWhitespaceSymbolIndex(startNode.nodeValue);3210 if (startOffset < firstNonWhitespaceSymbolIndex && startOffset !== 0) {3211 selection.startPos.offset = firstNonWhitespaceSymbolIndex;3212 selection.endPos.offset = endOffset + firstNonWhitespaceSymbolIndex - startOffset;3213 }3214 else if (endOffset > lastNonWhitespaceSymbolIndex && endOffset !== startNode.nodeValue.length) {3215 selection.startPos.offset = startNode.nodeValue.length;3216 selection.endPos.offset = endOffset + startNode.nodeValue.length - startOffset;3217 }3218 return selection;3219 }3220 // NOTE: https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/beforeinput_event3221 // The `beforeInput` event is supported only in Chrome-based browsers and Safari3222 // The order of events differs in Chrome and Safari:3223 // In Chrome: `beforeinput` occurs before `textInput`3224 // In Safari: `beforeinput` occurs after `textInput`3225 function simulateBeforeInput(element, text, needSimulate) {3226 if (needSimulate)3227 return eventSimulator$9.beforeInput(element, text);3228 return true;3229 }3230 // NOTE: Typing can be prevented in Chrome/Edge but can not be prevented in IE11 or Firefox3231 // Firefox does not support TextInput event3232 // Safari supports the TextInput event but has a bug: e.data is added to the node value.3233 // So in Safari we need to call preventDefault in the last textInput handler but not prevent the Input event3234 function simulateTextInput(element, text) {3235 var forceInputInSafari;3236 function onSafariTextInput(e) {3237 e.preventDefault();3238 forceInputInSafari = true;3239 }3240 function onSafariPreventTextInput(e) {3241 if (e.type === 'textInput')3242 forceInputInSafari = false;3243 }3244 if (browserUtils$8.isSafari) {3245 listeners$2.addInternalEventListener(window, ['textInput'], onSafariTextInput);3246 eventSandbox.on(eventSandbox.EVENT_PREVENTED_EVENT, onSafariPreventTextInput);3247 }3248 var isInputEventRequired = browserUtils$8.isFirefox || eventSimulator$9.textInput(element, text) || forceInputInSafari;3249 if (browserUtils$8.isSafari) {3250 listeners$2.removeInternalEventListener(window, ['textInput'], onSafariTextInput);3251 eventSandbox.off(eventSandbox.EVENT_PREVENTED_EVENT, onSafariPreventTextInput);3252 }3253 return isInputEventRequired || browserUtils$8.isIE11;3254 }3255 function _typeTextToContentEditable(element, text) {3256 var currentSelection = _getSelectionInElement(element);3257 var startNode = currentSelection.startPos.node;3258 var endNode = currentSelection.endPos.node;3259 var needProcessInput = true;3260 var needRaiseInputEvent = true;3261 var textInputData = text;3262 text = text.replace(WHITE_SPACES_RE, String.fromCharCode(160));3263 // NOTE: some browsers raise the 'input' event after the element3264 // content is changed, but in others we should do it manually.3265 var onInput = function () {3266 needRaiseInputEvent = false;3267 };3268 // NOTE: IE11 raises the 'textinput' event many times after the element changed.3269 // The 'textinput' should be called only once3270 function onTextInput(event, dispatched, preventEvent) {...

Full Screen

Full Screen

type-text.js

Source:type-text.js Github

copy

Full Screen

...28 const startParent = nativeMethods.nodeParentNodeGetter.call(startNode);29 const hasStartParent = startParent && startNode.parentElement;30 const browserRequiresSelectionUpdating = browserUtils.isChrome && browserUtils.version < 58 || browserUtils.isSafari;31 if (browserRequiresSelectionUpdating || !hasStartParent || !domUtils.isElementContainsNode(element, startNode)) {32 selection = _getSelectionInElement(element);33 if (textSelection.hasInverseSelectionContentEditable(element)) {34 selection = {35 startPos: selection.endPos,36 endPos: selection.startPos37 };38 }39 }40 selection.endPos.offset = selection.startPos.offset;41 return selection;42}43function _typeTextInElementNode (elementNode, text, offset) {44 const nodeForTyping = document.createTextNode(text);45 const textLength = text.length;46 const selectPosition = { node: nodeForTyping, offset: textLength };47 const parent = nativeMethods.nodeParentNodeGetter.call(elementNode);48 if (domUtils.getTagName(elementNode) === 'br')49 parent.insertBefore(nodeForTyping, elementNode);50 else if (offset > 0)51 elementNode.insertBefore(nodeForTyping, elementNode.childNodes[offset]);52 else53 elementNode.appendChild(nodeForTyping);54 textSelection.selectByNodesAndOffsets(selectPosition, selectPosition);55}56function _typeTextInChildTextNode (element, selection, text) {57 let startNode = selection.startPos.node;58 // NOTE: startNode could be moved or deleted on textInput event. Need ensure startNode.59 if (!domUtils.isElementContainsNode(element, startNode)) {60 selection = _excludeInvisibleSymbolsFromSelection(_getSelectionInElement(element));61 startNode = selection.startPos.node;62 }63 const startOffset = selection.startPos.offset;64 const endOffset = selection.endPos.offset;65 const nodeValue = startNode.nodeValue;66 const selectPosition = { node: startNode, offset: startOffset + text.length };67 startNode.nodeValue = nodeValue.substring(0, startOffset) + text +68 nodeValue.substring(endOffset, nodeValue.length);69 textSelection.selectByNodesAndOffsets(selectPosition, selectPosition);70}71function _excludeInvisibleSymbolsFromSelection (selection) {72 const startNode = selection.startPos.node;73 const startOffset = selection.startPos.offset;74 const endOffset = selection.endPos.offset;75 const firstNonWhitespaceSymbolIndex = contentEditable.getFirstNonWhitespaceSymbolIndex(startNode.nodeValue);76 const lastNonWhitespaceSymbolIndex = contentEditable.getLastNonWhitespaceSymbolIndex(startNode.nodeValue);77 if (startOffset < firstNonWhitespaceSymbolIndex && startOffset !== 0) {78 selection.startPos.offset = firstNonWhitespaceSymbolIndex;79 selection.endPos.offset = endOffset + firstNonWhitespaceSymbolIndex - startOffset;80 }81 else if (endOffset > lastNonWhitespaceSymbolIndex && endOffset !== startNode.nodeValue.length) {82 selection.startPos.offset = startNode.nodeValue.length;83 selection.endPos.offset = endOffset + startNode.nodeValue.length - startOffset;84 }85 return selection;86}87// NOTE: Typing can be prevented in Chrome/Edge but can not be prevented in IE11 or Firefox88// Firefox does not support TextInput event89// Safari supports the TextInput event but has a bug: e.data is added to the node value.90// So in Safari we need to call preventDefault in the last textInput handler but not prevent the Input event91function simulateTextInput (element, text) {92 let forceInputInSafari;93 function onSafariTextInput (e) {94 e.preventDefault();95 forceInputInSafari = true;96 }97 function onSafariPreventTextInput (e) {98 if (e.type === 'textInput')99 forceInputInSafari = false;100 }101 if (browserUtils.isSafari) {102 listeners.addInternalEventListener(window, ['textInput'], onSafariTextInput);103 eventSandbox.on(eventSandbox.EVENT_PREVENTED_EVENT, onSafariPreventTextInput);104 }105 const isInputEventRequired = browserUtils.isFirefox || eventSimulator.textInput(element, text) || forceInputInSafari;106 if (browserUtils.isSafari) {107 listeners.removeInternalEventListener(window, ['textInput'], onSafariTextInput);108 eventSandbox.off(eventSandbox.EVENT_PREVENTED_EVENT, onSafariPreventTextInput);109 }110 return isInputEventRequired || browserUtils.isIE11;111}112function _typeTextToContentEditable (element, text) {113 let currentSelection = _getSelectionInElement(element);114 let startNode = currentSelection.startPos.node;115 const endNode = currentSelection.endPos.node;116 let needProcessInput = true;117 let needRaiseInputEvent = true;118 const textInputData = text;119 text = text.replace(WHITE_SPACES_RE, String.fromCharCode(160));120 // NOTE: some browsers raise the 'input' event after the element121 // content is changed, but in others we should do it manually.122 const onInput = () => {123 needRaiseInputEvent = false;124 };125 // NOTE: IE11 raises the 'textinput' event many times after the element changed.126 // The 'textinput' should be called only once127 function onTextInput (event, dispatched, preventEvent) {...

Full Screen

Full Screen

type-char.js

Source:type-char.js Github

copy

Full Screen

...23function _updateSelectionAfterDeletionContent (element, selection) {24 var startNode = selection.startPos.node;25 var hasStartParent = startNode.parentNode && startNode.parentElement;26 if (browserUtils.isWebKit || !hasStartParent || !domUtils.isElementContainsNode(element, startNode)) {27 selection = _getSelectionInElement(element);28 if (textSelection.hasInverseSelectionContentEditable(element)) {29 selection = {30 startPos: selection.endPos,31 endPos: selection.startPos32 };33 }34 }35 selection.endPos.offset = selection.startPos.offset;36 return selection;37}38function _typeCharInElementNode (elementNode, text) {39 var nodeForTyping = document.createTextNode(text);40 var textLength = text.length;41 var selectPosition = { node: nodeForTyping, offset: textLength };42 if (domUtils.getTagName(elementNode) === 'br')43 elementNode.parentNode.insertBefore(nodeForTyping, elementNode);44 else45 elementNode.appendChild(nodeForTyping);46 textSelection.selectByNodesAndOffsets(selectPosition, selectPosition);47}48function _excludeInvisibleSymbolsFromSelection (selection) {49 var startNode = selection.startPos.node;50 var startOffset = selection.startPos.offset;51 var endOffset = selection.endPos.offset;52 var firstNonWhitespaceSymbolIndex = contentEditable.getFirstNonWhitespaceSymbolIndex(startNode.nodeValue);53 var lastNonWhitespaceSymbolIndex = contentEditable.getLastNonWhitespaceSymbolIndex(startNode.nodeValue);54 if (startOffset < firstNonWhitespaceSymbolIndex && startOffset !== 0) {55 selection.startPos.offset = firstNonWhitespaceSymbolIndex;56 selection.endPos.offset = endOffset + firstNonWhitespaceSymbolIndex - startOffset;57 }58 else if (endOffset > lastNonWhitespaceSymbolIndex && endOffset !== startNode.nodeValue.length) {59 selection.startPos.offset = startNode.nodeValue.length;60 selection.endPos.offset = endOffset + startNode.nodeValue.length - startOffset;61 }62 return selection;63}64function _typeCharToContentEditable (element, text) {65 var currentSelection = _getSelectionInElement(element);66 var startNode = currentSelection.startPos.node;67 var endNode = currentSelection.endPos.node;68 // NOTE: some browsers raise the 'input' event after the element69 // content is changed, but in others we should do it manually.70 var inputEventRaised = false;71 var onInput = () => {72 inputEventRaised = true;73 };74 var afterContentChanged = () => {75 nextTick()76 .then(() => {77 if (!inputEventRaised)78 eventSimulator.input(element);79 listeners.removeInternalEventListener(window, 'input', onInput);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('My first test', async t => {3 const select = Selector('select').nth(0);4 .click(select)5 .click(select.find('option').withText('Firefox'));6});7test('My second test', async t => {8 const select = Selector('select').nth(0);9 .click(select)10 .click(select.find('option').withText('Firefox'));11});12import { Selector } from 'testcafe';

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('Getting started with testcafe', async t => {3 const textInput = Selector('#developer-name');4 .typeText(textInput, 'Peter Parker')5 .click('#tried-test-cafe')6 .click('#submit-button')7 .expect(Selector('#article-header').innerText).eql('Thank you, Peter Parker!');8});9const testCafeCore = require('testcafe/lib/testcafe-core');10const textSelection = testCafeCore.textSelection;11const textInput = Selector('#developer-name');12 .typeText(textInput, 'Peter Parker')13 .click('#tried-test-cafe')14 .click('#submit-button')15 .expect(Selector('#article-header').innerText).eql('Thank you, Peter Parker!');16 const selection = await textSelection.getSelectionInElement(textInput);17 const { start, end } = selection;18 const selectionText = await selection.text;19 const selectionRect = await selection.rect;20 const caretRect = await selection.caretRect;21 const { startPos, endPos } = await selection.positions;22 const { startOffset, endOffset } = await selection.offsets;23 const { startElement, endElement } = await selection.elements;24 const { startNode, endNode } = await selection.nodes;25 const { startNodeRect, endNodeRect } = await selection.nodeRects;26 const { startNodeCaretRect, endNodeCaretRect } = await selection.nodeCaretRects;27 const { startNodeOffset, endNodeOffset } = await selection.nodeOffsets;28 const { startNode

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('Getting selection in element', async t => {3 .typeText('#input', 'test')4 .selectText('#input', 1, 3)5 .pressKey('ctrl+c')6 .click('#output')7 .pressKey('ctrl+v')8 .expect(Selector('#output').value).eql('est');9});10 $(function() {11 $('#input').on('select', function() {12 var sel = window.getSelection();13 console.log(sel.toString());14 });15 });16 Selector('#output') [id="output"]17 at ClientFunctionBuilder._ensureElementExists (C:\Users\testcafe\Desktop\testcafe\testcafe-automation\testcafe-automation\node_modules\testcafe\lib\client-functions\builder.js:187:13)18 at ClientFunctionBuilder._ensureElementExists (C:\Users\testcafe\Desktop\testcafe\testcafe-automation\

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('test', async t => {3 const searchBox = Selector('#lst-ib');4 .expect(searchBox.exists).ok()5 .typeText(searchBox, 'test')6 .expect(searchBox.value).eql('test')7 .pressKey('enter')8 .expect(Selector('title').innerText).eql('test - Google Search');9});10const searchBox = Selector('#searchBox');11 .expect(searchBox.exists).ok()12 .typeText(searchBox, 'test')13 .expect(searchBox.value).eql('test');

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2import { ClientFunction } from 'testcafe';3test('test', async t => {4 const getSelection = ClientFunction(() => {5 const selection = window.getSelection();6 const range = selection.getRangeAt(0);7 const rect = range.getBoundingClientRect();8 return {9 rect: {10 },11 text: selection.toString()12 };13 });14 .typeText(Selector('#editor'), 'hello world')15 .click(Selector('#editor'))16 .pressKey('ctrl+a')17 .expect(getSelection()).eql({18 rect: {19 },20 });21});

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('test', async t => {3 const editor = Selector('.ProseMirror');4 const selection = await editor._getSelectionInElement();5 console.log(selection);6});7{ start: 0, end: 0 }8import { Selector } from 'testcafe';9test('test', async t => {10 const editor = Selector('.ProseMirror');11 const selection = await editor._getSelectionInElement();12 console.log(selection);13});14{ start: 0, end: 0 }15import { Selector } from 'testcafe';16test('test', async t => {17 const editor = Selector('.ProseMirror');18 const selection = await editor._getSelectionInElement();19 console.log(selection);20});21{ start: 0, end: 0 }

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Selector } from 'testcafe';2test('test', async t => {3 .click(Selector('#input1'))4 .pressKey('ctrl+a')5 .pressKey('delete')6 .typeText(Selector('#input1'), '12345')7 .click(Selector('#input2'))8 .pressKey('ctrl+a')9 .pressKey('delete')10 .typeText(Selector('#input2'), '12345')11 .click(Selector('#input3'))12 .pressKey('ctrl+a')13 .pressKey('delete')14 .typeText(Selector('#input3'), '12345')15 .click(Selector('#input4'))16 .pressKey('ctrl+a')17 .pressKey('delete')18 .typeText(Selector('#input4'), '12345')19 .click(Selector('#input5'))20 .pressKey('ctrl+a')21 .pressKey('delete')22 .typeText(Selector('#input5'), '12345')23 .click(Selector('#input6'))24 .pressKey('ctrl+a')25 .pressKey('delete')26 .typeText(Selector('#input6'), '12345')27 .click(Selector('#input7'))28 .pressKey('ctrl+a')29 .pressKey('delete')30 .typeText(Selector('#input7'), '12345')31 .click(Selector('#input8'))32 .pressKey('ctrl+a')33 .pressKey('delete')34 .typeText(Selector('#input8'), '12345')35 .click(Selector('#input9'))36 .pressKey('ctrl+a')37 .pressKey('delete')38 .typeText(Selector('#input9'), '12345')39 .click(Selector('#input10'))40 .pressKey('ctrl+a')41 .pressKey('delete')42 .typeText(Selector('#input10'), '12345')43 .click(Selector('#input11'))44 .pressKey('ctrl+a')45 .pressKey('delete')46 .typeText(Selector('#input11'), '12345')47 .click(Selector('#input12'))48 .pressKey('ctrl+a')49 .pressKey('delete')50 .typeText(Selector('#input12'), '12345')51 .click(Selector('#

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcafe automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful