CGI-Test
view release on metacpan or search on metacpan
lib/CGI/Test/Form.pm view on Meta::CPAN
package CGI::Test::Form;
use strict;
use warnings;
####################################################################
# $Id: Form.pm 411 2011-09-26 11:19:30Z nohuhu@nohuhu.org $
# $Name: cgi-test_0-104_t1 $
####################################################################
# Copyright (c) 2001, Raphael Manfredi
#
# You may redistribute only under the terms of the Artistic License,
# as specified in the README file that comes with the distribution.
#
#
# Class interfacing with the content of a <FORM> tag, which comes from
# a CGI::Test::Page object. The tree nodes we are playing with here are
# direct pointers into the node of the page object.
#
use Carp;
#
# We may not create an instance of all those classes, but the cost of
# lazily requiring them would probably outweigh the cost of loading
# them once and for all, on reasonably sized forms.
#
use CGI::Test::Form::Widget::Button::Submit;
use CGI::Test::Form::Widget::Button::Reset;
use CGI::Test::Form::Widget::Button::Image;
use CGI::Test::Form::Widget::Button::Plain;
use CGI::Test::Form::Widget::Input::Text_Field;
use CGI::Test::Form::Widget::Input::Text_Area;
use CGI::Test::Form::Widget::Input::Password;
use CGI::Test::Form::Widget::Input::File;
use CGI::Test::Form::Widget::Menu::List;
use CGI::Test::Form::Widget::Menu::Popup;
use CGI::Test::Form::Widget::Box::Radio;
use CGI::Test::Form::Widget::Box::Check;
use CGI::Test::Form::Widget::Hidden;
######################################################################
#
# ->new
#
# Creation routine
#
######################################################################
sub new
{
my $this = bless {}, shift;
my ($node, $page) = @_;
$this->{tree} = $node; # <FORM> is the root node of the tree
$this->{page} = $page;
$this->{enctype} = $node->attr("enctype")
|| "application/x-www-form-urlencoded";
$this->{method} = uc $node->attr("method") || "POST";
foreach my $attr (qw(action name accept accept-charset))
{
my $oattr = $attr;
$oattr =~ s/-/_/g;
my $value = $node->attr($attr);
$this->{$oattr} = $value if defined $value;
}
#
# Although ACTION is now required in newer HTML DTDs, it was optional
# in HTML 2.0 and defaults to the base URI of the document.
#
$this->{action} = $page->uri->as_string unless exists $this->{action};
return $this;
}
######################################################################
# DEPRECATED
######################################################################
sub make
{ #
my $class = shift;
return $class->new(@_);
}
#
# Attribute access
#
######################################################################
sub tree
{
my $this = shift;
return $this->{tree};
}
lib/CGI/Test/Form.pm view on Meta::CPAN
#
# Initiate traversal to locate all widgets nodes.
#
my %is_widget = map {$_ => 1} qw(input textarea select button isindex);
my @wg = $this->tree->look_down(sub {$is_widget{$_[ 0 ]->tag}});
#
# Initialize all lists to be empty
#
for my $attr ( qw(buttons inputs radios checkboxes hidden menus widgets) )
{
$this->{$attr} = [];
}
#
# And now sort them out.
#
my %input = ( # [ class name, attribute ]
"submit" => [ 'Button::Submit', "buttons" ],
"reset" => [ 'Button::Reset', "buttons" ],
"image" => [ 'Button::Image', "buttons" ],
"text" => [ 'Input::Text_Field', "inputs" ],
"file" => [ 'Input::File', "inputs" ],
"password" => [ 'Input::Password', "inputs" ],
"radio" => [ 'Box::Radio', "radios" ],
"checkbox" => [ 'Box::Check', "checkboxes" ],
"hidden" => [ 'Hidden', "hidden" ],
);
my %button = ( # [ class name, attribute ]
"submit" => [ 'Button::Submit', "buttons" ],
"reset" => [ 'Button::Reset', "buttons" ],
"button" => [ 'Button::Plain', "buttons" ],
);
my $wlist = $this->{widgets}; # All widgets also inserted there
foreach my $node (@wg)
{
my $tag = $node->tag;
my ($class, $attr);
my $hlookup;
if ($tag eq "input")
{
$hlookup = \%input;
}
elsif ($tag eq "textarea")
{
($class, $attr) = ("Input::Text_Area", "inputs");
}
elsif ($tag eq "select")
{
$attr = "menus";
$class =
($node->attr("multiple") || defined $node->attr("size"))
? "Menu::List"
: "Menu::Popup";
}
elsif ($tag eq "button")
{
$hlookup = \%button;
}
elsif ($tag eq "isindex")
{
warn "ISINDEX is deprecated, ignoring %s", $node->starttag;
next;
}
else
{
confess "reached tag '$tag': invalid tree look_down()?";
}
#
# If $hlookup is defined, we need to look at the TYPE attribute
# within the tag to determine the object to build.
#
# This handles <INPUT TYPE="xxx"> and <BUTTON TYPE="xxx">
#
if (defined $hlookup)
{
my $type = $node->attr("type");
unless (defined $type)
{
warn "missing TYPE indication in %s: %s", uc($tag),
$node->starttag;
next;
}
my $info = $hlookup->{lc($type)};
unless (defined $info)
{
warn "unknown TYPE '%s' in %s: %s", $type, uc($tag),
$node->starttag;
next;
}
($class, $attr) = @$info;
}
#
# Create object of given class, insert into attribute list.
# Objects will not keep a reference on the node, but will reference us.
#
my $obj = "CGI::Test::Form::Widget::$class"->new($node, $this);
push @{$this->{$attr}}, $obj;
push @$wlist, $obj;
}
#
# Special handling for radio buttons: they need to be groupped, so that
# selecting one automatically unselects others from the same group.
#
# Special handling for checkboxes: one may wish to get at a "group of
# checkboxes" instead of an individual checkbox widget.
#
lib/CGI/Test/Form.pm view on Meta::CPAN
beasts.
This class is really the basis of the C<CGI::Test> testing abilities:
it provides the necessary routines to query the CGI widgets present in the
form: buttons, input areas, menus, etc... Queries can be made by type, and
by name. There is also an interface to specifically access groupped widgets
like checkboxes and radio buttons.
All widgets returned by the queries are polymorphic objects, heirs of
C<CGI::Test::Form::Widget>. If the querying interface can be compared to
the human eye, enabling you to locate a particular graphical item on the
browser screen, the widget interface can be compared to the mouse and keyboard,
allowing you to interact with the located graphical components. Please
refer to L<CGI::Test::Form::Widget> for interaction details.
Apart from the widget querying interface, this class also offers a few
services to other C<CGI::Test> components, like handling of I<reset> and
I<submit> actions, which need not be used directly in practice.
Finally, it provides inspection of the <FORM> tag attributes (encoding
type, action, etc...) and, if you really need it, to the HTML tree of
the all <FORM> content. This interface is based on the C<HTML::Element>
class, which represents a tree node. The tree is shared with other
C<CGI::Test> components, it is not a private copy. See L<HTML::Element> if
you are not already familiar with it.
If memory is a problem, you must be aware that circular references are
used almost everywhere within C<CGI::Test>. Because Perl's garbage collector
cannot reclaim objects that are part of such a reference loop, you must
explicitely call the I<delete> method on C<CGI::Test::Form>.
Simply forgetting about the reference to that object is not enough.
Don't bother with it if your regression test scripts die quickly.
=head1 INTERFACE
The interface is mostly a querying interface. Most of the routines return
widget objects, via lists or list references. See L<CGI::Test::Form::Widget>
for details about the interface provided by widget objects, and the
classification.
The order of the widgets returned lists is the same as the order the widgets
appear in the HTML representation.
=head2 Type Querying Interface
There are two groups or routines: one group returns expanded lists, the
other returns list references. They are listed in the table below.
The I<Item Polymorphic Type> column refers to the polymorphic dynamic
type of items held within the list: each item is guaranteed to at least
be of that type, but can be a descendant. Types are listed in the
abridged form, and you have to prepend the string C<CGI::Test::Form::>
in front of them to get the real type.
Expanded List List Reference Item Polymorphic Type
------------- -------------- ----------------------
button_list buttons Widget::Button
checkbox_list checkboxes Widget::Box::Check
hidden_list hidden Widget::Hidden
input_list inputs Widget::Input
menu_list menus Widget::Menu
radio_list radios Widget::Box::Radio
submit_list submits Widget::Button::Submit
widget_list widgets Widget
For instance:
my @widgets = @{$form->widgets}; # heavy style
my @widgets = $form->widget_list; # light style
A given widget may appear in several lists, i.e.the above do not form a
partition over the widget set. For instance, a submit button would appear
in the C<widget_list> (which lists I<all> widgets), in the C<button_list>
and in the C<submit_list>.
=head2 Name Querying Interface
Those routine take a name or a list of names, and return the widgets whose
parameter name is B<exactly> the given name (string comparison). You may
query all widgets, or a particular class, like all buttons, or all input
fields.
There are two groups of routines:
=over 4
=item *
One group allows for multiple name queries, and returns a list of widgets,
one entry for each listed name. Some widgets like radio buttons may have
multiple instances bearing the same name, and in that case only one is
returned. When querying for one name, you are allowed to use scalar context:
my @hidden = $form->hidden_by_name("foo", "bar");
my ($context) = $form->hidden_by_name("context");
my $context = $form->hidden_by_name("context");
When no widget (of that particular type) bearing the requested name is found,
C<undef> is returned for that particular slot, so don't blindly make method
calls on each returned value.
We shall call that group of query routines the B<by-name> group.
=item *
The other group allows for a single name query, but returns a list of all
the widgets (of some particular type when not querying the whole widget list)
bearing that name.
my @hidden = $form->hidden_named("foo");
Don't assume that only radios and checkboxes can have multiple instances
bearing the same name.
We shall call that group of query routines the B<all-named> group.
=back
The available routines are listed in the table below. Note that I<by-name>
queries are singular, because there is at most one returned widget per name
asked, whereas I<all-named> queries are plural, where possible.
The I<Item Polymorphic Type> column refers to the polymorphic dynamic
type of items held within the list: each defined item is guaranteed to at
least be of that type, but can be a descendant. Types are listed in the
abridged form, and you have to prepend the string C<CGI::Test::Form::>
in front of them to get the real type.
By-Name Queries All-Named Queries Item Polymorphic Type
---------------- ----------------- ----------------------
button_by_name buttons_named Widget::Button
checkbox_by_name checkboxes_named Widget::Box::Check
hidden_by_name hidden_named Widget::Hidden
input_by_name inputs_named Widget::Input
menu_by_name menus_named Widget::Menu
radio_by_name radios_named Widget::Box::Radio
submit_by_name submits_named Widget::Button::Submit
widget_by_name widgets_named Widget
=head2 Match Querying Interface
This is a general interface, which invokes a matching callback on each
widget of a particular category. The signature of the matching routines is:
my @matching = $form->widgets_matching(sub {code}, $arg);
and the callback is invoked as:
callback($widget, $arg);
A widget is kept if, and only if, the callback returns true. Be sure to
write your callback so that is only uses calls that apply to the particular
widget. When you know you're matching on menu widgets, you can call
menu-specific features, but should you use that same callback for buttons,
you would get a runtime error.
Each matching routine returns a list of matching widgets. Using the $arg
parameter is optional, and should be avoided unless you have no other choice,
so as to be as stateless as possible.
The following table lists the available matching routines, along with the
polymorphic widget type to be expected in the callback. As usual, you must
prepend the string C<CGI::Test::Form::> to get the real type.
Matching Routine Item Polymorphic Type
------------------- ---------------------
buttons_matching Widget::Button
checkboxes_matching Widget::Box::Check
hidden_matching Widget::Hidden
inputs_matching Widget::Input
menus_matching Widget::Menu
radios_matching Widget::Box::Radio
submits_matching Widget::Button::Submit
widgets_matching Widget
For instance:
my @menus = $form->widgets_matching(sub { $_[0]->is_menu });
my @color = $form->widgets_matching(
sub { $_[0]->is_menu && $_[0]->name eq "color" }
);
is an inefficient way of saying:
my @menus = $form->menu_list;
my @color = $form->menus_matching(sub { $_[0]->name eq "color" });
and the latter can further be rewritten as:
my @color = $form->menus_named("color");
=head2 Form Interface
This provides an interface to get at the attributes of the <FORM> tag.
For instance:
my $enctype = $form->enctype;
to get at the encoding type of that particular form.
The following attributes are available:
accept
accept_charset
action
enctype
method
name
as defined by HTML 4.0.
=head2 Group Querying Interface
There are two kinds of widgets that are architecturally groupped, meaning
more that one instance of that widget can bear the same name: radio buttons
and checkboxes (although you may have a single standalone checkbox).
All radio buttons and checkboxes defined in a form are automatically
inserted into a group of their own, which is an instance of the
C<CGI::Test::Form::Group> class. This class contains all the defined
groups for a particular kind. The routines:
checkbox_groups
radio_groups
give you access to the C<CGI::Test::Form::Group> container. Both routines
may return C<undef> when there is no checkbox or radio button in the form.
See L<CGI::Test::Form::Group> for its querying interface.
=head2 Memory Cleanup
You B<must> call the I<delete> method to break the circular references
( run in 0.839 second using v1.01-cache-2.11-cpan-39bf76dae61 )