in reply to Re: How to have 2 functions in one Perl file
in thread How to have 2 functions in one Perl file

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

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

    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

      Visually interesting but it doesn't work for me

        Whats not working?

        After I fixed the syntax errors in your code and installed Selenium::Remote::Driver the test suite worked very well for me. The Selenium code in the test suite is broken, but thats kind of beside the point.

      It's ok sorry. I just forget something. Perfect Thanks ! Can i ask u another and last question ?

        Sure

Re^3: How to have 2 functions in one Perl file
by stevieb (Canon) on Apr 18, 2016 at 14:19 UTC

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

      i have anything right now with my shell . Using @SImonPratt code. It doesn't run and when i launch my file , output in Shell is simply the path of my directory. Nothing happens.