Skip to content

Instantly share code, notes, and snippets.

@taybenlor
Created March 27, 2023 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taybenlor/6af1ee5a8c82698b7d20261c4179c39f to your computer and use it in GitHub Desktop.
Save taybenlor/6af1ee5a8c82698b7d20261c4179c39f to your computer and use it in GitHub Desktop.
A chrome extension for Cypress that forces the browser to emulate focus.
/**
* Force Focus
*
* This extension forces Emulation.setFocusEmulationEnabled
* which allows tests that are dependent on focus to work when
* the browser is not focused or when it is in headless mode
*/
chrome.tabs.onUpdated.addListener(function (tabId, info) {
console.log("Force focus opened new tab", tabId, info);
if (info.status === "complete") {
// Attach to the tab's debugger
chrome.debugger.attach({ tabId: tabId }, "1.3", () => {
if (chrome.runtime.lastError) {
console.log(
"runtime.lastError",
tabId,
chrome.runtime.lastError.message,
);
return;
}
console.log("Debugger attached", tabId);
// Force the tab to emulate focus
chrome.debugger.sendCommand(
{
tabId: tabId,
},
"Emulation.setFocusEmulationEnabled",
{
enabled: true,
},
(result) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
}
console.log("Command sent:", JSON.stringify(result));
},
);
});
}
});
{
"manifest_version": 3,
"name": "Force focus",
"version": "0.1",
"description": "Force the DevTools Emulation.setFocusEmulationEnabled setting",
"background": {
"service_worker": "background.js"
},
"permissions": ["debugger", "tabs", "activeTab"],
"host_permissions": ["http://*/*", "https://*/*"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment