--- 
author: 
  email: ash@firemirror.com
  keyid: bfc7465ebdca5337
  name: Ash Berlin
categories: []

date: 2007-09-05T12:49:07Z
guid: 5B4C0FBC-5B99-11DC-94D7-AB27370D5E4E
modified: 2007-09-05T12:49:07Z
raw: "-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nLast week at YAPC::EU I saw a few interesting talks, including Stevan's [Moose](http://www.iinteractive.com/moose/yapc_eu_2007_slides/start.html) introduction. I found this talk really helpful, maybe in part because I had read through Moose's documentation and cookbook examples once a month or so ago.\n\nSo I wanted to learn Moose a bit better, and I'm a firm believer of learning new technologies by actually writing something using them, so I needed a project - not to complex, but also not trivial. And I had just the thing - something I made a start on in June, but sadly only ever existed on my laptop that got stolen. I present the following code snippet for consideration taken from a test suite at work:\n\n    lang:perl\n    my $mech = WWW::Mechanize->new;\n\n    ok( $mech->get(\"$base/index\") );\n    ok( $mech->content =~ m{<div id=\"header\">Welcome To My Site</div>} );\n\nWhat's wrong with it? Well for a start, it should be using Test::WWW::Mechanize which fixes a few problems. But its still parsing XHTML (you *do* write XHTML web sites don't you.) using a regexp, which is always a bad idea. What happens if you change the ID of the element, or add a class to it? It'll stop working and your tests will break. My solution? [WWW::Mechanize::TreeBuilder](http://search.cpan.org/dist/WWW-Mechanize-TreeBuilder/lib/WWW/Mechanize/TreeBuilder.pm):\n\n\n    lang:perl\n    my $mech = Test::WWW::Mechanize->new;\n    WWW::Mechanize::TreeBuilder->meta->apply($mech);\n\n    $mech->get_ok(\"$base/index\");\n    is( $mech->look_down(_tag => 'div', id => 'header')->as_trimmed_text, 'Welcome To My Site');\n\nMuch better.\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1.4.7 (Darwin)\n\niD8DBQFG3qdSv8dGXr3KUzcRAkvnAJ9rXeqgm0GJy8f0hi3M373REu6X4QCfcBnv\njAS13SadFxbyLKwGY8tf8hQ=\n=cbld\n-----END PGP SIGNATURE-----\n"
signed: 1
summary: " Last week at YAPC::EU I saw a few interesting …"
tags: 
  - 
    perl: 0
  - 
    testing: 0
  - 
    cpan: 0
  - 
    moose: 0
text: "\nLast week at YAPC::EU I saw a few interesting talks, including Stevan's\nMoose [1] introduction. I found this talk really helpful, maybe in part\nbecause I had read through Moose's documentation and cookbook examples\nonce a month or so ago.\n\nSo I wanted to learn Moose a bit better, and I'm a firm believer of\nlearning new technologies by actually writing something using them, so I\nneeded a project - not to complex, but also not trivial. And I had just\nthe thing - something I made a start on in June, but sadly only ever ex-\nisted on my laptop that got stolen. I present the following code snippet\nfor consideration taken from a test suite at work:\n\nWhat's wrong with it? Well for a start, it should be using\nTest::WWW::Mechanize which fixes a few problems. But its still parsing\nXHTML (you do write XHTML web sites don't you.) using a regexp, which is\nalways a bad idea. What happens if you change the ID of the element, or\nadd a class to it? It'll stop working and your tests will break. My so-\nlution? WWW::Mechanize::TreeBuilder [2]:\n\nMuch better.\n\n-- \n [1] http://www.iinteractive.com/moose/yapc_eu_2007_slides/start.html\n [2] http://search.cpan.org/dist/WWW-Mechanize-\n     TreeBuilder/lib/WWW/Mechanize/TreeBuilder.pm\n"
title: WWW::Mechanize::TreeBuilder
type: markdown
uri: http://perlitist.com/articles/www-mechanize-treebuilder
xhtml: "<p>Last week at YAPC::EU I saw a few interesting talks, including Stevan&apos;s <a href=\"http://www.iinteractive.com/moose/yapc_eu_2007_slides/start.html\">Moose</a> introduction. I found this talk really helpful, maybe in part because I had read through Moose&apos;s documentation and cookbook examples once a month or so ago.</p><p>So I wanted to learn Moose a bit better, and I&apos;m a firm believer of learning new technologies by actually writing something using them, so I needed a project - not to complex, but also not trivial. And I had just the thing - something I made a start on in June, but sadly only ever existed on my laptop that got stolen. I present the following code snippet for consideration taken from a test suite at work:</p><pre><code><span class=\"Keyword\">my</span><span class=\"Normal\"> </span><span class=\"DataType\">$mech</span><span class=\"Normal\"> = </span><span class=\"Function\">WWW::Mechanize</span><span class=\"Normal\">-&gt;new;</span><span class=\"Normal\">\n</span><span class=\"Normal\">\n</span><span class=\"Normal\">ok( </span><span class=\"DataType\">$mech</span><span class=\"Normal\">-&gt;</span><span class=\"DataType\">get</span><span class=\"Normal\">(</span><span class=\"Operator\">&quot;</span><span class=\"DataType\">$base</span><span class=\"String\">/index</span><span class=\"Operator\">&quot;</span><span class=\"Normal\">) );</span><span class=\"Normal\">\n</span><span class=\"Normal\">ok( </span><span class=\"DataType\">$mech</span><span class=\"Normal\">-&gt;</span><span class=\"DataType\">content</span><span class=\"Normal\"> =~ </span><span class=\"Operator\">m{</span><span class=\"Others\">&lt;div id=&quot;header&quot;&gt;Welcome To My Site&lt;/div&gt;</span><span class=\"Operator\">}</span><span class=\"Normal\"> );</span><span class=\"Normal\">\n</span></code></pre><p>What&apos;s wrong with it? Well for a start, it should be using Test::WWW::Mechanize which fixes a few problems. But its still parsing XHTML (you do write XHTML web sites don&apos;t you.) using a regexp, which is always a bad idea. What happens if you change the ID of the element, or add a class to it? It&apos;ll stop working and your tests will break. My solution? <a href=\"http://search.cpan.org/dist/WWW-Mechanize-TreeBuilder/lib/WWW/Mechanize/TreeBuilder.pm\">WWW::Mechanize::TreeBuilder</a>:</p><pre><code><span class=\"Keyword\">my</span><span class=\"Normal\"> </span><span class=\"DataType\">$mech</span><span class=\"Normal\"> = </span><span class=\"Function\">Test::WWW</span><span class=\"Normal\">::</span><span class=\"Function\">Mechanize</span><span class=\"Normal\">-&gt;new;</span><span class=\"Normal\">\n</span><span class=\"Function\">WWW::Mechanize</span><span class=\"Normal\">::</span><span class=\"Function\">TreeBuilder</span><span class=\"Normal\">-&gt;meta-&gt;apply(</span><span class=\"DataType\">$mech</span><span class=\"Normal\">);</span><span class=\"Normal\">\n</span><span class=\"Normal\">\n</span><span class=\"DataType\">$mech</span><span class=\"Normal\">-&gt;</span><span class=\"DataType\">get_ok</span><span class=\"Normal\">(</span><span class=\"Operator\">&quot;</span><span class=\"DataType\">$base</span><span class=\"String\">/index</span><span class=\"Operator\">&quot;</span><span class=\"Normal\">);</span><span class=\"Normal\">\n</span><span class=\"Normal\">is( </span><span class=\"DataType\">$mech</span><span class=\"Normal\">-&gt;</span><span class=\"DataType\">look_down</span><span class=\"Normal\">(_tag =&gt; </span><span class=\"Operator\">&apos;</span><span class=\"String\">div</span><span class=\"Operator\">&apos;</span><span class=\"Normal\">, id =&gt; </span><span class=\"Operator\">&apos;</span><span class=\"String\">header</span><span class=\"Operator\">&apos;</span><span class=\"Normal\">)</span><span class=\"Operator\">-</span><span class=\"Normal\">&gt;as_trimmed_text, </span><span class=\"Operator\">&apos;</span><span class=\"String\">Welcome To My Site</span><span class=\"Operator\">&apos;</span><span class=\"Normal\">);</span><span class=\"Normal\">\n</span></code></pre><p>Much better.</p>"
