Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
#47 Fixing page.onInitialized and adding unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdesalas committed Mar 9, 2015
1 parent 0692dce commit b8120a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
6 changes: 2 additions & 4 deletions API/Modules/WebPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,6 @@ public void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArg
{
if (browser != null)
{
// Set current frame & handle initialization
switchToMainFrame();
this._fireEvent("initialized");
// DocumentCompleted is fired before window.onload and body.onload
// @see http://stackoverflow.com/questions/18368778/getting-html-body-content-in-winforms-webbrowser-after-body-onload-event-execute/18370524#18370524
browser.Document.Window.AttachEventHandler("onload", delegate
Expand All @@ -527,9 +524,10 @@ public void Navigated(object sender, WebBrowserNavigatedEventArgs args)
{
if (browser != null)
{
// Set current frame & add tools
// Initialize
switchToMainFrame();
AddToolset();
_fireEvent("initialized");
// Track unhandled errors
browser.Document.Window.Error += delegate(object obj, HtmlElementErrorEventArgs e)
{
Expand Down
18 changes: 9 additions & 9 deletions test/unit/spec/webpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,22 +638,23 @@ assert.suite('Module: WebPage', function() {
success: true,
url: request.url
});
response.write('<html><head><title>Test</title><script>var myGlobal = "glob928824"; var toolsLoaded = typeof window.callPhantom;</script></head><body>' + bodyText + '</body></html>');
response.write('<html><head><title>eventtest9837423401</title><script>var myLateGlobal = "glob928824";nonexistentVariable.shouldFail();</script></head><body>' + bodyText + '</body></html>');
response.close();
});


// --------------------------------------------
assert.section('Events: Initialize, CallPhantom', function() {
assert.section('Events: Initialize, CallPhantom, Error', function() {

var initData1, initData2;
var initData1, initData2, initData3;
var pageData = null, pageData2 = null, pageData3 = null, pageData4 = null;
var initTime, loadTime, callbackTime;
var date = new Date();

page.onInitialized = function() {
initData1 = page.evaluate(function() {return myGlobal;});
initData2 = page.evaluate(function() {return toolsLoaded;});
initData1 = page.evaluate(function() {return document.title;});
initData2 = page.evaluate(function() {return typeof window.callPhantom;});
initData3 = page.evaluate(function() {return myLateGlobal;});
initTime = new Date();
trifle.wait(1);
}
Expand All @@ -665,10 +666,9 @@ assert.suite('Module: WebPage', function() {

assert.waitUntilReady();

console.log(initData1, initData2, initTime, loadTime, typeof loadTime);

assert(initData1 === 'glob928824', 'page.onInitialized has access to global vars');
assert(initData2 === 'function', 'page.onInitialized confirms that ie tools are loaded');
assert(initData1 === 'eventtest9837423401', 'page.onInitialized has access to DOM in global context');
assert(initData2 === 'function', 'page.onInitialized is fired after ie tools are loaded');
assert(initData3 === null, 'page.onInitialized fires before scripts on the page are executed');
assert(initTime < loadTime, 'page.onInitialized fires before the page finishes loading');

page.onCallback = function(data, data2, data3, data4, data5) {
Expand Down

0 comments on commit b8120a7

Please sign in to comment.