added new sample: bbcshots.(js|coffee)
Showing
2 changed files
with
130 additions
and
0 deletions
samples/bbcshots.coffee
0 → 100644
1 | ### Create a mosaic image from all headline photos on BBC homepage | ||
2 | ### | ||
3 | casper = require('casper').create() | ||
4 | nbLinks = 0 | ||
5 | currentLink = 1 | ||
6 | images = [] | ||
7 | |||
8 | casper.start 'http://www.bbc.co.uk/', -> | ||
9 | nbLinks = @evaluate -> | ||
10 | return __utils__.findAll('#carousel_items_items li').length | ||
11 | @echo "#{nbLinks} items founds" | ||
12 | # hide navigation arrows | ||
13 | @evaluate -> | ||
14 | document.querySelector('.nav_left').style.display = "none" | ||
15 | document.querySelector('.nav_right').style.display = "none" | ||
16 | @mouse.move '#promo_carousel' | ||
17 | @waitUntilVisible '.autoplay.nav_pause', -> | ||
18 | @echo 'Moving over pause button' | ||
19 | @mouse.move '.autoplay.nav_pause' | ||
20 | @click '.autoplay.nav_pause' | ||
21 | @echo 'Clicked on pause button' | ||
22 | @waitUntilVisible '.autoplay.nav_play', -> | ||
23 | @echo 'Carousel has been paused' | ||
24 | # hide play button | ||
25 | @evaluate -> | ||
26 | document.querySelector('.autoplay').style.display = "none" | ||
27 | |||
28 | # Building resulting page and image | ||
29 | buildPage = -> | ||
30 | this.echo 'Build result page' | ||
31 | fs = require 'fs' | ||
32 | @viewport 624, 400 | ||
33 | pageHtml = "<html bgcolor=black><body>" | ||
34 | images.forEach (image) -> | ||
35 | pageHtml += "<img src='file://#{fs.workingDirectory}/#{image}'><br>" | ||
36 | pageHtml += "</body></html>" | ||
37 | fs.write 'result.html', pageHtml, 'w' | ||
38 | @thenOpen "file://#{fs.workingDirectory}/result.html", -> | ||
39 | this.echo 'Resulting image saved to result.png' | ||
40 | this.capture 'result.png' | ||
41 | |||
42 | # Capture carrousel area | ||
43 | next = -> | ||
44 | image = "bbcshot#{currentLink}.png" | ||
45 | images.push image | ||
46 | @echo "Processing image #{currentLink}" | ||
47 | @captureSelector image, '.carousel_viewport' | ||
48 | if currentLink < nbLinks | ||
49 | @click ".carousel_itemList_li[rel='#{currentLink}']" | ||
50 | @wait 1000, -> | ||
51 | this.then next | ||
52 | currentLink++ | ||
53 | else | ||
54 | this.then buildPage | ||
55 | |||
56 | casper.then next | ||
57 | |||
58 | casper.run() |
samples/bbcshots.js
0 → 100644
1 | /** | ||
2 | * Create a mosaic image from all headline photos on BBC homepage | ||
3 | * | ||
4 | */ | ||
5 | var casper = require('casper').create(), | ||
6 | nbLinks = 0, | ||
7 | currentLink = 1, | ||
8 | images = []; | ||
9 | |||
10 | casper.start('http://www.bbc.co.uk/', function() { | ||
11 | nbLinks = this.evaluate(function() { | ||
12 | return __utils__.findAll('#carousel_items_items li').length; | ||
13 | }); | ||
14 | this.echo(nbLinks + ' items founds'); | ||
15 | // hide navigation arrows | ||
16 | this.evaluate(function() { | ||
17 | document.querySelector('.nav_left').style.display = "none"; | ||
18 | document.querySelector('.nav_right').style.display = "none"; | ||
19 | }); | ||
20 | this.mouse.move('#promo_carousel'); | ||
21 | this.waitUntilVisible('.autoplay.nav_pause', function() { | ||
22 | this.echo('Moving over pause button'); | ||
23 | this.mouse.move('.autoplay.nav_pause'); | ||
24 | this.click('.autoplay.nav_pause'); | ||
25 | this.echo('Clicked on pause button'); | ||
26 | this.waitUntilVisible('.autoplay.nav_play', function() { | ||
27 | this.echo('Carousel has been paused'); | ||
28 | // hide play button | ||
29 | this.evaluate(function() { | ||
30 | document.querySelector('.autoplay').style.display = "none"; | ||
31 | }); | ||
32 | }); | ||
33 | }); | ||
34 | }); | ||
35 | |||
36 | // Capture carrousel area | ||
37 | var next = function() { | ||
38 | var image = 'bbcshot' + currentLink + '.png'; | ||
39 | images.push(image); | ||
40 | this.echo('Processing image ' + currentLink); | ||
41 | this.captureSelector(image, '.carousel_viewport'); | ||
42 | if (currentLink < nbLinks) { | ||
43 | this.click('.carousel_itemList_li[rel="' + currentLink + '"]'); | ||
44 | this.wait(1000, function() { | ||
45 | this.then(next); | ||
46 | currentLink++; | ||
47 | }); | ||
48 | } else { | ||
49 | this.then(buildPage); | ||
50 | } | ||
51 | }; | ||
52 | |||
53 | // Building resulting page and image | ||
54 | var buildPage = function() { | ||
55 | this.echo('Build result page'); | ||
56 | var fs = require('fs'); | ||
57 | this.viewport(624, 400); | ||
58 | var pageHtml = "<html bgcolor=black><body>"; | ||
59 | images.forEach(function(image) { | ||
60 | pageHtml += '<img src="file://' + fs.workingDirectory + '/' + image + '"><br>'; | ||
61 | }); | ||
62 | pageHtml += "</body></html>"; | ||
63 | fs.write('result.html', pageHtml, 'w'); | ||
64 | this.thenOpen('file://' + fs.workingDirectory + '/result.html', function() { | ||
65 | this.echo('Resulting image saved to result.png'); | ||
66 | this.capture('result.png'); | ||
67 | }); | ||
68 | }; | ||
69 | |||
70 | casper.then(next); | ||
71 | |||
72 | casper.run(); |
-
Please register or sign in to post a comment