Code Monkey home page Code Monkey logo

Comments (6)

okadurin avatar okadurin commented on March 22, 2025

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.

Skn0tt avatar Skn0tt commented on March 22, 2025

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.

okadurin avatar okadurin commented on March 22, 2025

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.

Skn0tt avatar Skn0tt commented on March 22, 2025

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.

okadurin avatar okadurin commented on March 22, 2025

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.

Skn0tt avatar Skn0tt commented on March 22, 2025

That's wonderful to hear! Closing then 😁

from playwright.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.