marked Casper.extend() as obsolete; updated samples/extends.(js|coffee) samples
Showing
4 changed files
with
78 additions
and
103 deletions
... | @@ -693,7 +693,6 @@ Casper.prototype.open = function(location, settings) { | ... | @@ -693,7 +693,6 @@ Casper.prototype.open = function(location, settings) { |
693 | } | 693 | } |
694 | } | 694 | } |
695 | this.emit('open', this.requestUrl, settings); | 695 | this.emit('open', this.requestUrl, settings); |
696 | //this.page.open(this.requestUrl, settings.method, settings.data); | ||
697 | this.page.openUrl(this.requestUrl, { | 696 | this.page.openUrl(this.requestUrl, { |
698 | operation: settings.method, | 697 | operation: settings.method, |
699 | data: settings.data | 698 | data: settings.data |
... | @@ -759,7 +758,7 @@ Casper.prototype.run = function(onComplete, time) { | ... | @@ -759,7 +758,7 @@ Casper.prototype.run = function(onComplete, time) { |
759 | } | 758 | } |
760 | this.log(f("Running suite: %d step%s", this.steps.length, this.steps.length > 1 ? "s" : ""), "info"); | 759 | this.log(f("Running suite: %d step%s", this.steps.length, this.steps.length > 1 ? "s" : ""), "info"); |
761 | this.emit('run.start'); | 760 | this.emit('run.start'); |
762 | this.checker = setInterval(this.checkStep, (time ? time: 250), this, onComplete); | 761 | this.checker = setInterval(this.checkStep, (time ? time: 100), this, onComplete); |
763 | return this; | 762 | return this; |
764 | }; | 763 | }; |
765 | 764 | ||
... | @@ -939,8 +938,8 @@ Casper.prototype.thenClick = function(selector, then, fallbackToHref) { | ... | @@ -939,8 +938,8 @@ Casper.prototype.thenClick = function(selector, then, fallbackToHref) { |
939 | if (arguments.length > 2) { | 938 | if (arguments.length > 2) { |
940 | this.emit("deprecated", "The thenClick() method does not process the fallbackToHref argument since 0.6"); | 939 | this.emit("deprecated", "The thenClick() method does not process the fallbackToHref argument since 0.6"); |
941 | } | 940 | } |
942 | this.then(function(self) { | 941 | this.then(function() { |
943 | self.click(selector); | 942 | this.click(selector); |
944 | }); | 943 | }); |
945 | return utils.isFunction(then) ? this.then(then) : this; | 944 | return utils.isFunction(then) ? this.then(then) : this; |
946 | }; | 945 | }; |
... | @@ -955,8 +954,8 @@ Casper.prototype.thenClick = function(selector, then, fallbackToHref) { | ... | @@ -955,8 +954,8 @@ Casper.prototype.thenClick = function(selector, then, fallbackToHref) { |
955 | * @see Casper#evaluate | 954 | * @see Casper#evaluate |
956 | */ | 955 | */ |
957 | Casper.prototype.thenEvaluate = function(fn, context) { | 956 | Casper.prototype.thenEvaluate = function(fn, context) { |
958 | return this.then(function(self) { | 957 | return this.then(function() { |
959 | self.evaluate(fn, context); | 958 | this.evaluate(fn, context); |
960 | }); | 959 | }); |
961 | }; | 960 | }; |
962 | 961 | ||
... | @@ -969,8 +968,8 @@ Casper.prototype.thenEvaluate = function(fn, context) { | ... | @@ -969,8 +968,8 @@ Casper.prototype.thenEvaluate = function(fn, context) { |
969 | * @see Casper#open | 968 | * @see Casper#open |
970 | */ | 969 | */ |
971 | Casper.prototype.thenOpen = function(location, then) { | 970 | Casper.prototype.thenOpen = function(location, then) { |
972 | this.then(this.createStep(function(self) { | 971 | this.then(this.createStep(function() { |
973 | self.open(location); | 972 | this.open(location); |
974 | }, { | 973 | }, { |
975 | skipLog: true | 974 | skipLog: true |
976 | })); | 975 | })); |
... | @@ -1030,15 +1029,15 @@ Casper.prototype.wait = function(timeout, then) { | ... | @@ -1030,15 +1029,15 @@ Casper.prototype.wait = function(timeout, then) { |
1030 | if (then && !utils.isFunction(then)) { | 1029 | if (then && !utils.isFunction(then)) { |
1031 | this.die("wait() a step definition must be a function"); | 1030 | this.die("wait() a step definition must be a function"); |
1032 | } | 1031 | } |
1033 | return this.then(function(self) { | 1032 | return this.then(function() { |
1034 | self.waitStart(); | 1033 | this.waitStart(); |
1035 | setTimeout(function() { | 1034 | setTimeout(function(self) { |
1036 | self.log(f("wait() finished wating for %dms.", timeout), "info"); | 1035 | self.log(f("wait() finished wating for %dms.", timeout), "info"); |
1037 | if (then) { | 1036 | if (then) { |
1038 | then.call(self, self); | 1037 | then.call(self, self); |
1039 | } | 1038 | } |
1040 | self.waitDone(); | 1039 | self.waitDone(); |
1041 | }, timeout); | 1040 | }, timeout, this); |
1042 | }); | 1041 | }); |
1043 | }; | 1042 | }; |
1044 | 1043 | ||
... | @@ -1069,8 +1068,8 @@ Casper.prototype.waitFor = function(testFx, then, onTimeout, timeout) { | ... | @@ -1069,8 +1068,8 @@ Casper.prototype.waitFor = function(testFx, then, onTimeout, timeout) { |
1069 | if (then && !utils.isFunction(then)) { | 1068 | if (then && !utils.isFunction(then)) { |
1070 | this.die("waitFor() next step definition must be a function"); | 1069 | this.die("waitFor() next step definition must be a function"); |
1071 | } | 1070 | } |
1072 | return this.then(function(self) { | 1071 | return this.then(function() { |
1073 | self.waitStart(); | 1072 | this.waitStart(); |
1074 | var start = new Date().getTime(); | 1073 | var start = new Date().getTime(); |
1075 | var condition = false; | 1074 | var condition = false; |
1076 | var interval = setInterval(function(self, testFx, timeout, onTimeout) { | 1075 | var interval = setInterval(function(self, testFx, timeout, onTimeout) { |
... | @@ -1095,7 +1094,7 @@ Casper.prototype.waitFor = function(testFx, then, onTimeout, timeout) { | ... | @@ -1095,7 +1094,7 @@ Casper.prototype.waitFor = function(testFx, then, onTimeout, timeout) { |
1095 | clearInterval(interval); | 1094 | clearInterval(interval); |
1096 | } | 1095 | } |
1097 | } | 1096 | } |
1098 | }, 100, self, testFx, timeout, onTimeout); | 1097 | }, 100, this, testFx, timeout, onTimeout); |
1099 | }); | 1098 | }); |
1100 | }; | 1099 | }; |
1101 | 1100 | ||
... | @@ -1187,8 +1186,11 @@ Casper.prototype.waitWhileVisible = function(selector, then, onTimeout, timeout) | ... | @@ -1187,8 +1186,11 @@ Casper.prototype.waitWhileVisible = function(selector, then, onTimeout, timeout) |
1187 | * Extends Casper's prototype with provided one. | 1186 | * Extends Casper's prototype with provided one. |
1188 | * | 1187 | * |
1189 | * @param Object proto Prototype methods to add to Casper | 1188 | * @param Object proto Prototype methods to add to Casper |
1189 | * @deprecated | ||
1190 | * @since 0.6 | ||
1190 | */ | 1191 | */ |
1191 | Casper.extend = function(proto) { | 1192 | Casper.extend = function(proto) { |
1193 | console.warn('Casper.extend() has been deprecated since 0.6; check the docs'); | ||
1192 | if (!utils.isObject(proto)) { | 1194 | if (!utils.isObject(proto)) { |
1193 | throw new Error("extends() only accept objects as prototypes"); | 1195 | throw new Error("extends() only accept objects as prototypes"); |
1194 | } | 1196 | } | ... | ... |
... | @@ -131,28 +131,27 @@ function format(f) { | ... | @@ -131,28 +131,27 @@ function format(f) { |
131 | exports.format = format; | 131 | exports.format = format; |
132 | 132 | ||
133 | /** | 133 | /** |
134 | * Inherit the prototype methods from one constructor into another. | 134 | * Inherit the prototype methods from one constructor into another, also |
135 | * exposes the `__super__` property to child class. | ||
135 | * | 136 | * |
136 | * The Function.prototype.inherits from lang.js rewritten as a standalone | 137 | * @param Function child Constructor function which needs to inherit the |
137 | * function (not on Function.prototype). NOTE: If this file is to be loaded | ||
138 | * during bootstrapping this function needs to be revritten using some native | ||
139 | * functions as prototype setup using normal JavaScript does not work as | ||
140 | * expected during bootstrapping (see mirror.js in r114903). | ||
141 | * | ||
142 | * @param {function} ctor Constructor function which needs to inherit the | ||
143 | * prototype. | 138 | * prototype. |
144 | * @param {function} superCtor Constructor function to inherit prototype from. | 139 | * @param Function parent Constructor function to inherit prototype from. |
140 | * @return Function The child class | ||
145 | */ | 141 | */ |
146 | function inherits(ctor, superCtor) { | 142 | function inherits(child, parent) { |
147 | ctor.super_ = superCtor; | 143 | for (var key in parent) { |
148 | ctor.prototype = Object.create(superCtor.prototype, { | 144 | if (Object.prototype.hasOwnProperty.call(parent, key)) { |
149 | constructor: { | 145 | child[key] = parent[key]; |
150 | value: ctor, | ||
151 | enumerable: false, | ||
152 | writable: true, | ||
153 | configurable: true | ||
154 | } | 146 | } |
155 | }); | 147 | } |
148 | function ctor() { | ||
149 | this.constructor = child; | ||
150 | } | ||
151 | ctor.prototype = parent.prototype; | ||
152 | child.prototype = new ctor(); | ||
153 | child.__super__ = parent.prototype; | ||
154 | return child; | ||
156 | } | 155 | } |
157 | exports.inherits = inherits; | 156 | exports.inherits = inherits; |
158 | 157 | ... | ... |
1 | Casper = require('casper').Casper | 1 | links = |
2 | 'http://edition.cnn.com/': 0 | ||
3 | 'http://www.nytimes.com/': 0 | ||
4 | 'http://www.bbc.co.uk/': 0 | ||
5 | 'http://www.guardian.co.uk/': 0 | ||
2 | 6 | ||
3 | articles = [] | 7 | class Fantomas extends require('casper').Casper |
8 | countLinks: -> | ||
9 | @evaluate -> | ||
10 | __utils__.findAll('a').length | ||
4 | 11 | ||
5 | ### | ||
6 | Adds two new methods to the Casper prototype: fetchTexts and renderJSON. | ||
7 | ### | ||
8 | Casper.extend | ||
9 | # Adds a new navigation step for casper; basically it will: | ||
10 | # | ||
11 | # 1. open an url, | ||
12 | # 2. on loaded, will fetch all contents retrieved through the provided | ||
13 | # CSS3 selector and return them in a formatted object. | ||
14 | fetchTexts: (location, selector) -> | ||
15 | @thenOpen location, -> | ||
16 | fetch = (selector) -> | ||
17 | elements = document.querySelectorAll(selector) | ||
18 | Array::map.call elements, (e) -> e.innerText | ||
19 | texts = @evaluate fetch, selector: selector | ||
20 | articles = articles.concat texts | ||
21 | |||
22 | # Echoes a JSON output of the fetched results and exits phantomjs. | ||
23 | renderJSON: (what) -> | 12 | renderJSON: (what) -> |
24 | @echo JSON.stringify what, null, ' ' | 13 | @echo JSON.stringify what, null, ' ' |
25 | @exit() | ||
26 | 14 | ||
27 | casper = new Casper | 15 | fantomas = new Fantomas |
28 | loadImages: false | 16 | loadImages: false |
29 | loadPlugins: false | ||
30 | logLevel: "debug" | 17 | logLevel: "debug" |
31 | verbose: true | 18 | verbose: true |
32 | 19 | ||
33 | casper.start() | 20 | fantomas.start() |
34 | 21 | ||
35 | # all article titles are stored in <h3> | 22 | for url of links |
36 | casper.fetchTexts 'http://www.liberation.fr/', 'h3' | 23 | do (url) -> |
24 | fantomas.thenOpen url, -> | ||
25 | links[url] = @countLinks() | ||
37 | 26 | ||
38 | # all article titles are stored in <h2 class="article"> | 27 | fantomas.run -> |
39 | casper.fetchTexts 'http://www.lemonde.fr/', 'h2.article' | 28 | @renderJSON links |
40 | 29 | @exit() | |
41 | casper.run -> @renderJSON articles | ... | ... |
1 | var Casper = require("casper").Casper; | 1 | var casper = require('casper').create({ |
2 | |||
3 | /** | ||
4 | * Adds two new methods to the Casper prototype: fetchTexts and renderJSON. | ||
5 | */ | ||
6 | Casper.extend({ | ||
7 | /** | ||
8 | * Adds a new navigation step for casper; basically it will: | ||
9 | * | ||
10 | * 1. open an url, | ||
11 | * 2. on loaded, will fetch all contents retrieved through the provided | ||
12 | * CSS3 selector and return them in a formatted object. | ||
13 | */ | ||
14 | fetchTexts: function(selector) { | ||
15 | return this.evaluate(function(selector) { | ||
16 | var elements = document.querySelectorAll(selector); | ||
17 | return Array.prototype.map.call(elements, function(e) { | ||
18 | return e.innerText; | ||
19 | }); | ||
20 | }, { selector: selector }); | ||
21 | }, | ||
22 | |||
23 | /** | ||
24 | * Echoes a JSON output of the fetched results and exits phantomjs. | ||
25 | */ | ||
26 | renderJSON: function(what) { | ||
27 | return this.echo(JSON.stringify(what, null, ' ')).exit(); | ||
28 | } | ||
29 | }); | ||
30 | |||
31 | var casper = new Casper({ | ||
32 | loadImages: false, | 2 | loadImages: false, |
33 | loadPlugins: false, | ||
34 | logLevel: "debug", | 3 | logLevel: "debug", |
35 | verbose: true | 4 | verbose: true |
36 | }); | 5 | }); |
37 | 6 | ||
38 | var articles = []; | 7 | var links = { |
8 | 'http://edition.cnn.com/': 0, | ||
9 | 'http://www.nytimes.com/': 0, | ||
10 | 'http://www.bbc.co.uk/': 0, | ||
11 | 'http://www.guardian.co.uk/': 0 | ||
12 | }; | ||
39 | 13 | ||
40 | casper.start('http://www.liberation.fr/', function() { | 14 | var fantomas = Object.create(casper); |
41 | articles = this.fetchTexts('h3'); | ||
42 | }); | ||
43 | 15 | ||
44 | casper.thenOpen('http://www.lemonde.fr/', function() { | 16 | fantomas.countLinks = function(selector) { |
45 | articles.concat(this.fetchTexts('h2.article')); | 17 | return this.evaluate(function() { |
18 | return __utils__.findAll('a[href]').length; | ||
19 | }); | ||
20 | }; | ||
21 | |||
22 | fantomas.renderJSON = function(what) { | ||
23 | return this.echo(JSON.stringify(what, null, ' ')); | ||
24 | }; | ||
25 | |||
26 | fantomas.start(); | ||
27 | |||
28 | Object.keys(links).forEach(function(url) { | ||
29 | fantomas.thenOpen(url, function() { | ||
30 | links[url] = this.countLinks(); | ||
31 | }); | ||
46 | }); | 32 | }); |
47 | 33 | ||
48 | casper.run(function(self) { | 34 | fantomas.run(function() { |
49 | self.renderJSON(articles); | 35 | this.renderJSON(links).exit(); |
50 | }); | 36 | }); | ... | ... |
-
Please register or sign in to post a comment