Mojoliciousアプリの出力するDOMを詳細にテストするやつ。
https://github.com/jamadam/Test-Mojo-DOM
Test::Mojoだけだと、domのチェックはelement_exitsとelement_exits_notしかできないので、詳細なテストはcontent_*でやるか、DOMを取り出してからTest::Moreを使う。
$t->get_ok('/'); my $dom = $t->tx->res->dom; is $dom->at('#hoge')->attrs('href'), 'http://example.com', 'right URL';
Test::Mojoと似た流れでDOMのテストもやりたい!
ということで、Test::Mojo::DOMを使うと下記のようにできるよ。APIは変わるかもだよ。
use Test::Mojo::DOM; use Test::More tests => 35; my $t = Test::Mojo::DOM->new(MyApp->new); $t->get_ok('/') ->status_is(200) ->dom_inspector(sub { my $t = shift; $t->at('a') ->attr_is('href', '../') ->attr_isnt('href', './') ->attr_like('href', qr'\.\./') ->attr_unlike('href', qr'\.\./a') ->text_is('some link') ->text_isnt('some link2') ->text_like(qr'some') ->text_unlike(qr'some2') ->has_attr('href') ->has_attr('empty') ->has_attr_not('not_exists'); $t->at('a')->get(1) ->text_is('some link2'); $t->at('a:nth-child(2)') ->text_is('some link2'); $t->at('a')->each(sub { my $t = shift; $t->text_like(qr{.}); $t->text_unlike(qr{a}); $t->attr_like('href', qr{.}); $t->attr_unlike('href', qr{a}); }); $t->at('a')->parent->attr_is('id', 'some_p'); $t->at('a')->parent->parent->attr_is('id', 'wrapper'); $t->at('#some_p')->has_child('a'); $t->at('#some_p2')->has_child_not('a'); $t->at('#some_img')->has_class('class1'); $t->at('#some_img')->has_class('class2'); $t->at('#some_img')->has_class('class3'); $t->at('#some_img')->has_class_not('class4'); });