Comments (7)
I think I've got something working for this using the Navigated
event. I'm not sure if it's spec equivalent to PhantomJS because it won't fire on reload, but I'll try to do some testing and submit a PR.
from triflejs.
There's actually another bug related to this. The toolset added to the window (json2 and ie tools) aren't actually available until the page onload
event is fired. In PhantomJS they are available immediately because they are inserted when the page is created.
This can be fixed very easily by moving the AddToolset()
call from DocumentCompleted
to Navigated
. The unhandled error tracking should also be moved.
from triflejs.
Hi @tejacques, like your fix. Will get to it ASAP.
from triflejs.
HI @tejacques,
Added a fix for this bug. Had to call page.onInitialized
during the first part of DocumentCompleted
event (instead of Navigated, but a bit earlier than before as I'm not waiting for all other js/css/img resources via window.onload
). Reason for this is that I couldn't get access to global variables using page.evaluate
during Navigated
.
Thanks for your help and for the detailed test example.
My unit tests are running ok now. Let me know how you go.
from triflejs.
Btw. I noticed that if I move 'onInitialized' to the 'Navigated' browser event I can write to global context on the webpage (but not read as the DOM is not available yet).. might consider moving functionality there if PhantomJS behaves the same way.
from triflejs.
In phantomJS onInitialized
is called before the DOM is ready or any JS executes on the page.
Here's an example to test with:
TestPage.html
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<script>
callPhantom({ hello: 'from script tag' });
window.onload = function () {
callPhantom({ hello: 'from onload' });
}
</script>
</body>
</html>
TestScript.js
var webPage = require('webpage');
var page = webPage.create();
page.onInitialized = function () {
console.log("Initialized");
}
page.onCallback = function(data) {
console.log('CALLBACK: ' + JSON.stringify(data));
// Prints 'CALLBACK: { "hello": "world" }'
};
page.onConsoleMessage = function (msg) {
console.log(msg);
};
page.evaluate(function () {
callPhantom({ hello: 'world' });
});
page.open('http://localhost:port/path/to/TestPage.html', function (status) {
phantom.exit();
});
PhantomJS Output:
>phantomjs path/to/TestScript.js
Initialized
CALLBACK: {"hello":"world"}
Initialized
CALLBACK: {"hello":"from script tag"}
CALLBACK: {"hello":"from onload"}
TrifleJS Output:
>trifleJS.exe path/to/TestScript.js
CALLBACK: {"hello":"world"}
CALLBACK: {"hello":"from script tag"}
Initialized
CALLBACK: {"hello":"from onload"}
Currently, TrifleJS isn't calling onInitialized at all unless a page is loaded, and onInitialized is called after the page runs, but before onload.
from triflejs.
Hiya,
Thanks for the great test example, I've used it to determine the load order for main lifecycle events (including onLoadStarted and onLoadFinished) and got the following from PhantomJS:
onInitialized
onCallback: beforeopen
onLoadStarted
onInitialized
onCallback: script
onCallback: onload
onLoadFinished
page.open
I've replicated this order (except for the first event) in my unit tests so I dont break it later.
The event missing is the onInitialized
event called at the very beginning (during page.evaluate()
and before page.open()
). My feeling with this one is that I'd rather leave it for now as I'll break a few things if I try to postpone initialization (before opening a remote page) in the same way.
from triflejs.
Related Issues (20)
- window.setTimeout never triggers after 3s HOT 1
- Avoid Window popup HOT 2
- Window SetTimeout / SetInterval don't work when building from source HOT 2
- TriffleJS --render fails on http://livedoor.com/ HOT 11
- window.callPhantom and unhandled error logging are not available until onload event fires. HOT 6
- window.callPhantom doesn't trigger page.onCallback after page.load HOT 8
- page.onCallback() does not handle dates HOT 1
- Can trifleJS use Chakra instead of V8? HOT 3
- clipRect can not work well HOT 2
- Question: Any plans to make this work outside of windows? HOT 1
- System args not populating
- Render pages supplied as text
- TrifleJs in Windows Server
- Crash: Vector smash protection is enabled. HOT 1
- System.IO.FileNotFoundException HOT 1
- webpage method
- uriString can not be null exception when emulating
- trifleJS shut down & builds error :Please install Windows SDK 2010. HOT 1
- About command mode high concurrency HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from triflejs.