cloudphantomjs

TDD on Client Side: PhantomJS & CasperJS

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:
  1. 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)”
  2. Verify instlation:
    $ brew -v

  1. 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


  2. verify installation:

  1. 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
  2. Checkout the latest version of Casperjs:
    $ cd casperjs/ && git checkout tags/0.6.7
  3. Create a link to the CasperJS source file:
    $ -sf ‘pwd’/bin/casperjs /usr/local/bin/casperjs
  4. verify instalation:
  5. general verification:


  6. Running the test:





    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);
    });

    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’);
    });

    casper.run(function() {
    this.test.renderResults(true);
    });







 

One Comment

  • Ruth commented on July 7, 2014 Reply

    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

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *