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

Hey guys ! I am a beginner in Perl ( yes it can exist ) and i would to create a function which switch between different browser to test a code , instructions). I did that but it doesn't work ?!

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' );

Replies are listed 'Best First'.
Re: shift method
by Discipulus (Canon) on Apr 18, 2016 at 08:39 UTC
    welcome to the monastery Chaoui05 and welcome to the wonderful world of Perl

    Yes shift is intended to be used in this way.

    Anyway if you have doubts, as i frequently have, add a firendly print statement as basic debug tool:

    sub run_tests{ my $browser = shift; print "The sub 'run_test' received [$browser] as parameter\n";

    if your program handles also other parameter maybe you can have a debug one so you can fill your programs of stuff like:

    print "The sub 'run_test' received [$browser] as parameter\n" if + $debug;

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Thanks !

Re: shift method
by choroba (Cardinal) on Apr 18, 2016 at 08:56 UTC
    There are many more tests than 4, in fact, there are 4 times the number of browsers. You can use
    my @browsers = sort ( 'chrome', 'internet explorer', 'phantomJS', 'fir +efox' ); run_tests($_) for @browsers; done_testing(4 * @browsers);

    instead of planning the number of tests if the number of browsers might change.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      I am sorry but it doesn't work . I did this :

      use Selenium::Remote::Driver; use Test::More tests=>4; sub run_tests{ my $browser = shift; my $driver = Selenium::Remote::Driver->new( browser_name => $browser +); my @browsers = sort ( 'chrome', 'internet explorer', 'phantomJS', 'f +ir +efox' ); $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 @browsers; done_testing(4 * @browsers);

        Look at the example code again. The array of browsers is defined outside of run_tests(), not inside.

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      Thanks . And how i do with

       my $browser = shift;

      ?

      Thank you