-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Toggle page visibility that affects document.visibilityState #2286
Comments
There is no API for that at the moment, all pages behave as if they are all visible and active. Can you describe your scenario where minimizing matters? Do you want to test |
The scenario is:- perform some action on page minimise the browser so that elements are not visible, read network logs of from browser (requests). I tried page.press(“//html ”, “Alt+Space+n”) but it is not working too. |
Does your page listen to events related to visibility changes or do you try to test how the page would behave when its tab becomes background (e.g. if the user switches to another tab in the browser or the browser window is minimized)? As of today all pages created by playwright behave as if they are active foreground tabs and the browser window is visible (not minimized). We could try and extend the API to support emulation of background tabs but we'd need to better understand what behavior you would like to emulate. |
But why? What's the reason for that? As that can be done without minimizing. |
My product deals with visibility, What an end user experiences, so to test features it is required that the framework should support browser handling. Yes I checked that all the pages of current context remains active but that is what I am not looking for. During the test browser should get minimize so that I can have some logs. If you guys can extend it will be a nice feature. As of now I am using other library to do key press so that I can minimize browser. |
Can you shed some light on how those logs are produced? Are they not a part of the page scripts? |
There are some events which are fired only if the page is visible and visibility is true for the page, so to test the functionality if it is working fine or not I have to do a minimize the browser for some time and check the events in network logs, again maximize the page and check if the events are getting fired again on page visibility=true. |
From what I hear it sounds to me that you want to emulate |
Thanks @yury-s |
For Chrome, you can use DevTools Protocol.
Found using CDP w/ |
Just wanted to chime-in with another use case. I work at BeFunky, an online photo editor, and we use quite a few async browser APIs, namely FileReader, ImageBitmap, and IndexedDB. We often find that due to browsers restricting resources & throttling tasks in background tabs, there are bugs that only occur when our tab is in the background. We wrap some of these APIs (especially less-reliable ones like FileReader) to ensure that they always give us a result or error within a reasonable timeframe. For instance, if FileReader hasn't fired an I'd like to write some automated tests in Tab A, where I occasionally switch to Tab B, then back to Tab A, etc. I understand Playwright was not built for this use case, but just wanted to throw it out there as a legitimate testing scenario. Thanks for all your hard work on this great library. |
I was doing some research and landed here as well. What I'm trying to test?Scenario: I'm using This is problematic when there is a scenario with an I'm trying to write an e2e test that cover this and I was oping to find an API that could trigger a page focus/blur in order to change the Reading other issues seems like it "should" be possible with this ugly hack: EDIT: it doesn't work await page.$eval(
'document',
(d) => Object.defineProperty(document, 'visibilityState', {get() { return "hidden";}}),
),
await page.$eval(
'document',
(d) => Object.defineProperty(document, 'visibilityState', {get() { return "visible";}}),
), I wonder if there is a better way or a plan to support a first class API in order to achieve this? |
I actually tried that doesn't seem to be working, I'm trying to do something different now but also not working await page.dispatchEvent("document", "visibilitychange") I think the reason is that I found a way to do it await page1.$eval("html", () => {
const ev = document.createEvent("Event")
ev.initEvent("visibilitychange")
document.dispatchEvent(ev)
}) But I'm wondering if there are better ways |
Why was this issue closed?We are prioritizing the features based on the upvotes, recency and activity in the issue thread. It looks like this issue only has a handful of upvotes, has not been touched recently and/or we lack sufficient feedback to act on it. We are closing issues like this one to keep our bug database maintainable. Please feel free to open a new issue and link this one to it if you think this is a mistake. |
Hi @yury-s I'm commenting on this issue, as I've ended up here while searching if there was a way in playwright to test visibility changes. Our use case is a simple listener like the one below, which we are using as a best-effort approach for refreshing our app data in case an user is editing on multiple tabs. Thank you :) document.addEventListener("visibilitychange", () => {
if (document.hidden) {
console.log("Browser tab is hidden");
} else {
setIsRefreshing(true);
loadData().then(() => {
setIsRefreshing(false);
});
}
} |
I too landed here when searching how to test this - then I realised you can probably mock it like so: async function hideTab(hide = true) {
await page.evaluate((hide) => {
Object.defineProperty(document, 'visibilityState', { value: hide ? 'hidden' : 'visible', writable: true })
Object.defineProperty(document, 'hidden', { value: hide, writable: true })
document.dispatchEvent(new Event('visibilitychange'))
}, hide)
}
await hideTab() // simulate page hidden
// run your tests
await hideTab(false) // simulate page restored
// run your tests Writing this comment for posterity. |
@zerodevx for me this didn't work, visibilitychnage is not triggered, and when I test the same code in console window |
No description provided.
The text was updated successfully, but these errors were encountered: