Skip to content
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

Javascript: Async/await #475

Open
erikaxel opened this issue Jul 9, 2024 · 1 comment
Open

Javascript: Async/await #475

erikaxel opened this issue Jul 9, 2024 · 1 comment
Assignees

Comments

@erikaxel
Copy link

erikaxel commented Jul 9, 2024

Describe the bug
Ferrum gives error on top level await calls in javascript

To Reproduce
Running the following javascript (through Cuprite, but the issue is in Ferrum):

  page.execute_script "await new Promise(r => setTimeout(r, 100));"

will give

Ferrum::JavaScriptError: SyntaxError: await is only valid in async functions and the top level bodies of modules

Expected behavior
It runs

Additional context
The same works if run through Selenium.

I'm pretty sure the problem is that the executable script is wrapped in a function in

expression = format("function() { %s }", expression)
and that function does not use the async keyword.

I'm not sure which Chrome versions Ferrum is targeting, but async/await has been available since Chrome 62 which came out in October 2017. So if it is ok to not support javascript execution in Chrome older than 62, one solution which might work could be to just add async:

expression = format("async function() { %s }", expression)

If this is an acceptable solution I will be happy to provide a PR.

@route
Copy link
Member

route commented Jul 10, 2024

@erikaxel sounds good to me in general, though I don't like current JS API which was type of inherited from Poltergeist.
I say let's try if it works and doesn't break non-async function execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants