Web Developers’ Guide to Web Intents in Chrome

Last year Google proposed the Web Intents API to help web applications integrate with one another with minimal effort. They have now enabled an experimental version of the API in the most recent stable version of Chrome.

For more information on Web Intents and to see just how powerful they can be, check out the following links:

Words from Connect with Web Intents.

View & Comment

Front-end Unit Testing with JavaScript

Unit testing can make your code less prone to bugs and gives you piece of mind that everything is working the way it should. Normally when I speak to other developers about work, we end up speaking about workflow, tools and release processes. I’m always surprised by the amount of front-end developers that don’t unit test their code. When asked why, they give the following answers:

“I did have a look at Selenium, but it freaked me out. Getting it installed and ready to go looked a right pig!”

“The back-end guys usually just do that”

“I don’t really get time to do any of that kind of stuff but I’d like to”

“Unit Testing? Say whaaaaattt?”

So rounding up the problems that people are facing, we get the following:

  • Time and effort it takes to get setup
  • Complex APIs and steep learning curves
  • General lack of exposure

Introducing PhantomJS & CasperJS

PhantomJS is a headless WebKit with JavaScript API. CasperJS is a navigation scripting & testing utility that runs on top/alongside of PhantomJS.

If you’re thinking “headless WebKit??!!?”, then it might be easier for you picture PhantomJS as a web inspector console that can be injected into any web page that you want, then accessed and used.

CasperJS makes it alot easier for you to navigate around a web page testing as you go. To be fair, my explanations don’t do these two tools enough justice. Please visit http://phantomjs.org/ and http://casperjs.org/ for a better explanation and more advanced features that aren’t covered here.

Both are really easy to use and extremely powerful!

Getting Started!

Requirements:

  • Mac OSX
  • Terminal app
  • Ruby
  • Git
  • Homebrew

First things first, your Mac probably already has Ruby installed (You can check by running “ruby -v” in the terminal) and you more than likely already have Git installed.

You will need Homebrew. Granted other package managers are out there, but you still need Homebrew in your life. I love Homebrew.

I’ve tried installing PhantomJS via other (non-package manager) methods and it was a shameful experience, trust me.

Don’t worry installing Homebrew is pretty damn easy.

Step 1:

Open up the terminal and copy and paste the following into it. Hit enter. This may take a few minutes to finish.

/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

Step 2:

Confirm Homebrew is all up in your system.

brew -v && brew help

This will output the homebrew (brew) version number and the help menu, should you fancy a quick rummage around.

Step 3:

Install PhantomJS. This may take a few minutes to run.

brew install phantomjs

Anyone one that tried and failed to install PhantomJS using the downloadable executable will be kicking themselves now (me included).

Step 4:

Has PhantomJS installed? Which version have I got?

phantomjs -v && phantomjs -h

This will do the same as Step 2 but for PhantomJS.

Step 5:

Install CasperJS. I’m assuming you have Git installed (rightly or wrongly). If you don’t then.. erm.. brew install git =). Run the following commands one after another.

Download CasperJS to a location that you’re happy with e.g. I download most software to a single location ~/src

git clone git://github.com/n1k0/casperjs.git

Access the downloaded file and use Git to checkout the latest version of Casperjs.

Note: The version number may change so maybe run “git tag” first, which will give you all the versions available. Just pick the highest number available and if it differs from 0.6.6 then change the command below to use the latest version e.g. git checkout tags/0.6.7

cd casperjs/ && git checkout tags/0.6.6

Create a link to the CasperJS source file

ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs

Check CasperJS is installed

casperjs --version

Now just to verify that everything is installed in a suitable place please run the following:

which brew && which casperjs && which phantomjs

Which should output something similar to the following:

/usr/local/bin/brew
/usr/local/bin/casperjs
/usr/local/bin/phantomjs

Now that everything is installed and ready to go we can get started with some basic tests. For example purposes only I’m going to run and build the test in a folder called “unit_tests” on the desktop (very imaginative I know). Normally, you would have them checked in to your project repo so other team members can add tests and use exisiting ones.

cd ~/Desktop
mkdir unit_tests
cd unit_tests
touch duckduck.js

Now open up duckduck.js with your favourite text editor e.g. sublime duckduck.js or mate duckduck.js

Copy and paste the following code and save the file:

Now run the following in the terminal:

casperjs test ~/Desktop/unit_tests/duckduck.js

You should get something cool like the following:

The above is just scraping the surface of what we can do. See http://phantomjs.org/ and http://casperjs.org/ for more information.

If you didn’t get the above result then please let me know what went wrong and we can update this to help others. Also, if you think I made a mistake or think something would be easier to understand, please let me know!

View & Comment

DOM, HTML5, & CSS3 Performance

Paul Irish, from the Chrome Developer Relations team, walks through smart techniques to improve the performance of your app. He describes CSS reflows and how to avoid them, hardware accelerated CSS, animation optimization, web workers, benchmarking and build scripts.

  • Reflows, what they are and how to avoid them
  • Hardware accelerated CSS, how to trigger it
  • Letting the browser optimize animation for you with requestAnimationFrame
  • Web Workers
  • Benchmarking
  • Build scripts

View & Comment

Become a Javascript Console Power-User

Level up on the Javascript console in the Chrome DevTools. Look at XHR requests, learn console helper functions to monitor events or explore objects better. Paul Irish from the Chrome team gives you a rundown.

View & Comment

Google I/O 2011: Learning to Love JavaScript

JavaScript remains one of the most popular and important programming languages in history. Web Developer and Chrome Engineer Alex Russell exposes the timeless strengths of the JavaScript language and why it is a vital part of the open web platform. Come hear what’s next for the JavaScript standard and how to get the most out of the new features coming soon in V8 and Chrome.

View & Comment

JavaScript Gig Finder

This week the guys at Songkick gave me access to their API. I had some spare time today to get to know it better and generally mess around with it.

I decided that what I really could do with was a simple way of finding out what gigs and concerts are on near me. So using the data kindly provided by Songkick (thanks guys!) I went ahead and built just that.

Gig Finder

You can view the gig finder on your smart phone or your desktop browser. As always, I’ve made sure that everything is functional in all the modern browsers.

View the application: http://dannycroft.co.uk/lab/gig-finder/

I’d love some feedback if you have any thoughts!

View & Comment

JavaScript W3C Unicorn Bookmarklet

I use the W3C Unicorn Validator for almost all my markup validation needs. However every time I need to validate something I almost always end up googling “W3C Unicorn”. Which I admit is still pretty fast, but I wanted to cut as much faff out of that process as possible.

W3C Unicorn

So today (as in just this minute), I made a JavaScript bookmarklet that will help me trim the fat as it were.

I’m not entirely sure if their are a million of these already online, but a quick Google search didn’t bring anything up (and that was a good enough reason for me).

Anyway, drag the following link into your bookmark toolbar to use it* :

Unicorn Validation

Happy validating!

* If your not sure how to add a bookmarklet to your toolbar then watch this

View & Comment