Best JavaScript code snippet using testcafe
position.js
Source:position.js  
...62            return rectangle;63        }64        return null;65    }66    function getMapElementRectangle(el) {67        var mapContainer = DOM.getMapContainer(el);68        if (mapContainer) {69            if (/^map$/i.test(el.tagName))70                return exports.getElementRectangle(mapContainer);71            else if (/^area$/i.test(el.tagName)) {72                var areaElementRectangle = getAreaElementRectangle(el, mapContainer);73                if (areaElementRectangle)74                    return areaElementRectangle;75            }76        }77        return {78            height: 0,79            left: 0,80            top: 0,81            width: 082        };83    }84    exports.checkPresenceInRectangle = function (point, rectangle) {85        return point.x >= rectangle.left && point.x <= rectangle.right && point.y >= rectangle.top && point.y <= rectangle.bottom;86    };87    // TODO: equal to offsetToClientCoords88    exports.clientToOffsetCoord = function (coords, currentDocument) {89        var $doc = $(currentDocument || document);90        return {91            x: coords.x + $doc.scrollLeft(),92            y: coords.y + $doc.scrollTop()93        };94    };95    exports.getClientDimensions = function (target) {96        if (!DOM.isDomElement(target)) {97            var clientPoint = exports.offsetToClientCoords(target);98            return {99                border: {100                    bottom: 0,101                    left: 0,102                    right: 0,103                    top: 0104                },105                bottom: clientPoint.y,106                height: 0,107                left: clientPoint.x,108                right: clientPoint.x,109                scroll: {110                    left: 0,111                    top: 0112                },113                top: clientPoint.y,114                width: 0115            };116        }117        var $target = $(target),118            isHtmlElement = /html/i.test(target.tagName),119            body = isHtmlElement ? $target.find('body')[0] : null,120            elementBorders = Style.getBordersWidth($target),121            elementRect = target.getBoundingClientRect(),122            elementScroll = Style.getElementScroll($target),123            isElementInIFrame = DOM.isElementInIframe(target),124            elementLeftPosition = isHtmlElement ? 0 : elementRect.left,125            elementTopPosition = isHtmlElement ? 0 : elementRect.top,126            elementHeight = isHtmlElement ? target.clientHeight : elementRect.height,127            elementWidth = isHtmlElement ? target.clientWidth : elementRect.width;128        if (isHtmlElement && isIFrameWithoutSrc && body) {129            elementHeight = body.clientHeight;130            elementWidth = body.clientWidth;131        }132        if (isElementInIFrame) {133            var iFrameElement = DOM.getIFrameByElement(target);134            if (iFrameElement) {135                var iFrameOffset = exports.getOffsetPosition(iFrameElement),136                    clientOffset = exports.offsetToClientCoords({137                        x: iFrameOffset.left,138                        y: iFrameOffset.top139                    }),140                    iFrameBorders = Style.getBordersWidth($(iFrameElement));141                elementLeftPosition += clientOffset.x + iFrameBorders.left;142                elementTopPosition += clientOffset.y + iFrameBorders.top;143                if (isHtmlElement) {144                    elementBorders.bottom = elementBorders.bottom + iFrameBorders.bottom;145                    elementBorders.left = elementBorders.left + iFrameBorders.left;146                    elementBorders.right = elementBorders.right + iFrameBorders.right;147                    elementBorders.top = elementBorders.top + iFrameBorders.top;148                }149            }150        }151        return {152            border: elementBorders,153            bottom: elementTopPosition + elementHeight,154            height: elementHeight,155            left: elementLeftPosition,156            right: elementLeftPosition + elementWidth,157            scroll: {158                left: elementScroll.left,159                top: elementScroll.top160            },161            scrollbar: {162                right: isHtmlElement || $target.innerWidth() === target.clientWidth ? 0 : DOM.getScrollbarSize(),163                bottom: isHtmlElement || $target.innerHeight() === target.clientHeight ? 0 : DOM.getScrollbarSize()164            },165            top: elementTopPosition,166            width: elementWidth167        };168    };169    exports.getElementClientRectangle = function (el) {170        var rect = exports.getElementRectangle(el),171            clientPos = exports.offsetToClientCoords({172                x: rect.left,173                y: rect.top174            });175        return {176            height: rect.height,177            left: clientPos.x,178            top: clientPos.y,179            width: rect.width180        };181    };182    //TODO: remove the skipIFramesDeeping flag183    exports.getElementFromPoint = function (x, y, currentDocument, skipIFramesDeeping) {184        var el = null;185        currentDocument = currentDocument || document;186        try {187            // Permission denied to access property 'getElementFromPoint' error in iFrame188            el = (currentDocument.getElementFromPoint || currentDocument.elementFromPoint).call(currentDocument, x, y);189        } catch (ex) {190            return null;191        }192        //NOTE: elementFromPoint returns null when is's a border of an iframe193        if (el === null)194            el = (currentDocument.getElementFromPoint || currentDocument.elementFromPoint).call(currentDocument, x - 1, y - 1);195        if (el && el.tagName.toLowerCase() === 'iframe' && !skipIFramesDeeping) {196            var iframeDocument = null;197            try {198                iframeDocument = $(el).contents()[0];199            } catch (e) {200                //cross-domain iframe201            }202            if (iframeDocument) {203                var iframePosition = exports.getOffsetPosition(el),204                    iframeClientPosition = exports.offsetToClientCoords({205                        x: iframePosition.left,206                        y: iframePosition.top207                    }, currentDocument),208                    iframeBorders = Style.getBordersWidth($(el)),209                    iframePadding = Style.getElementPadding($(el));210                el = exports.getElementFromPoint(211                    x - iframeClientPosition.x - iframeBorders.left - iframePadding.left,212                    y - iframeClientPosition.y - iframeBorders.top - iframePadding.top,213                    iframeDocument214                ) || el;215            }216        }217        return el;218    };219    exports.getElementRectangle = function (el) {220        var rectangle = {};221        if (DOM.isMapElement(el))222            rectangle = getMapElementRectangle(el);223        else if (Style.isVisibleChild(el))224            rectangle = exports.getSelectChildRectangle(el);225        else {226            var elementOffset = exports.getOffsetPosition(el),227                relativeRectangle = DOM.isSvgElement(el) ? exports.getSvgElementRelativeRectangle(el) : el.getBoundingClientRect();228            rectangle = {229                height: relativeRectangle.height,230                left: elementOffset.left,231                top: elementOffset.top,232                width: relativeRectangle.width233            };234        }235        rectangle.height = Math.round(rectangle.height);236        rectangle.left = Math.round(rectangle.left);237        rectangle.top = Math.round(rectangle.top);238        rectangle.width = Math.round(rectangle.width);239        return rectangle;240    };241    exports.getElementRectangleForMarking = function (element, padding, borderWidth) {242        var elementRectangle = exports.getElementRectangle(element),243            rectPadding = padding || 0,244            top = elementRectangle.top - rectPadding < 0 ? borderWidth / 2 : elementRectangle.top - rectPadding,245            left = elementRectangle.left - rectPadding < 0 ? borderWidth / 2 : elementRectangle.left - rectPadding,246            width = Math.min(247                elementRectangle.left - rectPadding < 0 ?248                    Math.max(elementRectangle.width + elementRectangle.left + rectPadding - left, 0) :249                    elementRectangle.width + rectPadding * 2,250                Style.getDocumentElementWidth() - borderWidth <= 1 ? 1 : Style.getDocumentElementWidth() - borderWidth),251            height = Math.min(252                elementRectangle.top - rectPadding < 0 ?253                    Math.max(elementRectangle.height + elementRectangle.top + rectPadding - top, 0) :254                    elementRectangle.height + rectPadding * 2,255                Style.getDocumentElementHeight() - borderWidth <= 1 ? 1 : Style.getDocumentElementHeight() - borderWidth);256        return {257            height: height,258            left: left,259            top: top,260            width: width261        };262    };263    exports.getEventAbsoluteCoordinates = function (ev) {264        var el = ev.target || ev.srcElement,265            pageCoordinates = exports.getEventPageCoordinates(ev),266            curDocument = DOM.findDocument(el),267            xOffset = 0,268            yOffset = 0;269        if (DOM.isElementInIframe(curDocument.documentElement)) {270            var currentIFrame = DOM.getIFrameByElement(curDocument);271            if (currentIFrame) {272                var iFrameOffset = exports.getOffsetPosition(currentIFrame),273                    iFrameBorders = Style.getBordersWidth($(currentIFrame));274                xOffset = iFrameOffset.left + iFrameBorders.left;275                yOffset = iFrameOffset.top + iFrameBorders.top;276            }277        }278        return {279            x: pageCoordinates.x + xOffset,280            y: pageCoordinates.y + yOffset281        };282    };283    exports.getEventPageCoordinates = function (ev) {284        var curCoordObject = /^touch/.test(ev.type) && ev.targetTouches ? (ev.targetTouches[0] || ev.changedTouches[0]) : ev;285        if ((curCoordObject.pageX === null || (curCoordObject.pageX === 0 && curCoordObject.pageY === 0 &&286            (curCoordObject.clientX !== 0 || curCoordObject.clientY !== 0))) && curCoordObject.clientX !== null) {287            var currentDocument = DOM.findDocument(ev.target || ev.srcElement),288                html = currentDocument.documentElement,289                body = currentDocument.body;290            return {291                x: Math.round(curCoordObject.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0)),292                y: Math.round(curCoordObject.clientY + (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0))293            };294        }295        return {296            x: Math.round(curCoordObject.pageX),297            y: Math.round(curCoordObject.pageY)298        };299    };300    exports.getFixedPosition = function (pos, iFrameWin, convertToClient) {301        if (!iFrameWin)302            return pos;303        var iFrame = DOM.getIFrameByWindow(iFrameWin),304            iFrameOffset = exports.getOffsetPosition(iFrame),305            iFrameBorders = Style.getBordersWidth($(iFrame)),306            iFramePadding = Style.getElementPadding($(iFrame)),307            documentScroll = Style.getElementScroll($(document));308        return {309            x: pos.x + iFrameOffset.left + iFrameBorders.left + iFramePadding.left - (convertToClient ? documentScroll.left : 0),310            y: pos.y + iFrameOffset.top + iFrameBorders.top + iFramePadding.top - (convertToClient ? documentScroll.top : 0)311        };312    };313    exports.getFixedPositionForIFrame = function (pos, iFrameWin) {314        var iFrame = DOM.getIFrameByWindow(iFrameWin),315            iFrameOffset = exports.getOffsetPosition(iFrame),316            iFrameBorders = Style.getBordersWidth($(iFrame)),317            iFramePadding = Style.getElementPadding($(iFrame));318        return {319            x: pos.x - iFrameOffset.left - iFrameBorders.left - iFramePadding.left,320            y: pos.y - iFrameOffset.top - iFrameBorders.top - iFramePadding.top321        };322    };323    exports.getIFrameCoordinates = function (iFrameWin) {324        var iFrame = DOM.getIFrameByWindow(iFrameWin),325            $IFrame = $(iFrame),326            iFrameOffset = exports.getOffsetPosition(iFrame),327            iFrameBorders = Style.getBordersWidth($IFrame),328            iFramePadding = Style.getElementPadding($IFrame),329            iFrameRectangleLeft = iFrameOffset.left + iFrameBorders.left + iFramePadding.left,330            iFrameRectangleTop = iFrameOffset.top + iFrameBorders.top + iFramePadding.top;331        return {332            bottom: iFrameRectangleTop + $IFrame.height(),333            left: iFrameRectangleLeft,334            right: iFrameRectangleLeft + $IFrame.width(),335            top: iFrameRectangleTop336        };337    };338    exports.getOffsetPosition = function (el) {339        if (DOM.isMapElement(el)) {340            var rectangle = getMapElementRectangle(el);341            return {342                left: rectangle.left,343                top: rectangle.top344            };345        }346        var doc = DOM.findDocument(el),347            isInIFrame = DOM.isElementInIframe(el, doc),348            currentIFrame = isInIFrame ? DOM.getIFrameByElement(doc) : null,349            offsetPosition = doc === el ? $(doc.documentElement).offset() : $(el).offset(),350            relativeRectangle = null;351        // NOTE: jquery .offset() function doesn't take body's border into account (except IE7)352        // http://bugs.jquery.com/ticket/7948353        //NOTE: Sometimes in IE method getElementFromPoint returns cross-domain iframe's documentElement, but we can't get his body354        var borders = doc.body ? Style.getBordersWidth($(doc.body)) : {...Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3    const getMapElementRectangle = Selector(() => {4        return window.getMapElementRectangle();5    });6    const { left, top } = await getMapElementRectangle();7        .click('#map')8        .click('#map', { offsetX: left, offsetY: top });9});10    <div id="map" style="width: 500px; height: 500px;"></div>11        window.onload = function () {12            var map = new google.maps.Map(document.getElementById('map'), {13                center: { lat: 53.9, lng: 27.56667 }14            });15            var marker = new RichMarker({16                position: { lat: 53.9, lng: 27.56667 },17            });18            window.getMapElementRectangle = function () {19                return marker.get('anchor').getBoundingClientRect();20            };21        };22import { Selector } fromUsing AI Code Generation
1import { Selector } from 'testcafe';2import { ClientFunction } from 'testcafe';3test('My first test', async t => {4        .typeText('#lst-ib', 'TestCafe')5        .click('#tsbb')6        .click('#rso > div:nth-child(2) > div > div > div > div >Using AI Code Generation
1import { Selector } from 'testcafe';2test('Getting element\'s rectangle', async t => {3    const button = Selector('#populate');4    const buttonRect = await button.getMapElementRectangle(0);5    console.log(buttonRect.x);6    console.log(buttonRect.y);7    console.log(buttonRect.width);8    console.log(buttonRect.height);9});10import { Selector } from 'testcafe';11test('Getting element\'s screenshot', async t => {12    const button = Selector('#populate');13    const buttonScreenshot = await button.getElementScreenshot();14    console.log(buttonScreenshot);15});16import { Selector } from 'testcafe';17test('Getting element\'s bounding rectangle', async t => {18    const button = Selector('#populate');19    const buttonBoundingRect = await button.getBoundingClientRect();20    console.log(buttonBoundingRect.x);21    console.log(buttonBoundingRect.y);22    console.log(buttonBoundingRect.width);23    console.log(buttonBoundingRect.height);24});25import { Selector } from 'testcafe';26test('Getting element\'s client rectangles', async t => {27    const button = Selector('#populate');28    const buttonClientRects = await button.getClientRects();29    console.log(buttonClientRects);30});31import { Selector } from 'testcafe';32test('Getting element\'s bounding rectangle', async t => {33    const button = Selector('#populate');34    const buttonBoundingRect = await button.getBoundingClientRect();35    console.log(buttonBoundingRect.xUsing AI Code Generation
1import { Selector } from 'testcafe';2test('Getting map element rectangle', async t => {3    const map = Selector('.widget-scene-canvas').with({ visibilityCheck: true });4    const mapRectangle = await t.getMapElementRectangle(map);5    console.log(mapRectangle);6});7import { writeFile } from 'fs';8import { ensureDir } from 'fs-extra';9import { createWriteStream } from 'fs';10import { join } from 'path';11import { mkdir } from 'shelljs';12import { Writable } from 'stream';13import { createStream } from 'table';14import { EOL } from 'os';15import { createGzip } from 'zlib';16import { createWriteStream as createWriteStreamAsync } from 'fs';17import { promisify } from 'util';18import { createWriteStream as createWriteStreamSync } from 'fs';19import { writeFileSync } from 'fs';20import { createWriteStream as createWriteStreamSync }Using AI Code Generation
1import { Selector } from 'testcafe';2test('Getting map element rectangle', async t => {3    const map = Selector('#map');4    const mapRect = await map.getBoundingClientRect();5    console.log(mapRect);6});7const map = Selector('#map');8const mapRect = await map.getBoundingClientRect();9console.log(mapRect);10const map = Selector('#map');11const mapRect = await map.getBoundingClientRect();12console.log(mapRect);13{ left: 8,14  y: 8 }Using AI Code Generation
1import { Selector } from 'testcafe';2test('Getting rectangle of a map element', async t => {3    const searchBox = Selector('#searchboxinput');4    const searchBoxRectangle = await searchBox.getBoundingClientRect();5    console.log(searchBoxRectangle);6});Using AI Code Generation
1import { Selector } from 'testcafe';2test('Get Map Element Rectangle', async t => {3    var mapElement = Selector('.map');4    var rect = await mapElement.getBoundingClientRect();5    console.log(rect);6});7var createTestRunError = function (err) {8    var mapElement = Selector('.map');9    var rect = await mapElement.getBoundingClientRect();10    console.log(rect);11    return {12    };13};14var createErrorDecorator = function (fn) {15    return function () {16        try {17            return fn.apply(this, arguments);18        }19        catch (err) {20            if (err.isTestCafeError)21                throw err;22            throw createTestRunError(err);23        }24    };25};26module.exports = {27    'reportTaskStart': createErrorDecorator(function (startTime, userAgents, testCount) {28        var self = this;29        this.startTime = startTime;30        this.testCount = testCount;31        this.write('<!DOCTYPE html><html><head><title>TestCafe Report</title>');Using AI Code Generation
1import { Selector } from 'testcafe';2test('TestCafe', async t => {3    const element = Selector('#element');4    const rect = await t.getMapElementRectangle(element);5});6import { Selector } from 'testcafe';7test('TestCafe', async t => {8    const element = Selector('#element');9    const rect = await t.getMapElementRectangle(element);10});11import { Selector } from 'testcafe';12test('TestCafe', async t => {13    const element = Selector('#element');14    const rect = await t.getMapElementRectangle(element);15});16import { Selector } from 'testcafe';17test('TestCafe', async t => {18    const element = Selector('#element');19    const rect = await t.getMapElementRectangle(element);20});21import { 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!!
