Author image

A fully-automated testing rig #3

Part 3: Using other scripts, datasources and directories

Earlier I described a bit about the scope of the project and how careful planning would be needed to keep the project growing smoothly. One result of this is our test template - a basic test script outline which most scripts should stick to - which makes test scripts easier to update because they all follow the same structure (yay standardisation!).

The template neatly gets most of the key setup/config variables from a config file in the utilities folder. This, though, was harder to get going than we thought thanks to some niggles with absolute and relative directories...

Here are a couple of things I discovered making this work:

  1. Sometimes you need to use absolute directories, and that's sort of ok.

    Ideally we would write everything to use relative directories, and that's an important thing when the testing server or directory may change or move, but sometimes you just can't do it that way. We spent a while trying to get PhantomCSS to run properly using relative directories, to no avail. In the end, we needed to borrow some useful boilerplate from PhantomJS to make things work. It turns out you can borrow PhantomJS' filesystem module though, and that enabled us to very easily work with absolute directories. Using their API we could then do the following to combine the working directory with the path to PhantomCSS within our project:
    # (If this looks weird, it's because it's in CoffeeScript!)  
    # Get filesystem module from phantomjs so we can find out our current directory
    fs = require 'fs'
    # Get phantom config info from single location
    phantomcss_config = require './../../utilities/phantomcss_config'
    # Get PhantomCSS on board
    phantomcss = require "#{fs.workingDirectory}#{phantomcss_config.phantomcss_location}"
  2. When you start experimenting with directories, they don't all do what you think they'll do.

    So we have a directory for *utilities* and a directory for *tests*, and our *post* script is stored neatly in the *utilities* directory. When the *post* script runs, from where / relative to which directory does it run? Ten points for the correct answer... The answer, oddly, is the *tests* directory, and this took a while to figure out. That's right: the post script 'runs from' the test suite directory. This was crazy strange, but we've had to run with it. So here's our *post* script, stored in the *utilities* directory, changing over to the *utilities* directory to access its adjacent config file:
    phantomcss_config = require './../utilities/phantomcss_config'
    Fun, huh!
  3. It is, once you get your directory paths sorted, really easy to externalise your config / datasources.

    We simply stored a plain ol' JSON object in our *utilities* directory, and all our tests can access it. This is used to provide to our scripts both the PhantomCSS configuration and a set of screen resolutions (similar to this gist).
Next article in series: 
Previous article in series: