Selenium webdriver PHP trying to execute a snippet of javascript

618 views
Skip to first unread message

Emily Isgar

unread,
Nov 13, 2011, 7:41:19 AM11/13/11
to seleniu...@googlegroups.com
Hi
I am currently writing a PHP Dashboard that enables users to record a
test via the Selenium IDE, import it into a PHP frameworked Dashboard
and then run those tests again (at any time).

I am trying to use the execute/runScript functions to inject
Javascript into the browser when running these tests via Selenium
Webdriver, however I cannot seem to get it to work.

This is the function I am trying to use in PHP Webdriver:
/**
     Inject a snippet of JavaScript into the page for execution in
the context of the currently selected frame.
    * The executed script is assumed to be synchronous and the result
of evaluating the script
    * is returned to the client.
    * @return Object result of evaluating the script is returned to
the client.
    */
   public function execute($script, $script_args) {
       $request = $this->requestURL . "/execute";
       $session = $this->curlInit($request);
       $args = array('script' => $script, 'args' => $script_args);
       $jsonData = json_encode($args);
       $this->preparePOST($session, $jsonData);
       $response = curl_exec($session);
       return $this->extractValueFromJsonResponse($response);
   }

However I know that Selenium IDE uses the runScript command.

Whenever I try to pass any Javascript commands over to this function I
just seem to get JAVA errors in the Selenium Log, and after spending a
lot of time searching Google and trying different things I only seem
to be able to find JAVA support for this functionality.

Please can anybody help?

Thank you
Emily
Sent from my iPhone

haotten

unread,
Nov 13, 2011, 8:53:16 PM11/13/11
to Selenium Users
Hi Emily, I am not a PHP coder, so I can't help you much here.  
However, a couple of thoughts.
I assume you are using WebDriver.execute($script,$script_args).
 According to the documentation, WebDriver creates an anonymous
javascript  function, and the $script_args variable is transformed
into a local array within that function called "arguments" and your
javascript script can use the "arguments" variable in the code.  NOTE:
The first argument can be accessed in javascript as "arguments[0]",
the second argument can be accessed as "arguments[1]", etc…
Also, according to the documentation, you can pass in as arguments any
primitive data type (int, string, float, etc…) and , most importantly,
you can pass in a WebElement object.  When a WebElement object is
passed in, it is transformed into the corresponding DOM element.
So, even though the "WebElement" object does not have a "setAttribute"
method, once the WebElement has been converted into a DOM element  the
javascript engine does have a "setAttribute" method.
So, in essence, if I tell WebDriver to execute the following
javascipt : "arguments[0].setAttribute(arguments[1],arguments[2]);"
 and I pass in as arguments a WebElement that I previously found (by
using WebDriver.findElement) and two strings, the Javascript will
properly set the attribute on the DOM element.
Hope that helps.  If not, send the errors you keep getting and the
script you are passing in.
Reply all
Reply to author
Forward
0 new messages