Sending characters at an element with WebDriver

One of the downsides of a Selenium binding that implements the wire protocol literally is, well, it implements the wire protocol literally. Take for instance sending text to an element using the Facebook bindings.

$e1 = $this->session->element("id", "some id");
$e1->value(array("value" => array("pumpkins")));

Yuck. Nested arrays? In user-space? Well, it should be in the Page Object, but still gross. Taking a cue from Python I have made it much for palatable in my fork of those bindings.

$e2 = $this->session->element("id", "some id");
$e2->sendKeys("turtles");

Sending non-character keys is a little more challenging and required lifting some code from the chibimagic bindings. (As a side note, if you release code, put a license in/around it. If not, I’ll pretend that there is one that I want there.)

$e3 = $this->session->element("id", "some id");
$e3->sendKeys(PHPWebDriver_WebDriverKeys::SpaceKey());

The list special key methods is in the WebDriverKeys.php file.

With the ability to send keys, like space, we can start to simulate things like ‘pressing the space bar scrolls the page and causes a fly-in advertisement to display’.

$e4 = $this->session->element("tag name", "body");
$e4->sendKeys(PHPWebDriver_WebDriverKeys::SpaceKey());
$e5 = $this->session->elements("id", "fly-in");
if (count($5) == 1) {
  $this->assert($e5[0]->visible());
} else {
  $this->fail("Should only be one ad")
}

This shows two WebDriver idioms.

  • To send an event at ‘the page’, you get the body tag and use that element
  • To determine whether or not an element exists on the page you do not do find_element, you do find_elements (or your binding’s equivalents) and check the length of the returned list. Empty list? Element is not there.

Comments 1

  1. Ross Patterson wrote:

    (As a side note, if someone publishes code, it is copyrighted regardless of whether they choose to offer you a license to use it. Which means if they don’t specify a licenses, they have the sole right to make or allow derivative works, and you need to seek their explicit permission.)

    Posted 27 Jan 2012 at 12:37 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *