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.
<pre lang="php">$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.
<pre lang="php">$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.)
<pre lang="php">$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’.
<pre lang="php">$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.