Best JavaScript code snippet using testcafe
index.js
Source:index.js
...1587 }1588 return contentEditableParent;1589 }1590 //selection utils1591 function getSelectedPositionInParentByOffset(node, offset) {1592 var currentNode = null;1593 var currentOffset = null;1594 var childCount = getChildNodesLength(node.childNodes);1595 var isSearchForLastChild = offset >= childCount;1596 // NOTE: we get a child element by its offset index in the parent1597 if (isShadowUIElement(node))1598 return { node: node, offset: offset };1599 // NOTE: IE behavior1600 if (isSearchForLastChild)1601 currentNode = node.childNodes[childCount - 1];1602 else {1603 currentNode = node.childNodes[offset];1604 currentOffset = 0;1605 }1606 // NOTE: skip shadowUI elements1607 if (isShadowUIElement(currentNode)) {1608 if (childCount <= 1)1609 return { node: node, offset: 0 };1610 isSearchForLastChild = offset - 1 >= childCount;1611 if (isSearchForLastChild)1612 currentNode = node.childNodes[childCount - 2];1613 else {1614 currentNode = node.childNodes[offset - 1];1615 currentOffset = 0;1616 }1617 }1618 // NOTE: we try to find text node1619 while (!isSkippableNode(currentNode) && isElementNode(currentNode)) {1620 var visibleChildren = getVisibleChildren(currentNode);1621 if (visibleChildren.length)1622 currentNode = visibleChildren[isSearchForLastChild ? visibleChildren.length - 1 : 0];1623 else {1624 //NOTE: if we didn't find a text node then always set offset to zero1625 currentOffset = 0;1626 break;1627 }1628 }1629 if (currentOffset !== 0 && !isSkippableNode(currentNode))1630 currentOffset = currentNode.nodeValue ? currentNode.nodeValue.length : 0;1631 return {1632 node: currentNode,1633 offset: currentOffset1634 };1635 }1636 function getSelectionStart(el, selection, inverseSelection) {1637 var startNode = inverseSelection ? selection.focusNode : selection.anchorNode;1638 var startOffset = inverseSelection ? selection.focusOffset : selection.anchorOffset;1639 var correctedStartPosition = {1640 node: startNode,1641 offset: startOffset1642 };1643 //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it1644 if ((isTheSameNode(el, startNode) || isElementNode(startNode)) && hasSelectableChildren(startNode))1645 correctedStartPosition = getSelectedPositionInParentByOffset(startNode, startOffset);1646 return {1647 node: correctedStartPosition.node,1648 offset: correctedStartPosition.offset1649 };1650 }1651 function getSelectionEnd(el, selection, inverseSelection) {1652 var endNode = inverseSelection ? selection.anchorNode : selection.focusNode;1653 var endOffset = inverseSelection ? selection.anchorOffset : selection.focusOffset;1654 var correctedEndPosition = {1655 node: endNode,1656 offset: endOffset1657 };1658 //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it1659 if ((isTheSameNode(el, endNode) || isElementNode(endNode)) && hasSelectableChildren(endNode))1660 correctedEndPosition = getSelectedPositionInParentByOffset(endNode, endOffset);1661 return {1662 node: correctedEndPosition.node,1663 offset: correctedEndPosition.offset1664 };1665 }1666 function getSelection(el, selection, inverseSelection) {1667 return {1668 startPos: getSelectionStart(el, selection, inverseSelection),1669 endPos: getSelectionEnd(el, selection, inverseSelection)1670 };1671 }1672 function getSelectionStartPosition(el, selection, inverseSelection) {1673 var correctedSelectionStart = getSelectionStart(el, selection, inverseSelection);1674 return calculatePositionByNodeAndOffset(el, correctedSelectionStart);...
content_editable_helper.js
Source:content_editable_helper.js
...198 }199 return contentEditableParent;200 };201 //selection utils202 function getSelectedPositionInParentByOffset(node, offset) {203 var currentNode = null,204 currentOffset = null,205 isSearchForLastChild = offset >= node.childNodes.length;206 //NOTE: IE behavior207 if (isSearchForLastChild)208 currentNode = node.childNodes[node.childNodes.length - 1];209 else {210 currentNode = node.childNodes[offset];211 currentOffset = 0;212 }213 while (Util.isRenderedNode(currentNode) && currentNode.nodeType === 1) {214 if (currentNode.childNodes && currentNode.childNodes.length)215 currentNode = currentNode.childNodes[isSearchForLastChild ? currentNode.childNodes.length - 1 : 0];216 else {217 //NOTE: if we didn't find a text node then always set offset to zero218 currentOffset = 0;219 break;220 }221 }222 if (currentOffset !== 0 && Util.isRenderedNode(currentNode))223 currentOffset = currentNode.nodeValue ? currentNode.nodeValue.length : 0;224 return {225 node: currentNode,226 offset: currentOffset227 };228 }229 function getSelectionStart(el, selection, inverseSelection) {230 var startNode = inverseSelection ? selection.focusNode : selection.anchorNode,231 startOffset = inverseSelection ? selection.focusOffset : selection.anchorOffset,232 correctedStartPosition = {233 node: startNode,234 offset: startOffset235 };236 //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it237 if ((Util.isTheSameNode(el, startNode) || startNode.nodeType === 1) && startNode.childNodes && startNode.childNodes.length)238 correctedStartPosition = getSelectedPositionInParentByOffset(startNode, startOffset);239 return {240 node: correctedStartPosition.node,241 offset: correctedStartPosition.offset242 };243 }244 function getSelectionEnd(el, selection, inverseSelection) {245 var endNode = inverseSelection ? selection.anchorNode : selection.focusNode,246 endOffset = inverseSelection ? selection.anchorOffset : selection.focusOffset,247 correctedEndPosition = {248 node: endNode,249 offset: endOffset250 };251 //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it252 if ((Util.isTheSameNode(el, endNode) || endNode.nodeType === 1) && endNode.childNodes && endNode.childNodes.length)253 correctedEndPosition = getSelectedPositionInParentByOffset(endNode, endOffset);254 return {255 node: correctedEndPosition.node,256 offset: correctedEndPosition.offset257 };258 }259 exports.getSelection = function (el, selection, inverseSelection) {260 var correctedStart = getSelectionStart(el, selection, inverseSelection),261 correctedEnd = getSelectionEnd(el, selection, inverseSelection);262 return {263 startNode: correctedStart.node,264 startOffset: correctedStart.offset,265 endNode: correctedEnd.node,266 endOffset: correctedEnd.offset267 };...
content-editable.js
Source:content-editable.js
...248 offset: startOffset249 };250 //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it251 if ((domUtils.isTheSameNode(el, startNode) || domUtils.isElementNode(startNode)) && hasChildren(startNode))252 correctedStartPosition = getSelectedPositionInParentByOffset(startNode, startOffset);253 return {254 node: correctedStartPosition.node,255 offset: correctedStartPosition.offset256 };257}258function getSelectionEnd (el, selection, inverseSelection) {259 var endNode = inverseSelection ? selection.anchorNode : selection.focusNode;260 var endOffset = inverseSelection ? selection.anchorOffset : selection.focusOffset;261 var correctedEndPosition = {262 node: endNode,263 offset: endOffset264 };265 //NOTE: window.getSelection() can't returns not rendered node like selected node, so we shouldn't check it266 if ((domUtils.isTheSameNode(el, endNode) || domUtils.isElementNode(endNode)) && hasChildren(endNode))267 correctedEndPosition = getSelectedPositionInParentByOffset(endNode, endOffset);268 return {269 node: correctedEndPosition.node,270 offset: correctedEndPosition.offset271 };272}273export function getSelection (el, selection, inverseSelection) {274 return {275 startPos: getSelectionStart(el, selection, inverseSelection),276 endPos: getSelectionEnd(el, selection, inverseSelection)277 };278}279export function getSelectionStartPosition (el, selection, inverseSelection) {280 var correctedSelectionStart = getSelectionStart(el, selection, inverseSelection);281 return calculatePositionByNodeAndOffset(el, correctedSelectionStart);...
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My Test', async t => {3 .click('#tried-test-cafe')4 .click('#comments')5 .typeText('#comments', 'Hello, world!')6 .click('#tried-test-cafe')7 .click('#submit-button');8});9import { Selector } from 'testcafe';10test('My Test', async t => {11 .click('#tried-test-cafe')12 .click('#comments')13 .typeText('#comments', 'Hello, world!')14 .click('#tried-test-cafe')15 .click('#submit-button');16});17import { Selector } from 'testcafe';18test('My Test', async t => {19 .click('#tried-test-cafe')20 .click('#comments')21 .typeText('#comments', 'Hello, world!')22 .click('#tried-test-cafe')23 .click('#submit-button');24});25import { Selector } from 'testcafe';26test('My Test', async t => {27 .click('#tried-test-cafe')28 .click('#comments')29 .typeText('#comments', 'Hello, world!')30 .click('#tried-test-cafe')31 .click('#submit-button');32});33import { Selector } from 'testcafe';34test('My Test', async t => {35 .click('#tried-test
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My test', async t => {3 const text = Selector('label').withText('I have tried TestCafe');4 const position = await text.getSelectedPositionInParentByOffset(5, 10);5 console.log(position);6});7{ x: 5, y: 10 }
Using AI Code Generation
1import { Selector } from 'testcafe';2test('Getting selected position in parent by offset', async t => {3 const searchInput = Selector('.gLFyf');4 .typeText(searchInput, 'testcafe')5 .expect(searchInput.getSelectedPositionInParentByOffset(0)).eql(0)6 .expect(searchInput.getSelectedPositionInParentByOffset(1)).eql(1)7 .expect(searchInput.getSelectedPositionInParentByOffset(2)).eql(2)8 .expect(searchInput.getSelectedPositionInParentByOffset(3)).eql(3)9 .expect(searchInput.getSelectedPositionInParentByOffset(4)).eql(4)10 .expect(searchInput.getSelectedPositionInParentByOffset(5)).eql(5)11 .expect(searchInput.getSelectedPositionInParentByOffset(6)).eql(6)12 .expect(searchInput.getSelectedPositionInParentByOffset(7)).eql(7)13 .expect(searchInput.getSelectedPositionInParentByOffset(8)).eql(8)14 .expect(searchInput.getSelectedPositionInParentByOffset(9)).eql(9)15 .expect(searchInput.getSelectedPositionInParentByOffset(10)).eql(10)16 .expect(searchInput.getSelectedPositionInParentByOffset(11)).eql(11)17 .expect(searchInput.getSelectedPositionInParentByOffset(12)).eql(12)18 .expect(searchInput.getSelectedPositionInParentByOffset(13)).eql(13);19});
Using AI Code Generation
1import {Selector} from 'testcafe';2test('Getting Selected Position in Parent by Offset', async t => {3 .click(Selector('input').withAttribute('name', 'q'))4 .typeText(Selector('input').withAttribute('name', 'q'), 'TestCafe')5 .pressKey('enter')6 .expect(Selector('div').withAttribute('id', 'resultStats').getSelectedPositionInParentByOffset()).eql(0);7});8import {Selector} from 'testcafe';9test('Getting Selected Position in Parent by Offset', async t => {10 .click(Selector('input').withAttribute('name', 'q'))11 .typeText(Selector('input').withAttribute('name', 'q'), 'TestCafe')12 .pressKey('enter')13 .expect(Selector('div').withAttribute('id', 'resultStats').getSelectedPositionInParentByOffset()).eql(0);14});15[getSelectedText Method](../../../test-api/dom-node-state/getselectedtext.md)16[Type Text Action](../../../test-api/actions/type-text.md)17[Select Text Action](../../../test-api/actions/select-text.md)18[Press Key Action](../../../test-api/actions/press-key.md)19[Type Action](../../../test-api/actions/type.md)
Using AI Code Generation
1import { Selector } from 'testcafe';2const getSelectedPositionInParentByOffset = Selector(() => {3 const selection = window.getSelection();4 const range = selection.getRangeAt(0);5 const parent = range.startContainer;6 const offset = range.startOffset;7 return { parent, offset };8});9test('test', async t => {10 .click('#tryhome')11 .click('#iframewrapper')12 .switchToIframe('#iframeResult')13 .selectTextAreaContent('#textarea1')14 .pressKey('backspace')15 .expect(getSelectedPositionInParentByOffset().offset).eql(0)16 .pressKey('backspace')17 .expect(getSelectedPositionInParentByOffset().offset).eql(0)18 .pressKey('backspace')19 .expect(getSelectedPositionInParentByOffset().offset).eql(0);20});21import { Selector } from 'testcafe';22const getSelectedPositionInParentByOffset = Selector(() => {23 const selection = window.getSelection();24 const range = selection.getRangeAt(0);25 const parent = range.startContainer;26 const offset = range.startOffset;27 return { parent, offset };28});29test('test', async t => {30 .click('#tryhome')31 .click('#iframewrapper')32 .switchToIframe('#iframeResult')33 .selectTextAreaContent('#textarea1')34 .pressKey('backspace')35 .expect(getSelectedPositionInParentByOffset().offset).eql(0)36 .pressKey('backspace')37 .expect(getSelectedPositionInParentByOffset().offset).eql(0)38 .pressKey('backspace')39 .expect(getSelectedPositionInParentByOffset().offset).eql(0);40});
Using AI Code Generation
1test('Getting selected position in parent by offset', async t => {2 .typeText('#input', 'test')3 .selectText('#input')4 .expect(getSelectedPositionInParentByOffset('#input', 0).left).eql(0)5 .expect(getSelectedPositionInParentByOffset('#input', 0).top).eql(0)6 .expect(getSelectedPositionInParentByOffset('#input', 1).left).eql(7)7 .expect(getSelectedPositionInParentByOffset('#input', 1).top).eql(0)8 .expect(getSelectedPositionInParentByOffset('#input', 2).left).eql(14)9 .expect(getSelectedPositionInParentByOffset('#input', 2).top).eql(0)10 .expect(getSelectedPositionInParentByOffset('#input', 3).left).eql(21)11 .expect(getSelectedPositionInParentByOffset('#input', 3).top).eql(0)12 .expect(getSelectedPositionInParentByOffset('#input', 4).left).eql(28)13 .expect(getSelectedPositionInParentByOffset('#input', 4).top).eql(0)14 .expect(getSelectedPositionInParentByOffset('#input', 5).left).eql(35)15 .expect(getSelectedPositionInParentByOffset('#input', 5).top).eql(0)16});
Using AI Code Generation
1import {2} from 'testcafe';3test('Getting Selected Position', async t => {4 const getSelection = ClientFunction(() => {5 const selection = window.getSelection();6 const anchorNode = selection.anchorNode;7 const anchorOffset = selection.anchorOffset;8 const focusNode = selection.focusNode;9 const focusOffset = selection.focusOffset;10 return {11 };12 });13 const selection = await getSelection();14 console.log(selection);15});16const hammerhead = window.getTestCafeModule('hammerhead');17const testCafeCore = window.getTestCafeModule('testCafeCore');18const domUtils = hammerhead.utils.dom;19const contentEditable = testCafeCore.contentEditable;20const positionUtils = testCafeCore.positionUtils;21const textSelection = testCafeCore.textSelection;22const textSelectionUtils = testCafeCore.textSelectionUtils;23const textSelectionPosition = testCafeCore.textSelectionPosition;24domUtils.getSelectedPositionInParentByOffset = function (startNode, startOffset, endNode, endOffset) {25 var startPos = textSelectionPosition.createTextPositionByElement(startNode, startOffset);26 var endPos = textSelectionPosition.createTextPositionByElement(endNode, endOffset);27 if (startPos === null || endPos === null)28 return null;29 var parent = startPos.node.parentNode;30 if (startPos.node !== endPos.node) {31 var startPosInParent = positionUtils.getPositionInParent(startPos.node);32 var endPosInParent = positionUtils.getPositionInParent(endPos.node);33 if (startPosInParent === null || endPosInParent === null)34 return null;35 startPos = textSelectionPosition.createTextPositionByElement(parent, startPosInParent);36 endPos = textSelectionPosition.createTextPositionByElement(parent, endPosInParent);37 }38 return textSelection.createTextSelection(startPos, endPos);39};40export default hammerhead;41const createTestCafe = require('testcafe');42const testCafeRunner = require('
Using AI Code Generation
1import { Selector } from 'testcafe';2test('Getting selected position in parent', async t => {3 const getSelectedPositionInParentByOffset = Selector(() => {4 const { getSelectedPositionInParentByOffset } = window.getTestCafeModule('select-text');5 return getSelectedPositionInParentByOffset();6 });7 .click('#input')8 .pressKey('ctrl+a delete')9 .typeText('#input', 'test')10 .click('#input', { caretPos: 2 })11 .expect(getSelectedPositionInParentByOffset()).eql({ start: 2, end: 2 });12});
Using AI Code Generation
1import { Selector, t } from 'testcafe';2const text = Selector('div').withText('Hello World');3test('My first test', async t => {4 .click(text)5 .expect(text.value).eql('Hello World')6 .expect(text.getSelectedPositionInParentByOffset(2)).eql({ x: 0, y: 0 })7 .expect(text.getSelectedPositionInParentByOffset(12)).eql({ x: 0, y: 0 })8 .expect(text.getSelectedPositionInParentByOffset(13)).eql({ x: 0, y: 0 })9 .expect(text.getSelectedPositionInParentByOffset(14)).eql({ x: 0, y: 0 })10 .expect(text.getSelectedPositionInParentByOffset(15)).eql({ x: 0, y: 0 })11 .expect(text.getSelectedPositionInParentByOffset(16)).eql({ x: 0, y: 0 })12 .expect(text.getSelectedPositionInParentByOffset(17)).eql({ x: 0, y: 0 })13 .expect(text.getSelectedPositionInParentByOffset(18)).eql({ x: 0, y: 0 })14 .expect(text.getSelectedPositionInParentByOffset(19)).eql({ x: 0, y: 0 })15 .expect(text.getSelectedPositionInParentByOffset(20)).eql({ x: 0, y: 0 })16 .expect(text.getSelectedPositionInParentByOffset(21)).eql({ x: 0, y: 0 })17 .expect(text.getSelectedPositionInParentByOffset(22)).eql({ x: 0, y: 0 })18 .expect(text.getSelectedPositionInParentByOffset(23)).eql({ x: 0, y: 0 })19 .expect(text.getSelectedPositionInParentByOffset(24)).eql({ x: 0, y: 0 })20 .expect(text.getSelectedPositionInParentByOffset(25)).eql({ x: 0, y: 0 })21 .expect(text.getSelectedPositionInParentByOffset(26)).eql({ x: 0,
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!!