Starting working on PhantomJS & CasperJS
PhantomJS: is a headless WebKit with JavaScript API. CasperJS: is a navigation scripting & testing utility that runs on top/alongside of PhantomJS.
The Api of Phantom JS is based on JavaScript: how would you execute code in a browser? Web sites JavaScript code is sandboxed, so that it does not interact with the main tool. Nothing is rendered to the screen like Selenium, we use the command-line.
Phantom JS is a browser itself, supporting DOM manipulation, Ajax calls, Canvas elements and more. It’s able to load entire html pages for end-to-end testing and only Selenium compares with it in functionality. It is very powerful.
We are going to use:
- Mac OSX
- Terminal app
- Ruby (must be already install on your MAC)
- Git
- Homebrew
- phantomJS
- casterJS
get homebrew:
- get it from https://github.com/mxcl/homebrew or using the terminal
$ /usr/bin/ruby -e “$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)” - Verify instlation:
$ brew -v
1 |
get phantomjs: |
- download phantomJS: http://code.google.com/p/phantomjs/downloads/list extract the content. The executable bin/phantomjs is ready to use, or use the terminal
1<span style="color: #339966;">$ brew install phantomjs</span>
1or
1<span style="color: #339966;">$ sudo port install phantomjs</span> - verify installation:
1<span style="color: #339966;">$ phantomjs -v</span>
1 |
get casperJS: |
- Assuming we have GIT move where you want to clone (if not http://git-scm.com/download/mac):
$ git clone git://github.com/n1k0/casperjs.git - Checkout the latest version of Casperjs:
$ cd casperjs/ && git checkout tags/0.6.7 - Create a link to the CasperJS source file:
$ -sf ‘pwd’/bin/casperjs /usr/local/bin/casperjs - verify instalation:
1<span style="color: #339966;">$ casperjs --version</span> - general verification:
1<span style="color: #339966;">$ which brew && which casperjs && which phantomjs</span>
1this must diplay something like:
1<span style="color: #339966;">$ /usr/local/bin/brew $ /usr/local/bin/casperjs $ /usr/local/bin/phantomjs</span> - Running the test:
1<span style="color: #339966;">$ cd ~/Desktop $ mkdir unit_tests $ cd unit_tests $ touch googletest.js</span>
1Finally, using nano or vi open the googletest.js and COPY + PASTE this:
1<span style="color: #339966;">var casper = require('casper').create();</span>
1<span style="color: #339966;">casper.start('http://www.google.com/', function() {</span>
this.test.assertTitle(‘Google’, ‘google homepage title is the one expected’);
this.test.assertExists(‘form[action=”/search”]’, ‘main form is found’);
this.fill(‘form[action=”/search”]’, {
q: ‘foo’
}, true);
});
1
casper.then(function() {
this.test.assertTitle(‘foo – Recherche Google’, ‘google title is ok’);
this.test.assertUrlMatch(/q=foo/, ‘search term has been submitted’);
this.test.assertEval(function() {
return __utils__.findAll(‘h3.r’).length >= 10;
}, ‘google search for “foo” retrieves 10 or more results’);
});
1
casper.run(function() {
this.test.renderResults(true);
});
1
1<span style="font-family: monospace;">Finnaly run it:</span>
1
1<span style="color: #339966;"><code>$ casperjs samples/googletest.js</code></span>
1
1<span style="font-family: monospace;">If TEST PASS you will end up with something like this:</span>
1
1<a href="http://leolanese.com/blog/wp-content/uploads/2012/06/test1.png"><img class="alignnone size-full wp-image-62" title="test" src="http://leolanese.com/blog/wp-content/uploads/2012/06/test1.png" alt="" width="640" height="127" /></a>
1 |
1 |
1 |
One Comment
It’s hard to find your articles in google. I found it on 18 spot, you
should build quality backlinks , it will help you to get more visitors.
I know how to help you, just type in google – k2 seo tips
and tricks