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.

  • What is browser visibility? What would be the real use-case? Like "user is doing some interaction and then collapses the window?"
    – Alexey R.
    Commented Feb 28, 2018 at 13:30
  • "What is browser visibility?" Is the browser visible e.g. it isn't the front window. Browsers test for this, hence the W3C link.
    – ian
    Commented Feb 28, 2018 at 21:17
  • Those properties are read-only. You cannot set them explicitly. Why does your browser go invisible?
    – Alexey R.
    Commented Mar 1, 2018 at 11:41
  • @AlexeyR. When I'm running specs in my terminal then the browser is not the foreground app and this counts as not visible (I know because some of the javascript isn't running until I switch to make the browser the foreground app). That doesn't seem to follow the specification but the linked spec is a proposal, after all.
    – ian
    Commented Mar 1, 2018 at 11:51

2 Answers 2


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.


The browser being the topmost app on the OS is actually (what I would call) a meta state that is outside of Selenium's scope of dealing with apps and browser settings.

So, if you really need the browser to always be on top, I would suggest extending your test initialize with a logic like this:

  • Initiate driver: browser opens
  • Initiate a tool to automate desktop (free, such as AutoIt)
  • Focus the app as you do manually (e.g. by clicking its icon)
  • Start your test

Edit: another way would be to create a test version of your Javascript that does everything the same, except checking for browser visibility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.