0

I am using the Perl Selenium package, WWW::Selenium.
Trying to resize the browser window, I am getting a mysterious JavaScript error:
"Threw an exception: missing ; before statement".

Here is the code:

use strict;
use warnings;
use 5.014;
use autodie; 
use warnings qw< FATAL utf8 >;
use Carp;
use Carp::Always;
use WWW::Selenium;

my $url = 'http://www.google.com'; #for example
my $sel = WWW::Selenium->new( host => 'localhost',
   port => 4444,
   browser => '*firefox F:\WIN 7 programs\Web & Internet\Firefox 8 bit\firefox.exe',
   browser_url => $url,
   );
$sel->open( $url );
$sel->wait_for_page_to_load(10000);
my $res = $sel->window_maximize();  # So far, this works fine
$res = $sel->get_eval( q{ WebDriver driver = ((WebDriverBackedSelenium) selenium).getWrappedDriver();
   driver.manage().window().setSize(1040,720);} );
   # (Following this: http://stackoverflow.com/questions/1522252/, Eli Colner's post)

The program then crashes here with:

"Threw an exception: missing ; before statement"

If I drop the first JavaScript line and just leave in the 2nd line, namely:

$res = $sel->get_eval( q{driver.manage().window().setSize(1040,720);} );

It bumps with: "driver not defined".

Help will be appreciated - Thanks in advance

Helen

Note: cross posted here: http://www.perlmonks.org/?node_id=1092355

Miller
  • 34,962
  • 4
  • 39
  • 60
Helen Craigman
  • 1,443
  • 3
  • 16
  • 25
  • 1
    You can certainly write it in one line: `((WebDriverBackedSelenium) selenium).getWrappedDriver().manage().window().setSize(1040,720);` – Matthias Jul 05 '14 at 11:31
  • @Matthias: when replacing it with your suggestion, it produces the error: `missing ) in parenthetical` This is typically a javascript error **not** pointing to a missing parentheses, but instead to invalid javascript, for some reason (sometimes: bad string representation) (see Slaks' comment here: [http://stackoverflow.com/questions/18242512)]. This leads me to think that somehow, the Perl WWW::Selenium module is not passing on right the javascript string to the Selenium server. – Helen Craigman Jul 05 '14 at 16:14
  • See here, too: [http://geekandpirate.wordpress.com/2010/12/11/missing-in-parenthetical/] – Helen Craigman Jul 05 '14 at 16:28

1 Answers1

2

I see invalid javascript in your code, you made a mistaken assumption. Regarding the referenced SO thread that you base your code on:

How to resize/maximize Firefox window during launching Selenium Remote Control?

what makes you think Eli Corner's answer/solution is "javascript"? That is Java, or C# otherwise, because only those language bindings for WebDriver (or Selenium 2) expose a WebDriverBackedSelenium feature. All other language bindings, including Perl have no such option. So even if the code syntax is correct, on execution it will fail because that's not javascript (or shall I say the referenced classes/objects are not javascript).

Your options for a solution the way I see it are:

  1. use real javascript code and Dave Hunt's solution (in that same SO thread) ideally should work, adapted for Perl:

    $sel->get_eval("window.resizeTo(1024, 768); window.moveTo(0,0);");

  2. use Perl WebDriver binding to correctly use Eli Corner's solution (adapted for Perl), not Selenium (RC) binding that you are currently using. Perl WebDriver binding is Selenium::Remote::Driver, not WWW:Selenium. You should then be able to do something like this (there is no need for the WebDriverBackedSelenium part in Perl, but it does mean you have to switch off using Selenium RC moving to WebDriver, there's no backward compatibility support, you need Java or C# for that):

    $driver->set_window_position(0, 0);

    $driver->set_window_size(640, 480);

Community
  • 1
  • 1
David
  • 3,223
  • 3
  • 29
  • 41