Chaoui05 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody ! I just have this simple question . I would like to do some tests with Selenium using Perl . And i would like to have in one Perl file 2 or more differents functions to test my page (webdrivers). To call them one after one. I know it's possible with others automate framework test like with Dalekjs using Javascript. How could i do it in Selenium for Perl ?

Thanks !!
  • Comment on How to have 2 functions in one Perl file

Replies are listed 'Best First'.
Re: How to have 2 functions in one Perl file
by SimonPratt (Friar) on Apr 18, 2016 at 11:50 UTC

    Without knowing anything about how your code is laid out, or the type of testing you want to do, I would suggest starting with Test::Simple and if that isn't enough, then Test::More should see you right.

      I would to do something like that for example, with 2 functions also. Do you think it's possible and how ? Thanks

      use Selenium::Remote::Driver; use Test::More tests=>4; sub run_tests{ my $browser = shift; my $driver = Selenium::Remote::Driver->new( browser_name => $browser +); $driver->get("http://www.google.com"); $driver->find_element('q','name')->send_keys("Hello WebDriver!"); ok($driver->get_title =~ /Google/,"title matches google"); is($driver->get_title,'Google',"Title is google"); ok($driver->get_title eq 'Google','Title equals google'); like($driver->get_title,qr/Google/,"Title matches google"); $driver->quit(); } run_tests ($_) for sort ( 'chrome', 'internet explorer', 'phantomJS', +'firefox' ); sub run_testing{ my $browser = shift; my $driver = Selenium::Remote::Driver->new( browser_name => $browser +); $driver->get("http://www.google.com"); $driver->pause(2000); $driver->set_window_size(640, 480); $driver->find_element('login','name') $driver->find_element('password','name') $driver->quit(); } run_testing ($_) for sort ( 'chrome', 'internet explorer', 'phantomJS' +, 'firefox' );

        Hmm, OK I think I see what you're trying to do. Try something like this, from Test::Class:

        use strict; package MyTestingSuite; use parent 'Test::Class'; use Selenium::Remote::Driver; use Test::More; sub startup : Test( startup => no_plan ) { my $self = shift; $self->{browsers} = [('chrome', 'internet explorer', 'phantomJS', 'f +irefox')]; } sub title_test : Tests { my $self = shift; foreach my $browser (@{$self->{browsers}}) { my $driver = Selenium::Remote::Driver->new( browser_name => $brows +er); $driver->get("http://www.google.com"); $driver->find_element('q','name')->send_keys("Hello WebDriver!"); ok($driver->get_title =~ /Google/,"title matches google"); is($driver->get_title,'Google',"Title is google"); ok($driver->get_title eq 'Google','Title equals google'); like($driver->get_title,qr/Google/,"Title matches google"); $driver->quit(); } } sub element_test : Tests { my $self = shift; foreach my $browser (@{$self->{browsers}}) { my $driver = Selenium::Remote::Driver->new( browser_name => $brows +er); $driver->get("http://www.google.com"); $driver->pause(2000); $driver->set_window_size(640, 480); $driver->find_element('login','name') $driver->find_element('password','name') $driver->quit(); } } 1;

        The relevant .t file might look like this:

        use strict; use warnings; use Test::Class::Load 'MyTestingSuite';

        Note: Sample code untested

        What is the problem you're facing? Do you get errors? Do the tests not run? Do you get unexpected results?