How to use checkBrowserHotkey method in Testcafe

Best JavaScript code snippet using testcafe

Run Testcafe automation tests on LambdaTest cloud grid

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

prevent-real-events.js

Source: prevent-real-events.js Github

copy
1import { utils, eventSandbox } from './deps/hammerhead';
2
3import { get, hasDimensions } from './utils/style';
4import { filter } from './utils/array';
5import { isShadowUIElement, isWindow, getParents } from './utils/dom';
6
7var browserUtils   = utils.browser;
8var listeners      = eventSandbox.listeners;
9var eventSimulator = eventSandbox.eventSimulator;
10
11const PREVENTED_EVENTS = [
12    'click', 'mousedown', 'mouseup', 'dblclick', 'contextmenu', 'mousemove', 'mouseover', 'mouseout',
13    'touchstart', 'touchmove', 'touchend', 'keydown', 'keypress', 'input', 'keyup', 'change', 'focus', 'blur',
14    'MSPointerDown', 'MSPointerMove', 'MSPointerOver', 'MSPointerOut', 'MSPointerUp', 'pointerdown',
15    'pointermove', 'pointerover', 'pointerout', 'pointerup'
16];
17
18const F12_KEY_CODE = 123;
19
20
21function checkBrowserHotkey (e) {
22    // NOTE: Opening browser tools with F12, CTRL+SHIFT+<SYMBOL KEY>
23    // on PC or with OPTION(ALT)+CMD+<SYMBOL KEY> on Mac.
24    return e.shiftKey && e.ctrlKey || (e.altKey || e.metaKey) && browserUtils.isMacPlatform || e.keyCode === F12_KEY_CODE;
25}
26
27// NOTE: when tests are running, we should block real events (from mouse
28// or keyboard), because they may lead to unexpected test result.
29function preventRealEventHandler (e, dispatched, preventDefault, cancelHandlers, stopEventPropagation) {
30    var target = e.target || e.srcElement;
31
32    if (!dispatched && !isShadowUIElement(target)) {
33        // NOTE: this will allow pressing hotkeys to open developer tools.
34        if (/^key/.test(e.type) && checkBrowserHotkey(e)) {
35            stopEventPropagation();
36            return;
37        }
38
39        // NOTE: if an element loses focus because of becoming invisible, the blur event is
40        // raised. We must not prevent this blur event. In IE, an element loses focus only
41        // if the CSS 'display' property is set to 'none', other ways of making an element
42        // invisible don't lead to blurring (in MSEdge, focus/blur are sync).
43        if (e.type === 'blur') {
44            if (browserUtils.isIE && browserUtils.version < 12) {
45                var isElementInvisible = !isWindow(target) && get(target, 'display') === 'none';
46                var elementParents     = null;
47                var invisibleParents   = false;
48
49                if (!isElementInvisible) {
50                    elementParents   = getParents(target);
51                    invisibleParents = filter(elementParents, parent => get(parent, 'display') === 'none');
52                }
53
54                if (isElementInvisible || invisibleParents.length) {
55                    // NOTE: In IE we should prevent the event and raise it on timeout. This is a fix for
56                    // the case when a focus event leads to the element disappearing. If we don't prevent
57                    // the blur event it will be raised before the previous focus event is raised (see B254768)
58                    eventSandbox.timers.deferFunction(() => {
59                        eventSimulator.blur(target);
60                    });
61                }
62            }
63            // NOTE: fix for a jQuery bug. An exception is raised when calling .is(':visible')
64            // for a window or document on page loading (when e.ownerDocument is null).
65            else if (target !== window && target !== window.document && !hasDimensions(target))
66                return;
67        }
68
69        preventDefault();
70    }
71}
72
73export default function preventRealEvents () {
74    listeners.initElementListening(window, PREVENTED_EVENTS);
75    listeners.addFirstInternalHandler(window, PREVENTED_EVENTS, preventRealEventHandler);
76}
77
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Testcafe on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)