WWW-Mechanize-Chrome

 view release on metacpan or  search on metacpan

lib/WWW/Mechanize/Chrome.pm  view on Meta::CPAN


Sets the field with the name given in C<$selector> to the given value.
Returns the value.

The method understands very basic CSS selectors in the value for C<$selector>,
like the L<HTML::Form> find_input() method.

A selector prefixed with '#' must match the id attribute of the input.
A selector prefixed with '.' matches the class attribute. A selector
prefixed with '^' or with no prefix matches the name attribute.

By passing the array reference C<@pre_events>, you can indicate which
Javascript events you want to be triggered before setting the value.
C<@post_events> contains the events you want to be triggered
after setting the value.

By default, the events set in the
constructor for C<pre_events> and C<post_events>
are triggered.

=cut

sub field($self,$name,$value,$index=undef,$pre=undef,$post=undef) {
    if( ref $index ) { # old API
        carp "Old API style for ->field() is deprecated. Please fix the call to pass undef for the third parameter if using pre_events/post_events!";
        $post  = $pre;
        $pre   = $index;
        $index = undef;
    };
    $self->get_set_value(
        name => $name,
        value => $value,
        pre => $pre,
        post => $post,
        index => $index,
        node => $self->current_form,
    );
}

=head2 C<< $mech->sendkeys( %options ) >>

    $mech->sendkeys( string => "Hello World" );

Sends a series of keystrokes. The keystrokes can be either a string or a
reference to an array containing the detailed data as hashes.

=over 4

=item B<string> - the string to send as keystrokes

=item B<keys> - reference of the array to send as keystrokes

=item B<delay> - delay in ms to sleep between keys

=back

=cut

sub sendkeys_future( $self, %options ) {
    $options{ keys } ||= [ map +{ type => 'char', text => $_ },
                           split m//, $options{ string }
                         ];

    my $f = Future->done(1);

    for my $key (@{ $options{ keys }}) {
        $f = $f->then(sub {
            $self->target->send_message('Input.dispatchKeyEvent', %$key );
        });
        if( defined $options{ delay }) {
            $f->then(sub {
                $self->sleep( $options{ delay });
            });
        };
    };

    return $f
};

sub sendkeys( $self, %options ) {
    $self->sendkeys_future( %options )->get
}

=head2 C<< $mech->upload( $selector, $value ) >>

  $mech->upload( user_picture => 'C:/Users/Joe/face.png' );

Sets the file upload field with the name given in C<$selector> to the given
file. The filename must be an absolute path and filename in the local
filesystem.

The method understands very basic CSS selectors in the value for C<$selector>,
like the C<< ->field >> method.

=cut

# Page.setInterceptFileChooserDialog
# doesn't help anything, since we can only suppress that dialog but not
# supply file names or anything. See the ->upload() method for how to actually
# set filenames

sub upload($self,$name,$value) {
    my %options;

    my @fields = $self->_field_by_name(
                     name => $name,
                     user_info => "upload field with name '$name'",
                     %options );
    $value = [$value]
        if ! ref $value;

    # Stringify all files:
    @$value = map { "$_" } @$value;

    if( @fields ) {
        $self->target->send_message('DOM.setFileInputFiles',
            nodeId => 0+$fields[0]->nodeId,
            files => $value,
            )->get;
    }



( run in 0.625 second using v1.01-cache-2.11-cpan-71847e10f99 )