Skip to content

Example command reference

ir4georgia edited this page Oct 17, 2013 · 62 revisions

Example command reference

Overview

Here is my attempt at helping others learn to use this binding. Others out there who are more experienced with this PHP binding, please add to this page as you get to it please.

Working with elements

NOTE: some examples below may be done shorthand, in single line chained calls from the session object, though if we wanted to perform several actions on the same element, we could save the WebDriverElement into a variable and then make subsequent calls from that element.

Getting text of an element

$result = $driver->findElement(WebDriverBy::id('signin'))->getText();

Clicking an element (link, checkbox, etc.)

//POST w/ empty data to click command.  using just click() may work for you too.
$driver->findElement(WebDriverBy::id('signin'))->click("");

Typing into text field

$driver->findElement(WebDriverBy::id("element id"))->sendKeys("I want to send this");

How to clear the existing value in text field

If a text field already has a non-empty value and you want to update that field, here's how to clear the existing value:

$driver->findElement(WebDriverBy::id("element id"))->clear();

Alerts, confirmations and prompts

Alerts

$driver->switchTo()->alert()->dismiss(); // dismiss
$driver->switchTo()->alert()->getText(); // get alert text, works for confirmations and prompts

Confirmations

$driver->switchTo()->alert()->accept(); // accept
$driver->switchTo()->alert()->dismiss(); // dismiss

Prompts

$driver->switchTo()->alert()->sendKeys('Test'); // enter text
$driver->switchTo()->alert()->accept(); // submit

$driver->switchTo()->alert()->dismiss(); // dismiss

Windows

Maximize the browser window

$driver->manage()->window()->maximum();

[IMPORTANT] Everything under this line is not up-to-dated

Sending native keypress OR pressing non-text key

TBD - how to do this? Can refer to JSONWireProtocol, but how to send the keycode?

Get attribute of an element

$attr = $session->element('id','signin')->attribute('maxlength');

Get value of an input element

// no direct method to call like $element->value(); since that is not in the protocol
$element->attribute("value");

Take a screenshot

$imgData = base64_decode($session->screenshot());
file_put_contents('screenshot.png', $imgData);

Drag And Drop

Drag and Drop from one element to another

//this requires a sequence of steps as follows:
//1st find the source and target/destination elements to use as reference to do the drag and drop
$from = $session->element('xpath',"//div[@class='here']");
$to = $session->element('id','there');
//now perform drag and drop
$session->moveto(array('element' => $from->getID())); //move to source location, using reference to source element
$session->buttondown(""); //click mouse to start drag, defaults to left mouse button
$session->moveto(array('element' => $to->getID())); //move to target location, using reference to target element
$session->buttonup(""); //release mouse to complete drag and drop operation
//it may be worthwhile to encapsulate these steps into a function called draganddrop($src,$target), etc.

Executing JavaScript

Synchronous script execution

Remember that the script you run is implicitly inserted into an anonymous javascript function. That means if you want to access global variables, you have to use the full name of the variable, for example window.document

$sScriptResult = $session->execute(array(
  'script' => 'return window.document.location.hostname',
  'args' => array(),
));

$sScriptResult now holds the value of the current document hostname

Asynchronous script execution

Make sure you tell the server how long it should wait before it gives up on your script and throws a timeout exception.

// wait at most 5 seconds before giving up with a timeout exception
$session->timeouts()->async_script(array('ms'=>5000));

Similar to synchronous script, the async script is wrapped in an anonymous function.

Simple example

$sResult = $session->execute_async(array(
  'script' => 'arguments[arguments.length-1]("done");',
  'args' => array(),
));

$sResult == "done"

More complex example

In the example below, we poll the global window.MY_STUFF_DONE value at regular intervals, waiting for it to exist with a non-false value. Once we see it, we return back to the calling php-webdriver code with the value "done".

// define the javascript code to execute. This just checks at a periodic
// interval to see if your page created the window.MY_STUFF_DONE variable
$sJavascript = <<<END_JAVASCRIPT

var callback = arguments[arguments.length-1], // webdriver async script callback
    nIntervalId; // setInterval id to stop polling

function checkDone() {
  if( window.MY_STUFF_DONE ) {
    window.clearInterval(nIntervalId); // stop polling
    callback("done"); // return "done" to PHP code
  }
}

nIntervalId = window.setInterval( checkDone, 50 ); // start polling
END_JAVASCRIPT;

$sResult = $session->execute_async(array(
  'script' => $sJavascript,
  'args' => array(),
));

$sResult == "done"

Window handling

Getting current window handle

$handle = $session->window_handle();

Getting all window handles as array

$handles = $session->window_handles(); //now can iterate through array to get desired handle

Switching to window

See project home page or the readme file for examples

Closing a window

See project home page or the readme file for examples

Switching frames

// Open URL
$session->open('http://www.facebook.com/cocacola');

// Grab hold of the ID of your iframe
$id = $session->element('xpath', "//div[@id='pagelet_app_runner']//iframe")->attribute('id');

// Switch frames using that ID
$session->frame(array('id'=>$id));

// Output the HTML so we can see the right frame was selected
echo $session->source();

// Do element queries as normal
var_dump($session->element('id', "bottom]")->displayed());

Switch Frame With the updated bindings

    //Find an iframe
    $iframe = $session->findElement(WebDriverBy::tagName('iframe));

    //Get the iframe id
    $frameId = $iframe->getAttribute('id');

    //Switch the driver to the iframe
    $session->switchTo()->frame($frameId);
    
    <Do whatever you needed to do in the i-frame (click a link, for me)>

    //Go back to the main document
    $session->switchTo()->defaultContent();