Allow a global onReadyScript to be run throughout all scenarios
First, thanks for this project :)
Second, the juicy part:
We currently have a per-scenario onReadyScript. A global onReadyScript would be useful for QA-use cases like generalization of dynamic content.
My specific case: Replacing dynamic product images, texts and other data with generic placeholders. The aim is to be able to compare two versions of a site even if they are running on different datasets. These images and other elements can exist on every page. Obviously, I want to avoid duplicating code in the scenario-specific onReadyScript. Sharing a single onReadyScript throughout all scenarios is also not really desirable duer to lost flexibility.
Will provide a PR for this issue in a moment. What do you think of the idea in general?
Since i was at it, i also added an onBeforeStartScript. This can be used for registering event listeners for e.g. cancelling requests or other things that have to be setup before casper starts to load the first page.
Hi @laubsauger -- thanks for posting!
It would help me to understand why using hideSelectors doesn't work for you in this case.
Also, There is already a global onReadyScript script option. see ==> https://github.com/garris/BackstopJS/pull/133
Hi @garris, thanks for the hint - didn't see that. Unfortunately, the solution from #133 only allows the scenario-based onReady or the global onReady to run. I need both in my use case. The global one is used for generic scripting (e.g. replace all dynamic images, texts, etc.) while the local one caters to page specific things.
As for the onBeforeStartScript: At the moment im specifically using this to cancel requests to ads, trackers and the like. Hiding via selectors won't work here. This is done to speed up things and to make tests more stable. But i'm sure there are other possible use cases.
You can see an example of this here.
I'm definitely open to discussing theses approaches. Maybe there already is a better way to do this, but i can't see it.
Hi @laubsauger -- sorry for the delay getting back, I have been on on-call duty at work which has made it hard to respond to people. Anycase, in your first point, I am pretty sure you can require() a global file in your scenario-level onReadyScript() files. The current behavior is more of a override/fallback orientation, so assuming that require() will work for you, I don't think we have a strong case for making changes there.
I do think there is a good case for your onBeforeStartScript -- with these remarks:
- it should be implemented similar to
onReadyScript()running the scenario-level script (if available) and falling back to running any available root level onReadyScript if no scenario-level script is found. - it probably should be able to override any aspect of the environment prior to loading the root resource -- putting it before this line should enable that... https://github.com/garris/BackstopJS/blob/master/capture/genBitmaps.js#L80
- a name like
onBeforeEachScript()might be more appropriate given this location in the flow
Also, I am still curious...
hideSelectorsdoesn't work for your use case because of the performance issue or because of some other on-screen side-effects?- what is the actual performance gain (in milliseconds) you get by canceling your requests -- IOW: how much sooner can you generate screenshots compared to non-canceled approach?
Please let me know your thoughts -- it always helps me to understand the motivation of creative users such as yourself.
Thanks!
Hi there,
@laubsauger please clear your PR from code adjustments - it is very hard to read what exactly was changed.
I like idea of both global scripts (there is always something what is needed in global scope). I think there also would be useful possibility to call beforeScript for single test (somewhere inside casper.each(scenarios but before this.thenOpen)
FWIW: the above comment does not represent my attitude at all. 👷
Hi y'all,
@garris You're right require()'ing common scripts is the sane thing to do here. Infact i'm doing it already for stuff like login scripts. Just didn't think about using it for this aswell. Case closed :)
Regarding my onBeforeScriptuse case: I'm working on a site that's drenched in retargeting/marketing/analytics tags. Some of these will screw with caspers detection of the finished page load. This leads to timeouts or delays depending on the type of script and their response times. So instead of bothering with any third-party scripts (which shouldn't affect the on-screen layout anyway fingerscrossed) i'm just killing them as soon as the outgoing request is detected by casper.
Using hideSelectors won't help here since i can't really target these things with css-selectors.
Please ask for further details if it's still not clear.
As for the performance gain: It's timeout/failed test vs. we're golden. No need to measure in milliseconds. It should boost performance anyway, though.
@mayacode I'll revisit my changes and provide a clean PR also incorporating @garris' suggestions asap.
That's what I was guessing but I wanted to make sure. I can see a benefit to shunting all that ad code so you can work faster. I can also a benefit to testing with the ad code to make sure it doesn't break what you just worked on. So, glad this change would enable both cases. thanks!
Thanks! Merged to master. Could use a little documentation. I will update the readme as soon as I can. Feel free to take a stab at it if you're inclined. 🚢 Cheers
@garris Bit of documentation: https://github.com/garris/BackstopJS/pull/156