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

Scenario : there is a java class as given below :
public class TestSelenium { public void googleTest() throws Exception { WebDriver driver = new InternetExplorerDriver(); driver.get("http://www.google.com/webhp?complete=1&hl=en"); } }
There is a perl program which makes use of Inline::Java module to call the googleTest of java class written above. Perl program looks like.
use warnings; use Selenium::Remote::Driver; use Inline Java => 'STUDY', CLASSPATH => 'C:\selenium\selenium-java-2.37.0\selenium-2.37.0\libs\se +lenium-java-2.37.0.jar;C:\selenium\SeleniumTestPoc\bin\MyJar.jar;C:\s +elenium\selenium-java-2.37.0\selenium-2.37.0\libs\selenium-server-sta +ndalone-2.37.0.jar', STUDY => ['TestSelenium']; $test= TestSelenium->new; $test->googleTest;
Now the above Perl code will open IExplorer and go to google.com page. In my Perl program further to $test->googleTest; I want to make use of same browser that was opened by java(WebDriver driver = new InternetExplorerDriver();) and perform a search for text "Cheese".

Question is, can the object of WebDriver class("driver" in this case) be further used in my Perl program so that I can use same browser and perform different UI operations on it in Perl?

Replies are listed 'Best First'.
Re: Make use of a java object in Perl
by jeffa (Bishop) on Mar 31, 2015 at 21:33 UTC

    Why not just write this in Perl?

    use strict; use warnings; use HTTP::WebTest; my $webtest = HTTP::WebTest->new; $webtest->run_tests([ { test_name => 'obtain google cheese', url => 'http://www.google.com/webhp?complete=1&hl=en' +, text_require => [ 'Cheese' ], }, { test_name => 'something that makes sense', url => 'http://www.google.com', text_require => [ 'Google' ], }, ]);

    You want cheese? We can get you cheese. No Java needed.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Make use of a java object in Perl
by GotToBTru (Prior) on Mar 31, 2015 at 19:22 UTC

    As written, WebDriver is not accessible outside of the TestSelenium class. Making it so is a Java problem, not Perl.

    Dum Spiro Spero
Re: Make use of a java object in Perl
by choroba (Cardinal) on Mar 31, 2015 at 22:19 UTC
    Crossposted at StackOverflow. It's considered polite to inform about crossposting so people not attending both sites don't waste their time hacking a solution for a problem already solved at the other corner of the internet.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Make use of a java object in Perl
by locked_user sundialsvc4 (Abbot) on Mar 31, 2015 at 20:52 UTC

    Different worlds . . . all that the Perl code can do is to invoke the Java routines as-presented.   It cannot dip into Java’s world and start manipulating its variables.   You will need first to address this purely on the Java side:   “can Java manipulate the browser-instance in this way?”   Only then can Perl, through Selenium, issue a request to the Java routine to do it.   (And, even then, only if the whole thing makes sense given the strictures of the stateless HTTP protocol, which is most unlikely.)

      Have you ever used Inline::Java? Because it can do exactly what you assert it can’t. Namely create, access, extend Java as directly as one likes or has the stomach to map.

      If you have no experience in a problem domain, offering advice is hurtful as often as it helpful.

        As a matter of fact, yes.   But only briefly, because it was much more expedient to simply write the whole thing in the native language.   Here’s the relevant quote – and the problem – taken directly from the Perl web-page:

        Then Perl asks the Java classes what public methods have been defined.   These classes and methods are available to the Perl program as if they had been written in Perl.
        . . . but the phrase, “as if they had been written in Perl,” is somewhat misleading.   Perl has transparent access to Java’s foreign-function interfaces, but is no more privileged in that regard as would be any other client using a similar interface.

        The two interpreter environments are running in tandem, using (in this case) Java’s public interfaces.   Those interfaces do not expose whatever is private, and they (of course, necessarily) do not have Perl-VM “doing” anything to, or directly with, the Java-VM’s environment, because languages never expose interfaces that would de-stabilize or compromise their own integrity, even in the name of testing.   As I read the OP, the latter is along those lines:   “you, Java, having created this var, let me, Perl, do with it.”   Your capacity to do this is, as I read it, too limited to support the requested scenario.

        Java has testing frameworks comparable to, and in some ways superior to, those offered by Perl.   Therefore, if it were me, I would cleanly divide this testing scenario into three non-intersecting parts:   (1) a test of Perl by Perl; (2) a test of Java by Java; and (3) a test of the interface between them, specifically where those interfaces are meant to be “public interfaces offered by this Java app for the consumption by others.”

        Although it may not seem so, and although I do not always explain myself too well, I don’t make comments here merely to see my handle showing up on a web page.   If I am mistaken, simply correct me.