Commit 42633d67 42633d67d2124f4c7c8e7acb0dc9e3053f130021 by Nicolas Perriault

updated all code samples to use new require()

1 parent 31154e2b
......@@ -3,13 +3,7 @@
* log every received resource.
*
*/
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var casper = new phantom.Casper({
var casper = require('casper').create({
/**
* Every time a resource is received, a new log entry is added to the stack
* at the 'verbose' level.
......
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var logo;
new phantom.Casper({
var casper = require('casper').create({
verbose: true
}).start('http://www.google.fr/', function(self) {
});
casper.start('http://www.google.fr/', function(self) {
// download the google logo image as base64
logo = self.base64encode('http://www.google.fr/images/srpr/logo3w.png');
}).run(function(self) {
});
casper.run(function(self) {
self.echo(logo).exit();
});
......
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var links = [
'http://google.com/',
'http://yahoo.com/',
'http://bing.com/'
];
var casper = new phantom.Casper();
casper.start();
var casper = require('casper').create();
casper.each(links, function(self, link) {
casper.start().each(links, function(self, link) {
self.thenOpen(link, function(self) {
self.echo(self.getTitle());
});
......
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var articles = [];
var CasperClass = require('casper').Casper;
/**
* Adds two new methods to the Casper prototype: fetchTexts and renderJSON.
*/
phantom.Casper.extend({
CasperClass.extend({
/**
* Adds a new navigation step for casper; basically it will:
*
......@@ -18,14 +14,12 @@ phantom.Casper.extend({
*/
fetchTexts: function(location, selector) {
return this.thenOpen(location, function(self) {
var texts = self.evaluate(function() {
var elements = document.querySelectorAll('%selector%');
var texts = self.evaluate(function(selector) {
var elements = document.querySelectorAll(selector);
return Array.prototype.map.call(elements, function(e) {
return e.innerText;
});
}, {
selector: selector.replace("'", "\'")
});
}, { selector: selector });
articles = articles.concat(texts);
});
},
......@@ -38,17 +32,21 @@ phantom.Casper.extend({
}
});
var casper = new phantom.Casper({
var casper = new CasperClass({
loadImages: false,
loadPlugins: false,
logLevel: "debug",
verbose: true
});
casper.start()
.fetchTexts('http://www.liberation.fr/', 'h3') // all article titles are stored in <h3>
.fetchTexts('http://www.lemonde.fr/', 'h2.article') // all article titles are stored in <h2 class="article">
.run(function(self) {
self.renderJSON(articles);
})
;
casper.start();
// all article titles are stored in <h3>
casper.fetchTexts('http://www.liberation.fr/', 'h3');
// all article titles are stored in <h2 class="article">
casper.fetchTexts('http://www.lemonde.fr/', 'h2.article');
casper.run(function(self) {
self.renderJSON(articles);
});
......
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var links = [];
var casper = new phantom.Casper();
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('h3.r a');
......
......@@ -9,13 +9,9 @@
* borris: 2370000
* winner is "nicolas" with 69600000 results
*/
var CasperClass = require('casper').Casper;
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
phantom.Casper.extend({
CasperClass.extend({
fetchScore: function() {
return this.evaluate(function() {
var result = document.querySelector('#resultStats').innerText;
......@@ -24,7 +20,7 @@ phantom.Casper.extend({
}
});
var casper = new phantom.Casper({
var casper = new CasperClass({
verbose: true
}), terms = casper.cli.args, scores = [], i = 0;
......@@ -57,4 +53,3 @@ casper.run(function(self) {
self.echo('winner is "' + winner.term + '" with ' + winner.score + ' results');
self.exit();
});
......
# get multiple pages of google search results
#
# usage: phantomjs googlepagination.coffee my search terms
#
# (all arguments will be used as the query)
#
if not phantom.casperLoaded
console.log "This script is intended to work with CasperJS, using its executable."
phantom.exit 1
links = []
casper = new phantom.Casper
""" Capture multiple pages of google search results
usage: casperjs googlepagination.coffee my search terms
(all arguments will be used as the query)
"""
casper = require('casper').create()
if casper.cli.args.length == 0
casper.echo "usage: $ casperjs my search terms"
casper.exit()
casper.start 'http://google.com', ->
@fill 'form[name=f]', q: casper.cli.args.join(' '), true
......@@ -28,15 +27,15 @@ casper.then ->
cspr.evaluate ->
if nextLink = document.querySelector('table#nav td.cur').nextElementSibling?.querySelector('a')
nextLink.setAttribute 'id', 'next-page-of-results'
nextLink.setAttribute "id", "next-page-of-results"
nextPage = 'a#next-page-of-results'
nextPage = "a#next-page-of-results"
if cspr.exists nextPage
cspr.echo 'requesting next page...'
cspr.echo "requesting next page..."
cspr.thenClick(nextPage).then(processPage)
else
cspr.echo "that's all, folks."
processPage(casper)
casper.run -> @exit()
casper.run()
......
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var casper = new phantom.Casper({
var casper = require('casper').create({
logLevel: "debug"
});
......@@ -25,4 +20,4 @@ casper.then(function(self) {
casper.run(function(self) {
self.test.renderResults(true);
});
\ No newline at end of file
});
......
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var casper = new phantom.Casper({
var casper = require('casper').create({
verbose: true,
logLevel: 'debug'
});
......
var casper = new phantom.Casper({
var casper = require('casper').create({
verbose: true
});
......
/**
* This script will capture a screenshot of a twitter account page
*
*/
var casper = require('casper').create({
logLevel: "debug",
verbose: true,
......@@ -13,6 +17,4 @@ casper.start('https://twitter.com/#!/twilio', function(self) {
}, null, 12000);
});
casper.run(function(self) {
self.exit();
});
casper.run();
......
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var links = [];
var casper = new phantom.Casper({
/**
* This script will add a custom HTTP status code handler, here for 404 pages.
*
*/
var casper = require('casper').create({
httpStatusHandlers: {
404: function(self, resource) {
self.echo('Resource at ' + resource.url + ' not found (404)');
self.echo('Resource at ' + resource.url + ' not found (404)', 'COMMENT');
}
},
verbose: true
......@@ -16,4 +14,5 @@ var casper = new phantom.Casper({
casper.start('http://www.google.com/plop', function(self) {
self.echo('Done.').exit();
});
casper.run();
\ No newline at end of file
casper.run();
......
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
var failed = [];
if (phantom.casperArgs.args.length === 0) {
console.log('You must provide a timeout value');
phantom.exit(1);
}
var timeout = Number(phantom.casperArgs.args[0], 10);
if (timeout < 1) {
console.log('A timeout value must be a positive integer');
phantom.exit(1);
}
var casper = new phantom.Casper({
stepTimeout: timeout,
var casper = require('casper').create({
onStepTimeout: function(self) {
self.echo(self.requestUrl + ' failed to load in less than ' + timeout + 'ms', 'ERROR');
failed.push(self.requestUrl);
}
});
var links = [
'http://google.com/',
'http://akei.com/',
'http://lemonde.fr/',
'http://liberation.fr/',
'http://cdiscount.fr/'
];
casper.echo('Testing with timeout=' + timeout + 'ms.');
var timeout = ~~casper.cli.get(0);
casper.options.stepTimeout = timeout > 0 ? timeout : 1000;
casper.echo('Testing with timeout=' + casper.options.stepTimeout + 'ms.');
casper.start();
casper.each(links, function(self, link, i) {
casper.each(links, function(self, link) {
self.test.comment('Adding ' + link + ' to test suite');
self.thenOpen(link, function(self) {
self.echo(self.requestUrl + ' loaded');
var testStatus = self.test.pass;
if (failed.indexOf(self.requestUrl) > -1) {
self.test.fail(self.requestUrl);
} else {
self.test.pass(self.requestUrl);
}
});
});
casper.run(function(self) {
self.test.renderResults(true);
self.exit();
});
\ No newline at end of file
});
......
/**
* Just a silly game.
*
* $ phantomjs samples/timeout.js 500
* $ casperjs samples/timeout.js 500
* Will google.com load in less than 500ms?
* NOPE.
* $ phantomjs samples/timeout.js 1000
* $ casperjs samples/timeout.js 1000
* Will google.com load in less than 1000ms?
* NOPE.
* $ phantomjs samples/timeout.js 1500
* $ casperjs samples/timeout.js 1500
* Will google.com load in less than 1500ms?
* NOPE.
* $ phantomjs samples/timeout.js 2000
* $ casperjs samples/timeout.js 2000
* Will google.com load in less than 2000ms?
* YES!
*/
if (!phantom.casperLoaded) {
console.log('This script is intended to work with CasperJS, using its executable.');
phantom.exit(1);
}
if (phantom.casperArgs.args.length === 0) {
console.log('You must provide a timeout value');
phantom.exit(1);
} else {
var timeout = Number(phantom.casperArgs.args[0], 10);
if (timeout < 1) {
console.log('A timeout value must be a positive integer');
phantom.exit(1);
}
}
var casper = new phantom.Casper({
timeout: timeout,
var casper = require('casper').create({
onTimeout: function(self) {
self.echo('NOPE.', 'RED_BAR').exit();
}
});
var timeout = ~~casper.cli.get(0);
if (timeout < 1) {
casper.echo('You must pass a valid timeout value').exit();
}
casper.echo('Will google.com load in less than ' + timeout + 'ms?');
casper.options.timeout = timeout;
casper.start('http://google.com/', function(self) {
casper.start('http://www.google.com/', function(self) {
self.echo('YES!', 'GREEN_BAR').exit();
});
......