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

Add ability to receive a message from the web page context. #11

Closed
samir-mahendra opened this issue Apr 25, 2017 · 10 comments
Closed

Add ability to receive a message from the web page context. #11

samir-mahendra opened this issue Apr 25, 2017 · 10 comments

Comments

@samir-mahendra
Copy link

There are times where we need the Javascript running in the context of a web-page to send a signal to the remote process.

PhantomJS has this feature: http://phantomjs.org/api/webpage/handler/on-callback.html

This is currently possible by having the web application call: "console.log('expected event')", and then checked for the message received using the "Runtime.consoleAPICalled" event, but that seems a bit hacky.

@dgozman
Copy link

dgozman commented Apr 25, 2017

Does Runtime.awaitPromise (or awaitPromise flag in Runtime.evaluate) help?

@dgozman
Copy link

dgozman commented Apr 25, 2017

@aslushnikov who was hitting the same

@samir-mahendra
Copy link
Author

samir-mahendra commented Apr 27, 2017

awaitPromise helps in situations where the controlling process knows to expect a signal from the web context at some later time, and it's true that this handles most of the use cases PhantomJS's onCallback was used for.

Here's a use case where it won't work for us:

One application we are testing is a non-single page app, and has a page that, after on load, runs about 30K lines of Javascript (dynamically generated by server, so # of lines varies) in the HTML body before it's "ready" to check it's DOM. We initially handled testing it by waiting for the load Event and then timing out, but then under load test conditions, with the CPU pegged, the timeout was firing too early.

This application is being tested through a re-writing proxy, so in the proxy, we added a call to "callPhantom" after the last line of Javascript, like so:

// Existing JS
window.top.callPhantom({event: "readytotest"});
</script>

In PhantomJS, we then get a onCallback that tells us in a deterministic way that the page is loaded.

We're looking for something similar for Headless Chrome without using a 'console.log' and looking for it with a 'Runtime.consoleAPICalled' callback.

@dgozman
Copy link

dgozman commented Apr 27, 2017 via email

@pavelfeldman
Copy link
Contributor

awaitPromise can be only used once, while it might be handy to be able to call something repeatedly. I would say that using console.debug is the most pragmatic approach to address it atm.

@kensoh
Copy link

kensoh commented Jul 15, 2017

Hi @samir-mahendra I'm also using headless Chrome for web automation. At the moment I try to check for some changes in DOM as an indication that the script or some activity that is supposed to be done is ready.

It is probably not an elegant way to do it this way, but it closely resembles a user's behaviour when using the browser (click something, wait for something to visually appear, then do something else). So I use that approach.

@mmuffins
Copy link

mmuffins commented Aug 4, 2018

Considering that the last message in this issue was written over a year ago - does anybody know if there were any updates related to this question?

I have a very similar use case where I want to react to a custom event, and the suggested workaround to inject some javascript that calls console.debug whenever the event is fired and listen to runtime.consoleAPICalled still seems like the best (and only) approach to do so, but it really feels more like a hack than a proper solution.

@alexkozy
Copy link

alexkozy commented Aug 4, 2018

You may consider using Runtime.addBinding. As soon as binding called you will get Runtime.bindingCalled notification with string paylod.

@mmuffins
Copy link

mmuffins commented Aug 4, 2018

I'll have a look at that, thanks a lot!

@TimvdLippe
Copy link
Contributor

This repository is related to Chrome DevTools Protocol, but does not track issues regarding its definition or implementation. If you want to file an issue for the Chrome DevTools Protocol, please open an issue on https://crbug.com under component: Platform>DevTools>Platform. Thanks in advance!

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

7 participants