Putting tab in the background

115 views
Skip to first unread message

Mor Amrani

unread,
Sep 27, 2023, 7:46:58 AM9/27/23
to devtools-dev
Hello devtools community,

I'm looking for a way to put an existing tab in the background, in order to simulate something like "tab switching" made by the user.
Is there a way to put a tab in the background using CDP? These are the available options I spotted so far, but none of them does exactly what I'm looking for:
  • Emulation.setFocusEmulationEnabled - Can only turn the tab state to 'visible', but not to the opposite hidden/invisible/background state.
  • Page.setWebLifecycleState - Unfortunately, the allowed values for it are 'active' or 'frozen', and there is no option for 'hidden', to simulate a tab that was moved to the background.
  • Target.createTarget({background: true}) - Obviously is useful only when creating new targets, and I look for something to use on existing targets.
Thanks!
Mor

Alex Rudenko

unread,
Sep 27, 2023, 7:54:59 AM9/27/23
to Mor Amrani, devtools-dev
Hi Mor,

I believe using Page.bringToFront is the closest method to emulate "tab switching" by the user. It allows bringing another tab in the foreground which brings other tabs into the background.

Best regards,
Alex

P.S. I have just sent another reply but it looks like it got lost somewhere (in case you receive duplicate emails from me).

This electronic communication and the information and any files transmitted with it, or attached to it, are confidential and are intended solely for the use of the individual or entity to whom it is addressed and may contain information that is confidential, legally privileged, protected by privacy laws, or otherwise restricted from disclosure to anyone else. If you are not the intended recipient or the person responsible for delivering the e-mail to the intended recipient, you are hereby notified that any use, copying, distributing, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited. If you received this e-mail in error, please return the e-mail to the sender, delete it from your computer, and destroy any printed copy of it.

--
You received this message because you are subscribed to the Google Groups "devtools-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to devtools-dev...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/devtools-dev/53bd96e4-c40b-43da-94bc-893adeb37327n%40chromium.org.

Mor Amrani

unread,
Sep 27, 2023, 8:41:29 AM9/27/23
to devtools-dev, alexr...@chromium.org, devtools-dev, Mor Amrani
Thanks Alex for the quick answer!

This can be very useful for me, but I tried using Page.bringToFront()while having 2 tabs running on the same context browser, and it looks like the two of them still visible. I checked with document.visibilityState which returns 'visible', and also I see that the CPU consumption wasn't reduced and looks like both tabs are still being rendered. Am I missing something?

In addition, I wonder if there's an option to simulate a situation when all the tabs are in the background? Let's say the user have moved to other application, and left the whole browser in the background.

Thanks a lot,
Mor

Alex Rudenko

unread,
Sep 27, 2023, 8:48:13 AM9/27/23
to devtools-dev, mor.a...@broadcom.com, Alex Rudenko, devtools-dev
Hi Mora,

they should not be both visible if that are in the same window because the browser UI does not allow that (but you should not use the old headless mode which has the custom implementation for visibility and pretends all tabs are visible).
You can use https://chromedevtools.github.io/devtools-protocol/tot/Browser/#method-setWindowBounds for minimizing/maximizing the browser window. 

Best regards,
Alex
Reply all
Reply to author
Forward
0 new messages