PHP-WebDriver and Touch events
When I was making sure that PHP-WebDriver was going to work with Android for Sauce’s Android Sauce announcement I looked for something other than ‘yes, it works’ to be able to showcase with it. And luckily for me, there is a sub-API of WebDriver called Touch which lent itself well to this task.
Now Facebooks’s [now unofficially abandoned] project did have support for Touch events already.
<pre lang="php">$e = $this->session->element("id", "normal");
$this->session->touch()->click(array('element' => $e->getID()));
That’s not really pretty (but I’m starting to wonder what is…). But again, it is close to the Wire Protocol — why click and not touch though?
What I did add however was PHPWebDriver_WebDriverTouchActions which allows you to chain a series of these touch events together, and then execute them as one sequence. This chain illustrates all the touch actions available to you in one ridiculous sequence that doesn’t really do anything.
<pre lang="php">$e = $this->session->element("id", "normal");
$a = new \PHPWebDriver_WebDriverTouchActions($this->session);
$a = $a->single_tap($e);
$a = $a->down(1000, 50);
$a = $a->move(-1000, 0);
$a = $a->up(-2000, 50);
$a = $a->element_scroll($e, -2000, 50);
$a = $a->scroll(-2000, 50);
$a = $a->double_tap($e);
$a = $a->long_tap($e);
$a = $a->flick(40, 400);
$a = $a->element_flick($e, 40, 400, \PHPWebDriver_WebDriverSession::FLICK_SPEED_NORMAL);
$a->perform();
Because I have yet to figure out how to get PHP to generate docs automatically, here is what each of these are and their arguments
- single_tap – like a click
- element to tap
- down – finger down, but don’t move
- x coordinate
- y coordinate
- move – move finger from where it is, to some other place
- x coordinate
- y coordinate
- up – lift finger up (these arguments seem redundant to me…)
- x coordinate
- y coordinate
- element_scroll – scrolling
- element to start scrolling from
- x offset in pixels to scroll
- y offset in pixels to scroll
- scroll – more scrolling, but not really on anywhere in particular
- x offset in pixels to scroll
- y offset in pixels to scroll
- double_tap – like a double click
- element to double tap
And no, I can’t think about ‘double tap’ without thinking about ‘rule number 2’
- long_tap – not really sure what the definition of ‘long’ is… I would likely just do a down, perform, sleep, up, perform combination
- element to long tap
- flick – I have no idea what this means…
- The x speed in pixels per second
- The y speed in pixels per second
- element_flick – This makes sense
- element where the flick starts
- The x offset in pixels to flick by
- The y offset in pixels to flick by
- The speed; either PHPWebDriver_WebDriverSession::FLICK_SPEED_NORMAL or PHPWebDriver_WebDriverSession::FLICK_SPEED_FAST
As for how to use these chains, as the code snippet implies, you need to…
- Create a new PHPWebDriver_WebDriverTouchActions object
- Add events to it
- Tell the object to perform() them