How to use hideAllUnlessAnotherWindowIsFocused method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

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

windows.js

Source: windows.js Github

copy
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.trackState = exports.open = exports.create = exports.defaults = exports._newBrowserWindow = exports.getByWebContents = exports.focusMainWindow = exports.hideAllUnlessAnotherWindowIsFocused = exports.showAll = exports.get = exports.destroy = exports.reset = exports.removeAllExtensions = exports.installExtension = void 0;
4const tslib_1 = require("tslib");
5const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
6const bluebird_1 = (0, tslib_1.__importDefault)(require("bluebird"));
7const electron_context_menu_1 = (0, tslib_1.__importDefault)(require("electron-context-menu"));
8const electron_1 = require("electron");
9const debug_1 = (0, tslib_1.__importDefault)(require("debug"));
10const cwd_1 = (0, tslib_1.__importDefault)(require("../cwd"));
11const saved_state_1 = (0, tslib_1.__importDefault)(require("../saved_state"));
12const resolve_dist_1 = require("../../../resolve-dist");
13const debug = (0, debug_1.default)('cypress:server:windows');
14let windows = {};
15let recentlyCreatedWindow = false;
16const getUrl = function (type) {
17    switch (type) {
18        case 'INDEX':
19            return (0, resolve_dist_1.getPathToDesktopIndex)();
20        default:
21            throw new Error(`No acceptable window type found for: '${type}'`);
22    }
23};
24const getByType = (type) => {
25    return windows[type];
26};
27const setWindowProxy = function (win) {
28    if (!process.env.HTTP_PROXY) {
29        return;
30    }
31    return win.webContents.session.setProxy({
32        proxyRules: process.env.HTTP_PROXY,
33        proxyBypassRules: process.env.NO_PROXY,
34    });
35};
36function installExtension(win, path) {
37    return win.webContents.session.loadExtension(path)
38        .then((data) => {
39        debug('electron extension installed %o', { data, path });
40    })
41        .catch((err) => {
42        debug('error installing electron extension %o', { err, path });
43        throw err;
44    });
45}
46exports.installExtension = installExtension;
47function removeAllExtensions(win) {
48    let extensions;
49    try {
50        extensions = win.webContents.session.getAllExtensions();
51        extensions.forEach(({ id }) => {
52            win.webContents.session.removeExtension(id);
53        });
54    }
55    catch (err) {
56        debug('error removing all extensions %o', { err, extensions });
57    }
58}
59exports.removeAllExtensions = removeAllExtensions;
60function reset() {
61    windows = {};
62}
63exports.reset = reset;
64function destroy(type) {
65    let win;
66    if (type && (win = getByType(type))) {
67        return win.destroy();
68    }
69}
70exports.destroy = destroy;
71function get(type) {
72    return getByType(type) || (() => {
73        throw new Error(`No window exists for: '${type}'`);
74    })();
75}
76exports.get = get;
77function showAll() {
78    return lodash_1.default.invoke(windows, 'showInactive');
79}
80exports.showAll = showAll;
81function hideAllUnlessAnotherWindowIsFocused() {
82    // bail if we have another focused window
83    // or we are in the middle of creating a new one
84    if (electron_1.BrowserWindow.getFocusedWindow() || recentlyCreatedWindow) {
85        return;
86    }
87    // else hide all windows
88    return lodash_1.default.invoke(windows, 'hide');
89}
90exports.hideAllUnlessAnotherWindowIsFocused = hideAllUnlessAnotherWindowIsFocused;
91function focusMainWindow() {
92    return getByType('INDEX').show();
93}
94exports.focusMainWindow = focusMainWindow;
95function getByWebContents(webContents) {
96    return electron_1.BrowserWindow.fromWebContents(webContents);
97}
98exports.getByWebContents = getByWebContents;
99function _newBrowserWindow(options) {
100    return new electron_1.BrowserWindow(options);
101}
102exports._newBrowserWindow = _newBrowserWindow;
103function defaults(options = {}) {
104    return lodash_1.default.defaultsDeep(options, {
105        x: null,
106        y: null,
107        show: true,
108        frame: true,
109        width: null,
110        height: null,
111        minWidth: null,
112        minHeight: null,
113        devTools: false,
114        trackState: false,
115        contextMenu: false,
116        recordFrameRate: null,
117        onFocus() { },
118        onBlur() { },
119        onClose() { },
120        onCrashed() { },
121        onNewWindow() { },
122        webPreferences: {
123            partition: null,
124            webSecurity: true,
125            nodeIntegration: false,
126            backgroundThrottling: false,
127        },
128    });
129}
130exports.defaults = defaults;
131function create(projectRoot, _options = {}, newBrowserWindow = _newBrowserWindow) {
132    const options = defaults(_options);
133    if (options.show === false) {
134        options.frame = false;
135    }
136    options.webPreferences.webSecurity = !!options.chromeWebSecurity;
137    if (options.partition) {
138        options.webPreferences.partition = options.partition;
139    }
140    const win = newBrowserWindow(options);
141    win.on('blur', function (...args) {
142        return options.onBlur.apply(win, args);
143    });
144    win.on('focus', function (...args) {
145        return options.onFocus.apply(win, args);
146    });
147    win.once('closed', function (...args) {
148        win.removeAllListeners();
149        return options.onClose.apply(win, args);
150    });
151    // the webview loses focus on navigation, so we
152    // have to refocus it everytime top navigates in headless mode
153    // https://github.com/cypress-io/cypress/issues/2190
154    if (options.show === false) {
155        win.webContents.on('did-start-loading', () => {
156            if (!win.isDestroyed()) {
157                return win.focusOnWebView();
158            }
159        });
160    }
161    win.webContents.on('crashed', function (...args) {
162        return options.onCrashed.apply(win, args);
163    });
164    win.webContents.on('new-window', function (...args) {
165        return options.onNewWindow.apply(win, args);
166    });
167    if (options.trackState) {
168        trackState(projectRoot, options.isTextTerminal, win, options.trackState);
169    }
170    // open dev tools if they're true
171    if (options.devTools) {
172        // and possibly detach dev tools if true
173        win.webContents.openDevTools();
174    }
175    if (options.contextMenu) {
176        // adds context menu with copy, paste, inspect element, etc
177        (0, electron_context_menu_1.default)({
178            showInspectElement: true,
179            window: win,
180        });
181    }
182    return win;
183}
184exports.create = create;
185// open desktop-gui BrowserWindow
186function open(projectRoot, options = {}, newBrowserWindow = _newBrowserWindow) {
187    // if we already have a window open based
188    // on that type then just show + focus it!
189    let win;
190    win = getByType(options.type);
191    if (win) {
192        win.show();
193        return bluebird_1.default.resolve(win);
194    }
195    recentlyCreatedWindow = true;
196    lodash_1.default.defaults(options, {
197        width: 600,
198        height: 500,
199        show: true,
200        webPreferences: {
201            contextIsolation: true,
202            preload: (0, cwd_1.default)('lib', 'ipc', 'ipc.js'),
203        },
204    });
205    if (!options.url) {
206        options.url = getUrl(options.type);
207    }
208    win = create(projectRoot, options, newBrowserWindow);
209    debug('creating electron window with options %o', options);
210    if (options.type) {
211        windows[options.type] = win;
212        win.once('closed', () => {
213            delete windows[options.type];
214        });
215    }
216    // enable our url to be a promise
217    // and wait for this to be resolved
218    return bluebird_1.default.join(options.url, setWindowProxy(win))
219        .spread((url) => {
220        // navigate the window here!
221        win.loadURL(url);
222        recentlyCreatedWindow = false;
223    }).thenReturn(win);
224}
225exports.open = open;
226function trackState(projectRoot, isTextTerminal, win, keys) {
227    const isDestroyed = () => {
228        return win.isDestroyed();
229    };
230    win.on('resize', lodash_1.default.debounce(() => {
231        if (isDestroyed()) {
232            return;
233        }
234        const [width, height] = win.getSize();
235        const [x, y] = win.getPosition();
236        const newState = {};
237        newState[keys.width] = width;
238        newState[keys.height] = height;
239        newState[keys.x] = x;
240        newState[keys.y] = y;
241        return saved_state_1.default.create(projectRoot, isTextTerminal)
242            .then((state) => {
243            return state.set(newState);
244        });
245    }, 500));
246    win.on('moved', lodash_1.default.debounce(() => {
247        if (isDestroyed()) {
248            return;
249        }
250        const [x, y] = win.getPosition();
251        const newState = {};
252        newState[keys.x] = x;
253        newState[keys.y] = y;
254        return saved_state_1.default.create(projectRoot, isTextTerminal)
255            .then((state) => {
256            return state.set(newState);
257        });
258    }, 500));
259    win.webContents.on('devtools-opened', () => {
260        const newState = {};
261        newState[keys.devTools] = true;
262        return saved_state_1.default.create(projectRoot, isTextTerminal)
263            .then((state) => {
264            return state.set(newState);
265        });
266    });
267    return win.webContents.on('devtools-closed', () => {
268        const newState = {};
269        newState[keys.devTools] = false;
270        return saved_state_1.default.create(projectRoot, isTextTerminal)
271            .then((state) => {
272            return state.set(newState);
273        });
274    });
275}
276exports.trackState = trackState;
277
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 Cypress 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)