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

Hello everybody ! Iam beginner with Perl. I would like to create one function to have all my code inside. And i would like to call it with 2 "same" calls. I did that but it doesn't work:
my $item; my $root = "'//a[@href="/"]'; my $name; sub myHomeTest { $elem = $driver->find_element('//a[@href="/"]'); # Other way to get + Xpath: $elem = $driver->find_element("//a[\@href='/']"); $driver->mouse_move_to_location(element => $elem); # xoffset => +x, yoffset => y $driver->click_element_ok('//a[@href="/license/view"]', 'xpath', + @name); $driver->pause(3000); $driver->capture_screenshot("$path/snapLicenseview-$browser.png" +); $driver->get_ok("$base_url","get Home url"); $driver->pause(2000); #return OneHome(); } myHomeTest (first var, second var,third,var); myHomeTest (first var, second var,third,var);
And my code is following:
$elem = $driver->find_element('//a[@href="/"]'); $driver->mouse_move_to_location(element => $elem); # xoffset => x, + yoffset => y $driver->click_element_ok('//a[@href="/license/view"]', 'xpath', ' +License View loaded'); $driver->pause(2000); $driver->capture_screenshot("$path/snapLicenseview-$browser.png"); $driver->get_ok("$base_url","Get Home url"); $driver->pause(2000); $elem = $driver->find_element('//a[@href="/"]'); $driver->mouse_move_to_location(element => $elem); # xoffset => x, + yoffset => y $driver->click_element_ok('//a[@href="/licenses"]', 'xpath', 'Lice +nses loaded'); $driver->pause(2000); $driver->capture_screenshot("$path/snapLicenses-$browser.png"); $driver->get_ok("$base_url", "get Home url"); $driver->pause(2000);
How can i do ? Thanks !

Replies are listed 'Best First'.
Re: One function with 2 calls
by Discipulus (Canon) on May 10, 2016 at 08:11 UTC
    hello, just a consideration (because i'm not sure to understand your issue):

    are you parsing something? if so maybe the second call to the sub has not the possibility to go backward to match.

    Due surely to my ignorance i dunno what your $driver is: probably Selenium::Remote::Driver but i'm guessing.

    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.
      Sorry , yes iam using Selenium with Perl. I change my title !
Re: One function with 2 calls
by FloydATC (Deacon) on May 10, 2016 at 08:49 UTC
    Hmm. Aside from the fact that I don't fully understand what you're trying to do, there are a few issues with the code:

    You'll have to look carefully at this line and see what exactly you want the string to contain because I can't tell and Perl will complain that it Can't find string terminator '"' anywhere before EOF:
    my $root = "'//a[@href="/"]';

    Next, you're calling myHomeTest() with dummy arguments (plain words) but that sub doesn't actually seem to use arguments at any point unless there's some magic in the $driver object that I can't see.

    Finally, $driver is not defined anywhere so it would just throw Can't call method "find_element" on an undefined value as it is.

    When you say it doesn't work, do you mean you get error messages that you can't figure out or does the code just not do what you expect it to?

    -- FloydATC

    Time flies when you don't know what you're doing

      Sorry, i am using Selenium ! I will write it in title
        Now i did this :
        my $root ='//a[@href="/"]'; my @name = ('//a[@href="/install/kit"]','//a[@href="/license/view" +); my @snapname = ("Installkit","Licenseview"); sub myHomeTest { my ( $RefName, $RefSnapname ) = @_; my $elem = $driver->find_element($root); # Other way to get Xpa +th: $elem = $driver->find_element("//a[\@href='/']"); $driver->mouse_move_to_location(element => $elem); # xoffset => +x, yoffset => y foreach my $name ( @{RefName}){ $driver->click_element_ok($name, 'xpath', "Loaded"); } $driver->pause(3000); foreach my $name ( @{RefSnapname}){ $driver->capture_screenshot("$path/snap$snapname-$browser.png"); } $driver->get_ok("$base_url","get Home url"); $driver->pause(2000); } myHomeTest ($root, $name, $snapname);
        And i have following issue, STDOUT in my Shell:

        Variable "$elem" will not stay shared at MyTestingSuite.pm line 169.

        Variable "$driver" will not stay shared at MyTestingSuite.pm line 169.

        Variable "$root" will not stay shared at MyTestingSuite.pm line 169.

        Variable "$browser" will not stay shared at MyTestingSuite.pm line 176.

        Global symbol "@RefName" requires explicit package name at MyTestingSuite.pm line 171.

        Global symbol "@RefSnapname" requires explicit package name at MyTestingSuite.pm line 175

        Global symbol "$snapname" requires explicit package name at MyTestingSuite.pm line 176.

        Global symbol "$name" requires explicit package name at MyTestingSuite.pm line 182.

        Global symbol "$snapname" requires explicit package name at MyTestingSuite.pm line 182.

Re: One function with 2 calls with Selenium
by Laurent_R (Canon) on May 10, 2016 at 09:33 UTC
      O My apologizes ! I forgot i asked question. But right now it's a bit different. I didn't do anything since this time. Iam working on it right now.