fascinating-indigo•3y ago
Can I use modules inside the `evaluate`?
All the title says, specifically, can I use other modules like enum like object in
evaluateAll
function?
For example:
11 Replies
fascinating-indigoOP•3y ago
Oh, maybe should I ask this in playwright community
optimistic-gold•3y ago
I don't quite understand your question.
But below is an example that works:
I see an example in Playwright docs with passing out-scoped value as another parameter https://playwright.dev/docs/api/class-locator#locator-evaluate-all
at this example it is
min = 10
optimistic-gold•3y ago
And you can't import modules inside the page, which does not know anything about where to import that file.
See here for more information: https://playwright.dev/docs/evaluating
Evaluating JavaScript | Playwright
Playwright scripts run in your Playwright environment. Your page scripts run in the browser page environment. Those environments don't intersect, they are running in different virtual machines in different processes and even potentially on different computers.
optimistic-gold•3y ago
But Playwright's Context provides a way to inject new function in the browser instance:
browserContext.exposeFunction(name, callback)
https://playwright.dev/docs/api/class-browsercontext#browser-context-expose-functionBrowserContext | Playwright
* extends: [EventEmitter]
fascinating-indigoOP•3y ago
Maybe this would be help, thanks, gotta give a try.
I need to define
Foo
enum outside the module.
I know about it, well so I wrote the comment in the text. Thank you tho
Thank you, this could be a solution.@rikusen0335 just advanced to level 2! Thanks for your contributions! 🎉
@rikusen0335 Everything going inside evaluate is going through JSON.stringify/parse so you can only pass such values that survive this serialization
fascinating-indigoOP•3y ago
Oh, okay I got it. So the enum maybe cannot be used
I believe Enums should be fine https://www.typescriptlang.org/play?#code/KYOwrgtgBAsgngUXNA3gKCpqA1AggGQFUEBGKAXigCI8jSqAaDLW4gJgutYTarQF80aAMYB7EAGdRAG2AA6aaIDmACgBSAZQDyAOTkSALgCcAliCUmAZnBXwkkAJQOgA since it is possible to serialize them.
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
fascinating-indigoOP•3y ago
Oh thank you for confirming it! I really appreciate it:blobdance: