Q:

Force a browser's visibility setting to true

I'm trying to set up some specs against a real (not headless) browser but some of the javascript on the target page only runs if the browser is visible (it doesn't need to be the active app). I've been perusing the W3C Page Visibility API docs and [MDN's] too but I can't see a way to manipulate the setting while running Watir.
I know there's the option of running the spec on a headless machine but while I'm developing the specs it'd be handy to not have to switch back and forth between browser and terminal to avoid timeouts and failures. I've had a look for Chrome switches that might help and found this very helpful list but it doesn't appear to have the magic bullet either.
I've also tried browser.execute_script('document.hidden = false'); but that had no effect.
Any help or insight would be much appreciated.

A:

It's possible to force this status with a Javascript injection via execute_script.
To simulate a visible document:
Object.defineProperty(document, 'visibilityState', {value: 'visible', writable: true});
Object.defineProperty(document, 'hidden', {value: false, writable: true});
document.dispatchEvent(new Event("visibilitychange"));

To simulate an hidden document:
Object.defineProperty(document, 'visibilityState', {value: 'hidden', writable: true});
Object.defineProperty(document, 'hidden', {value: true, writable: true});
document.dispatchEvent(new Event("visibilitychange"));

Note that you'll have to run it again if the page is reloaded.

