-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from hotrush/pr/5
Images waiting and timeout
- Loading branch information
Showing
9 changed files
with
210 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var page = new WebPage(); | ||
page.settings.javascriptEnabled = true; | ||
page.onResourceTimeout = function(request) { | ||
phantom.exit(); | ||
}; | ||
page.settings.resourceTimeout = 30000; | ||
|
||
page.open('https://github.com', function () { | ||
waitFor(function() { | ||
return page.evaluate(function() { | ||
for(var i = 0; i < document.images.length; i++) { | ||
if (!document.images[i].complete) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
}, function() { | ||
savePage(); | ||
phantom.exit(); | ||
}, 30000); | ||
}); | ||
|
||
function savePage() { | ||
page.viewportSize = { | ||
width: 1200, | ||
height: 800 }; | ||
page.clipRect = { | ||
width: 1200, | ||
height: 800 } | ||
page.render('tests/tmp/github.png'); | ||
} | ||
|
||
function waitFor(testFx, onReady, timeOutMillis) { | ||
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, | ||
start = new Date().getTime(), | ||
condition = false, | ||
interval = setInterval(function() { | ||
if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) { | ||
// If not time-out yet and condition not yet fulfilled | ||
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code | ||
} else { | ||
if (!condition) { | ||
// If condition still not fulfilled (timeout but condition is 'false') | ||
phantom.exit(1); | ||
} else { | ||
// Condition fulfilled (timeout and/or condition is 'true') | ||
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled | ||
clearInterval(interval); //< Stop this interval | ||
} | ||
} | ||
}, 250); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters