Since : I always work for my future. And : Linux is the future. So: I work for Linux

网页自动测试 2. 理解selenium的perl 工具

上一篇 / 下一篇  2008-05-06 02:34:39 / 个人分类:automation

要用到selenium的perl工具,自然需要对它有所了解。

selenium的perl工具很好用,在和selenium IDE联合到一起之后,写一段即便是很复杂的测试程序也变得很简单:
=======tc_adduser.pl========
#!/usr/bin/perl

use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;

my $sel = Test::WWW::Selenium->new( host => "master",
                                    port => 4444,
                                    browser => "*firefox",
                                    browser_url => "https://master.test.com");

$sel->open_ok("/landingpage");
$sel->click_ok("link=Add User");
$sel->wait_for_page_to_load_ok("30000");
$sel->type_ok("form_title", "auto001");
$sel->type_ok("form_givenname", "selenium");
$sel->type_ok("form_sn", "001");
$sel->type_ok("form_krbprincipalkey", "some123");
$sel->type_ok("form_krbprincipalkey_confirm", "some123");
$sel->click_ok("document.form.submit[1]");
$sel->wait_for_page_to_load_ok("30000");
$sel->is_text_present_ok("s001 added!");
=======tc_adduser.pl end========

这段程序的目的就是建立一个新的用户。
程序执行也很容易:

$ perl tc_adduser.pl
 
ok 1 - open, /landingpage
ok 2 - click, link=Add User
ok 3 - wait_for_page_to_load, 30000
ok 4 - type, form_title, auto001
ok 5 - type, form_givenname, selenium
ok 6 - type, form_sn, 001
ok 7 - type, form_krbprincipalkey, some123
ok 8 - type, form_krbprincipalkey_confirm, some123
ok 9 - click, document.form.submit[1]
ok 10 - wait_for_page_to_load, 30000
ok 11 - is_text_present, s001 added!
1..11

我自己希望能够对selenium的perl module 有更多的了解,所以花了些时间看进去。
基本上,任何一个类似于open_ok的程序都是调用Test::Simple的ok程序。从它的源程序里面可以看出来:
========== Selenium.pm 源程序 ==================
...前面省略:
sub AUTOLOAD {
    my $name = $AUTOLOAD;
    $name =~ s/.*:://;
    return if $name eq 'DESTROY';

    my $sub;
    if ($name =~ /(\w+)_(is|isnt|like|unlike)$/i) {
        my $getter = "get_$1";
        my $comparator = $comparator{lc $2};

        # make a subroutine that will call Test::Builder's test methods
        # with selenium data from the getter
        if ($no_locator{$1}) {
            $sub = sub {
                my( $self, $str, $name ) = @_;
                diag "Test::WWW::Selenium running $getter (@_[1..$#_])"
                    if $self->{verbose};
                $name = "$getter, '$str'"
                    if $self->{default_names} and !defined $name;
                no strict 'refs';
                return $Test->$comparator( $self->$getter, $str, $name );
            };
        }
        else {
            $sub = sub {
                my( $self, $locator, $str, $name ) = @_;
                diag "Test::WWW::Selenium running $getter (@_[1..$#_])"
                    if $self->{verbose};
                $name = "$getter, $locator, '$str'"
                    if $self->{default_names} and !defined $name;
                no strict 'refs';
                return $Test->$comparator( $self->$getter($locator), $str, $name );
            };
        }
    }
   elsif ($name =~ /(\w+?)_?ok$/i) {
        my $cmd = $1;

        # make a subroutine for ok() around the selenium command
        $sub = sub {
            my( $self, $arg1, $arg2, $name ) = @_;
            if ($self->{default_names} and !defined $name) {
                $name = $cmd;
                $name .= ", $arg1" if defined $arg1;
                $name .= ", $arg2" if defined $arg2;
            }
            diag "Test::WWW::Selenium running $cmd (@_[1..$#_])"
                    if $self->{verbose};

            local $Test::Builder::Level = $Test::Builder::Level + 1;
            my $rc = '';
            eval { $rc = $self->$cmd( $arg1, $arg2 ) };
            die $@ if $@ and $@ =~ /Can't locate object method/;
            diag($@) if $@;
           return ok( $rc, $name );
        };
    }

    # jump directly to the new subroutine, avoiding an extra frame stack
    if ($sub) {
        no strict 'refs';
        *{$AUTOLOAD} = $sub;
        goto &$AUTOLOAD;
    }
    else {
        # try to pass through to WWW::Selenium
        my $sel = 'WWW::Selenium';
        my $sub = "${sel}::${name}";
        goto &$sub if exists &$sub;
        my ($package, $filename, $line) = caller;
        die qq(Can't locate object method "$name" via package ")
            . __PACKAGE__
            . qq(" (also tried "$sel") at $filename line $line\n);
    }
}
=================== end of source code of Selenium.pm ===================
了解了这个,也就比较容易写更复杂一些的测试语句了。



TAG: web Automation automation

 

评分:0

我来说两句

日历

« 2024-04-18  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 25073
  • 日志数: 37
  • 图片数: 1
  • 建立时间: 2008-05-01
  • 更新时间: 2008-10-22

RSS订阅

Open Toolbar