1WWW::Mechanize::TreeBuiUlsdeerr(C3o)ntributed Perl DocumWeWnWt:a:tMieocnhanize::TreeBuilder(3)
2
3
4
6 WWW::Mechanize::TreeBuilder
7
9 use Test::More tests => 2;
10 use Test::WWW::Mechanize;
11 use WWW::Mechanize::TreeBuilder;
12 # or
13 # use WWW::Mechanize;
14 # or
15 # use Test::WWW::Mechanize::Catalyst 'MyApp';
16
17 my $mech = Test::WWW::Mechanize->new;
18 # or
19 #my $mech = Test::WWW::Mechanize::Catalyst->new;
20 # etc. etc.
21 WWW::Mechanize::TreeBuilder->meta->apply($mech);
22
23 $mech->get_ok('/');
24 is( $mech->look_down(_tag => 'p')->as_trimmed_text, 'Some text', 'It worked' );
25
27 This module combines WWW::Mechanize and HTML::TreeBuilder. Why? Because
28 I've seen too much code like the following:
29
30 like($mech->content, qr{<p>some text</p>}, "Found the right tag");
31
32 Which is just all flavours of wrong - its akin to processing XML with
33 regexps. Instead, do it like the following:
34
35 ok($mech->look_down(_tag => 'p', sub { $_[0]->as_trimmed_text eq 'some text' })
36
37 The anon-sub there is a bit icky, but this means that anyone should
38 happen to add attributes to the "<p>" tag (such as an id or a class) it
39 will still work and find the right tag.
40
41 All of the methods avaiable on HTML::Element (that aren't 'private' -
42 i.e. that don't begin with an underscore) such as "look_down" or
43 "find" are automatically delegated to "$mech->tree" through the magic
44 of Moose.
45
47 Everything in WWW::Mechanize (or which ever sub class you apply it to)
48 and all public methods from HTML::Element except those where
49 WWW::Mechanize and HTML::Element overlap. In the case where the two
50 classes both define a method, the one from WWW::Mechanize will be used
51 (so that the existing behaviour of Mechanize doesn't break.)
52
54 HTML::TreeBuilder::XPath allows you to use use xpath selectors to
55 select elements in the tree. You can use that module by providing
56 parameters to the moose role:
57
58 with 'WWW::Mechanize::TreeBuilder' => {
59 tree_class => 'HTML::TreeBuilder::XPath'
60 };
61
62 # or
63
64 # NOTE: No hashref using this method
65 WWW::Mechanize::TreeBuilder->meta->apply($mech,
66 tree_class => 'HTML::TreeBuilder::XPath';
67 );
68
69 and class will be automatically loaded for you. This class will be used
70 to construct the tree in the following manner:
71
72 $tree = $tree_class->new_from_content($req->decoded_content)->elementify;
73
74 You can also specify a "element_class" parameter which is the
75 (HTML::Element sub)class that methods are proxied from. This module
76 provides defaults for element_class when "tree_class" is
77 "HTML::TreeBuilder" or "HTML::TreeBuilder::XPath" - it will warn
78 otherwise.
79
81 Ash Berlin "<ash@cpan.org>"
82
84 Same as Perl 5.8, or at your option any later version of Perl.
85
86
87
88perl v5.12.0 2010-04-13 WWW::Mechanize::TreeBuilder(3)