Test::WWW::Selenium::More

I recently released Test::WWW::Selenium::More to CPAN. It is a small collection of utilities to help you write Selenium tests. Here are some reasons to use it:

  1. It has a manual which provides a short but fairly comprehensive howto guide to Selenium testing in Perl.

  2. It uses Moose so you can more easily use roles. For example you might want a role for methods that deal with authentication and a role for methods that deal with payments.

  3. Smarter testing with methods like wait_for_jquery() and jquery_click(). You should never sleep() in your Selenium tests because that leads to slow tests with random failures which leads to frustration, low morale, hair pulling, and heavy drinking.

  4. Method chaining. Here is what this looks like:

    use Test::Most; use Test::WWW::Selenium::More;

    Test::WWW::Selenium::More->new()

    –>note(‘Search google’) –>open_ok(“http://www.google.com”) –>title_like(qr/Google Search/) –>type_ok(‘cat pictures’) –>follow_link_ok(‘Search’)

    –>note(‘Check the number of results’) –>is_text_present_ok(‘2 bajillion results’);

    done_testing;

Bugs or patches? https://github.com/kablamo/Test-WWW-Selenium-More

New on CPAN: MooseX::CachingProxy

Last week I released MooseX::CachingProxy to CPAN.

Its a small module that intercepts requests from your LWP based application. Those requests are relayed on to the intended server unless they already exist in the cache.

To toggle on and off the caching proxy, MooseX::CachingProxy provides the attribute ‘caching_proxy’. Here is a quick demo:

package MyApp;
use Moose;
use WWW::Mechanize; # or any LWP based library
with 'MooseX::CachingProxy';

sub url { 'http://example.com' } # required by MooseX::CachingProxy

sub download { 
    my $self = shift;
    $self->start_caching_proxy;
    return WWW::Mechanize->new()->get($self->url . '/foo'); 
}

Under the covers, its a tiny Plack application that mashes up Plack::Middleware::Cache and Plack::App::Proxy.

Log::JSON unleashed upon the CPAN

I released Log::JSON v0.001 to CPAN today. Its a very simple JSON logger.

The advantage of a JSON logger is that a human can open a mysterious new log file and quickly decipher the content because each piece of information is labeled. Using JSON also means that parsing the log file and reviving the data structures is trivial.

Here is some example usage for you:

use Log::JSON;
my $logger = Log::JSON->new(
    file            => '/path/errorlog.json', # required
    date            => 1, # optional
    remove_newlines => 1, # optional
);
$logger->log(a => 1, b => 2);
# '/path/errorlog.json' now contains:
# {"__date":"2010-03-28T23:15:52Z","a":1,"b":1}

I wish I had written it as a Log::Dispatch plugin, and perhaps I’ll get around to that sometime.

One problem with using JSON is that there is a lot repetition and if your log file is a bajillion lines long, then that’s going to be a big file. Happily, file compression solves this problem very well. And Vim and less handle compressed files on the fly so viewing the file is not inconvenient. And now that I write this, I think some kind of compression feature may be nice for Log::JSON and pretty great for Log::Dispatch too.

Log::JSON on github: https://github.com/kablamo/Log-JSON

Produce professional pdfs with Perl

I created fancy marketing brochures for work using Perl and PDF::API2.  After some struggle I emerged victorious from the battle.  Here is what I learned.

1.  PDF::API2 is a low level library.  If you are going to spend a month of your life building pdfs, you will want to build tools on top of it.  For example I needed higher level tools for creating text, for layout, for creating tables, and for creating charts.

2.  For PDFs, the origin (0, 0) is the bottom left corner of the document.  This is annoying because its more natural to do layout starting from the top left corner.  If you are building higher level methods, make them do layout from the top left.  Otherwise you will end up with a method that builds tables, from the bottom up which is a layout problem if your table can have an arbitrary number of rows.  Consider using PDF::API2::Content->translate().

3.  Build each piece of your page as a small pdf and then insert it into your page.  This helps with layout and it helps with speed during testing as you can just iterate on that one component rather than generating the whole document.  

4. Use lines to measure to indicate margins and gutters and to line up components.  This is similar to what designers are doing in Illustrator.  

5. Use SVG::TT::Graph to easily create charts as SVG using CSS.  Then convert them from SVG to PDF using batik.

6. PDF::API2 assumes you know all about print room terminology like media boxes, trim boxes, and bleed boxes.  If your document is going to print to the margin, you need to understand these printroom concepts.  

7. Images should be at least 300 dpi.  It makes a difference.

 

fugitive: VIM + Git

If you use VIM and Git, you should use fugitive.vim.  Its so useful that I have been using it hourly for the last 2 weeks.  Here's whats awesome about it:

- View the current branch in your statusline. 

- Use :Gdiff to bring up the staged version of the file side by side with the working tree version

- Use :Gstatus to see the output of git-status.  Put your cursor over a filename and press `-` to stage that file.  Use '-' again to unstage the file.  Press 'D' to diff that file with HEAD.

- Use :Gblame to bring up a buffer showing who commited which line of code in your current buffer.  Place your cursor on any line and press enter to bring up a buffer showing that file as it was when that line of code was originally committed.  Type 'o' to open a vertical split to see the details of that commit including the log message and a diff of changes.

View my latest .vimrc in my dotfiles github repo.

 

 

Vim tip: How to spell check

Use :set spell to turn on spell-checking. Misspelled words will be highlighted with one color and miscapitalized words will be highlighted with another color. Vim is smart enough to not spell check your code. It will automatically check your git commit messages though.

Also you can place your cursor over a word and type z= to get a list of suggested replacedments. See my Stackoverflow answer for more details.

Vim with Slime and Scratch

Slime pastes selected text to a screen session.  This means you can use a REPL to evaluate code you are editing in Vim.  So try this work flow for example:

  1. Start a named screen session: screen -S whatever
  2. Start up re.pl or sqlite3 or bash or whatever
  3. In a second window, edit some code with Vim and place your cursor over some code and type <C-c><C-c>
  4. Watch your code evaluate in your REPL.
I find this especially useful for working on SQL queries because its easier to edit a large query in Vim than on the command line.  Its also great for testing small Perl snippets.

I use Scratch and Slime together.  Scratch is a simple plugin that lets you create a new un savable scratch buffer.

Devel::REPL and delicious magical chocolate ponies

Today I learned about Devel::REPL.  Devel::REPL is Perl's command evaluation loop thinger.  In Python, you type 'python', in Ruby, you type 'ruby' and in Perl, you type 're.pl' which is a script that uses Devel::REPL.  So thats less than intuitive.  Like so much of Perl, this stuff is not obvious to outsiders and newcomers.  But once you dig under the covers you find that Perl is filled with delicious magical chocolate ponies.  Seriously, python and ruby REPLs are way not as cool as Perl's REPL.  afaik.  But whos keeping score anyway.

The real power comes from its plugin system.  Search CPAN for 'devel repl plugin' to see all of them.   All you gotta do to load them is add some commands to $HOME/.re.pl/repl.rc.  Here is mine:

What to do with your $1 million nest egg

I wish had this problem.  

Anyway, Ryan Waggoner's article "How to retire at 30 on 1 million"  made Hacker News today.  Instead of investing in the stock market for a measly 2% return, Ryan suggests buying and operating commercial real estate.  Like an apartment building.  He claims you can make a monthly income tax free of $100,000 off an initial investment of $1 million.  

I doubt there is anything magical about real estate (its not clear to me if he thinks so) and I wish he had mentioned the additional risk you take on when running a business not to mention the lack of a diverse portfolio.  But he makes an interesting point: If you are willing to take the risk and make the effort, putting that $1 million into running a business could potentially be a lot more lucrative than putting it into the stock market.