如何在单个Perl文件中为多个URL运行Selenium测试?

时间:2012-06-15 08:32:31

标签: perl selenium-rc

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        => "localhost",
    port        => 4444,
    browser     => "*firefox",
    browser_url => "http://10.201.3.192:8000/"
);
$sel->open_ok("/abc-dd/");
$sel->window_maximize();
$sel->$sel->$sel->$sel->_ok();
$sel->close_ok();

以上代码适用于单个网址。我需要在另一个URL上执行相同的操作。以下代码仅完成了第一部分的执行。

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        => "localhost",
    port        => 4444,
    browser     => "*firefox",
    browser_url => "http://10.201.3.192:8000/"
);
$sel->open_ok("/abc-dd/");
$sel->window_maximize();
$sel->$sel->$sel->$sel->_ok();
$sel->close_ok();
$sel->open_ok("http://10.201.3.195:8000/abc-dd/");
$sel->window_maximize();
$sel->$sel->$sel->$sel->_ok();
$sel->close_ok();

我必须在一个Perl文件中对两个URL执行操作:

http://10.201.3.192:8000/abc-dd/
http://10.201.3.195:8000/abc-dd/

如何附加第二个网址?

2 个答案:

答案 0 :(得分:1)

您可以创建一个网址数组并循环显示它

my @urls = qw (http://10.201.3.192:8000 
               http://10.201.3.195:8000); # create list of urls

for my $url (@urls) {

    my $sel = Test::WWW::Selenium->new( host => "localhost", 
                                    port => 4444, 
                                    browser => "*firefox", 
                                    browser_url => $url );

    $sel->open_ok("/abc-dd/");
    $sel->
    $sel->
    ...
}

答案 1 :(得分:1)

将您的逻辑打包到函数中以便重用:

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

sub test_url {
    my $sel = Test::WWW::Selenium->new( host => "localhost", 
                                        port => 4444, 
                                        browser => "*firefox", 
                                        browser_url => "http://10.201.3.192:8000/" );

    $sel->open_ok("/abc-dd/");
    $sel->window_maximize();
    $sel->
    $sel->
    $sel->
    $sel->_ok();
    $sel->close_ok();
}

test_url("/abc-dd/");
test_url("http://10.201.3.195:8000/abc-dd/");