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