HTTP::WebTest::ReportPlugin - Subclass for HTTP::WebTest report plugins.


HTTP-WebTest documentation Contained in the HTTP-WebTest distribution.

Index


Code Index:

NAME

Top

HTTP::WebTest::ReportPlugin - Subclass for HTTP::WebTest report plugins.

SYNOPSIS

Top

Not applicable.

DESCRIPTION

Top

This is a subclass of HTTP::WebTest. HTTP::WebTest report plugin classes can inherit from this class. It handles some test parameters common to report plugins by providing implementation of the method print.

TEST PARAMETERS

Top

output_ref

GLOBAL PARAMETER

A reference to a scalar that accumulates text of test report. If this test parameter is specified then value of test parameter fh_out is ignore.

This parameter can be used only when passing the test parameters as arguments from a calling Perl script.

fh_out

GLOBAL PARAMETER

A filehandle (or anything else that supports print) to use for test report output. This parameter is ignored if test parameter output_ref is specified also.

This parameter can be used only when passing the test parameters as arguments from a calling Perl script.

mail

GLOBAL PARAMETER

Option to e-mail output to one or more addresses specified by mail_addresses test parameter.

mail_success_subject

GLOBAL PARAMETER

Sets Subject header for test report e-mails when all tests are passed successfully. In this string some character sequences have special meaning (see mail_failure_subject parameter for their description).

Default Value

Web tests succeeded

mail_failure_subject

GLOBAL PARAMETER

Sets Subject header for test report e-mails when some tests fail. In this string some character sequences have special meaning:

%f

the number of failed tests

%s

the number of successful tests

%t

the total number of tests

%%

replaced with single %

Default Value

WEB TESTS FAILED! FOUND %f ERROR(S)

mail_addresses

GLOBAL PARAMETER

A list of e-mail addresses where report will be send (if sending report is enabled with mail test parameter).

* all

Send e-mail containing test results.

* errors

Send e-mail only if one or more tests fails.

* no

Do not send e-mail.

Default value

no

mail_server

GLOBAL PARAMETER

Fully-qualified name of of the mail server (e.g., mailhost.mycompany.com).

Default value

localhost

mail_from

GLOBAL PARAMETER

Sets From: header for test report e-mails.

Default Value

Name of user under which test script runs.

CLASS METHODS

Top

test_output ()

Returns

Returns a reference to buffer that stores copy of test output.

start_tests ()

This method is called by HTTP::WebTest at the beginning of the test run. Its implementation in this class initializes the output buffer for the test report.

If you redefine this method in a subclass, be sure to call the superclass method in the new method:

    sub start_tests {
        my $self = shift;

        $self->SUPER::start_tests;

        # your code here
        ....
    }

end_tests ()

This method is called by HTTP::WebTest at the end of a test run. Its implementation in this class e-mails the test report according test parameters mail***.

If you redefine this method in subclass be sure to call the superclass method in the new method:

    sub end_tests {
        my $self = shift;

        # your code here
        ....

        $self->SUPER::end_tests;
    }

COPYRIGHT

Top

SEE ALSO

Top

HTTP::WebTest

HTTP::WebTest::API (HTTP::WebTest::API)

HTTP::WebTest::Plugins (HTTP::WebTest::Plugins)

HTTP::WebTest::Plugin


HTTP-WebTest documentation Contained in the HTTP-WebTest distribution.
# $Id: ReportPlugin.pm,v 1.10 2003/09/05 19:32:18 m_ilya Exp $

package HTTP::WebTest::ReportPlugin;

use strict;

use Net::SMTP;

use HTTP::WebTest::Utils qw(make_access_method);

use base qw(HTTP::WebTest::Plugin);

# declare some supported test params
sub param_types {
    return q(output_ref           stringref
                          fh_out               anything
                          mail_addresses       list('scalar','...')
                          mail                 scalar
                          mail_server          scalar
                          mail_from            scalar
                          test_name            scalar
                          mail_success_subject scalar
                          mail_failure_subject scalar);
}

*test_output = make_access_method('TEST_OUTPUT', sub { my $s = ''; \$s } );

sub print {
    my $self = shift;

    $self->global_validate_params(qw(output_ref fh_out));

    my $output_ref = $self->global_test_param('output_ref');
    my $fh_out     = $self->global_test_param('fh_out');

    my $text = join '', @_;

    ${$self->test_output} .= $text;

    if(defined $output_ref) {
	${$output_ref} .= $text;
    } elsif(defined $fh_out) {
	print $fh_out $text;
    } else {
	print $text;
    }
}

sub start_tests {
    my $self = shift;

    # reset temporary output storage
    $self->test_output(undef);
}

sub end_tests {
    my $self = shift;

    if($self->_email_report_is_expected) {
        $self->_send_email_report;
    }
}

# check if we need to mail report
sub _email_report_is_expected {
    my $self = shift;

    $self->global_validate_params(qw(mail));

    my $mail = $self->global_test_param('mail');

    return unless defined $mail;
    return unless $mail eq 'all' or $mail eq 'errors';
    return if $mail eq 'errors' and $self->webtest->have_succeed;

    return 1;
}

# sends test report on email
sub _send_email_report {
    my $self = shift;

    $self->global_validate_params(qw(mail_addresses mail_server mail_from));

    my $mail_addresses = $self->global_test_param('mail_addresses');
    my $mail_server    = $self->global_test_param('mail_server', 'localhost');
    my $mail_from      = $self->global_test_param('mail_from');

    my $smtp = Net::SMTP->new($mail_server);
    die "HTTP::WebTest: Can't create Net::SMTP object"
	unless defined $smtp;

    my $from = $mail_from || getlogin() || getpwuid($<) || 'nobody';

    $self->_smtp_cmd($smtp, 'mail', $from);
    $self->_smtp_cmd($smtp, 'to', @$mail_addresses);
    $self->_smtp_cmd($smtp, 'data');
    $self->_smtp_cmd($smtp, 'datasend', "From: $from\n");
    {
        my $mail_addresses = join ', ', @$mail_addresses;
        $self->_smtp_cmd($smtp, 'datasend', "To: $mail_addresses\n");
    }
    $self->_smtp_cmd($smtp, 'datasend',
                     'Subject: ' . $self->_subject_header . "\n");
    $self->_smtp_cmd($smtp, 'datasend', "\n");
    $self->_smtp_cmd($smtp, 'datasend', ${$self->test_output});
    $self->_smtp_cmd($smtp, 'dataend');
    $self->_smtp_cmd($smtp, 'quit');
}

# returns value of subject header for email report
sub _subject_header {
    my $self = shift;

    $self->global_validate_params(qw(mail_success_subject mail_failure_subject));

    my $success_subject
        = $self->global_test_param('mail_success_subject',
                                   'Web tests succeeded');
    my $fail_subject
        = $self->global_test_param('mail_failure_subject',
                                   'WEB TESTS FAILED! FOUND %f ERROR(S)');

    my %replace = ('f' => $self->webtest->num_fail,
                   's' => $self->webtest->num_succeed,
                   't' => ($self->webtest->num_fail +
                           $self->webtest->num_succeed),
                   '%' => '%'
                  );

    my $subject = ($self->webtest->have_succeed ?
                   $success_subject :
                   $fail_subject);

    $subject =~ s/%(.)/exists $replace{$1} ? $replace{$1} : '%' . $1/ge;

    return $subject;
}

# simple helper method that automates error handling
sub _smtp_cmd {
    my $self = shift;
    my $smtp = shift;
    my $cmd = shift;

    my $ret = $smtp->$cmd(@_);

    unless($ret) {
	my $msg = $smtp->message;
	die "HTTP::WebTest: mail error for command $cmd: $msg";
    }
}

1;