Passing Parameter to Test::Class Setup method

2019-09-18 23:54发布

I need to invoke a browser in selenium dynamically.

To achieve this I need to send the browser name as parameter to the set-up or start-up methods in Test::Class. How do I achieve this?

2条回答
再贱就再见
2楼-- · 2019-09-19 00:02

I take it you want to get a browser, then reuse it for some tests, then destroy it later? So just use a global to hold the browser you create. For example:

my $browser = '';
sub b_connect : Test(startup) {
   $browser = WWW::Selenium->new( host => "localhost",
                              port => 4444,
                              browser => "*iexplore",
                              browser_url => "http://www.google.com",
                            );
};

sub b_disconnect : Test(shutdown) {
  $browser->close()
};

Just use the $browser var in you tests.

查看更多
唯我独甜
3楼-- · 2019-09-19 00:22
sub startup : Test( startup ) {

    my ($self) = @_;
    my $arg = shift;

     $self->{browser_type} = $arg->{browser};

    -------------------------------#some other code for myself

    $self->{browser} =
        Test::WWW::Selenium->new(
            host        => $self->{host},
            port        => $self->{port},
            browser     => $self->{browser_type},
            browser_url => $self->{test_url},
    );

In my test script I need it to call using the following

my $t1 =  Test::Class::Selenium::TestCases->new(browser=>$browser,);
Test::Class->runtests($t1);
查看更多
登录 后发表回答