Comments (6)
Ok I worked it around by using global object at the browser side and then requesting the property of a global object later as follows:
const input = await page.locator('css=input');
await input.focus();
await page.evaluate(() => {
const config = {
hasDropdownFlashed: false,
observer: null
};
const dialog = document.querySelector('complex-object-combobox').shadowRoot.querySelector('dialog');
config.observer = new MutationObserver((mutationList, observer) => {
for (const mutation of mutationList) {
if (dialog.style.display === '') {
config.hasDropdownFlashed = true;
}
}
});
config.observer.observe(dialog, { attributeFilter: ['style'] });
document.my = { config };
});
await page.keyboard.type('a');
await page.waitForTimeout(1000);
await page.keyboard.type('r');
await page.waitForTimeout(1000);
const hasDropdownFlashed = await page.evaluate(() => {
console.log('hasDropdownFlashed: ', document.my.config.hasDropdownFlashed); // that value that will be passed to Node
console.log('document.my.config.observer: ', document.my.config.observer); // actual observer in the browser
document.my.config.observer.disconnect();
return document.my.config.hasDropdownFlashed;
});
console.log('hasDropdownFlashed: ', hasDropdownFlashed); // `true` in Node
// expect config.hasDropdownFlashed to be false
from playwright.
Hi! You ran into the behaviour that's documented here: https://playwright.dev/docs/api/class-page#page-evaluate
If the function passed to the page.evaluate() returns a non-Serializable value, then page.evaluate() resolves to undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.
Instead of storing your config
on document.my
, you can keep a handle to it:
// ...
const config = await page.evaluateHandle(() => {
// ...
return config;
});
// ...
const hasDropdownFlashed = await page.evaluate(config => {
config.observer.disconnect();
return config.hasDropdownFlashed;
}, config);
// ...
Let me know if that helps!
from playwright.
Hi @Skn0tt , thank you for quick response! I have just tried your suggestion and I got the error Error: page.evaluate: TypeError: config.observer.disconnect is not a function
from playwright.
Hmm, unsure why that happens. I don't have access to your full code so I can't make sure that my snippet works. This example works for me:
import { test } from "@playwright/test";
test("repro", async ({ page }) => {
await page.goto("https://example.com");
const config = await page.evaluateHandle(() => {
const observer = new MutationObserver(() => {});
const config = { observer }
return config;
})
await page.evaluate(config => {
config.observer.disconnect();
}, config);
})
If you're getting config.observer.disconnect is not a function
, then that's probably related to your specific implementation.
from playwright.
Yes you are right! I double checked the code and I noticed that first time I did not use evaluateHandle
and used evaluate instead. Thank you! The solution works.
from playwright.
That's wonderful to hear! Closing then 😁
from playwright.
Related Issues (20)
- [Bug]: Unable to turn off GeoLocation or block a given website from accessing the GeoLocation feature. HOT 2
- [Feature]: Use system wide webview HOT 1
- [Bug]: `LaunchAsync` finishes with `TimeoutException` on Chrome HOT 1
- [Bug]: Playwright locator from generateLocator refers to the wrong html element HOT 1
- [internal]: remove screenshot-instead-of-snapshot usages
- Executing the Playwright automation suite before the Pull Request get merged into main branch HOT 2
- [Feature]: Add `page.clock` method to check if `page.clock.install()` was called HOT 2
- [Bug]: The current line of code in the Source tab is not getting updated when hovering different actions
- [Docs]: "Open in VS Code" button has moved
- [Feature]: specify junit test properties by env variable
- [internal] Rework Merge Report Status comments
- [internal] line highlights in docs are broken HOT 1
- [Bug]: Sharding not working as expected HOT 5
- [Feature]: Dynamic tags in runtime HOT 9
- [Bug]: Error: EPERM HOT 3
- [Bug]: The type method needs to be re-instated HOT 1
- [Bug]: MSedge browser not installing, dependent on repo that has no current release file HOT 1
- [Regression]: `route.continue` now maintains headers through redirects which does not match documentation HOT 1
- [Questions] does Playwright support Tray from Electron (without BrowserWindow)? HOT 1
- [Feature]: Allow to use page after a test failed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from playwright.