Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 4c27108

Browse files
committed
Add functional tests for basic actions (click, double click etc.)
1 parent 944c76e commit 4c27108

File tree

4 files changed

+229
-1
lines changed

4 files changed

+229
-1
lines changed

lib/Interactions/WebDriverCompositeAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getNumberOfActions()
5151
}
5252

5353
/**
54-
* Perform the seqeunce of actions.
54+
* Perform the sequence of actions.
5555
*/
5656
public function perform()
5757
{
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace Facebook\WebDriver;
17+
18+
use Facebook\WebDriver\Remote\WebDriverBrowserType;
19+
20+
/**
21+
* @coversDefaultClass Facebook\WebDriver\Interactions\WebDriverActions
22+
*/
23+
class WebDriverActionsTest extends WebDriverTestCase
24+
{
25+
protected function setUp()
26+
{
27+
parent::setUp();
28+
29+
$this->driver->get($this->getTestPageUrl('events.html'));
30+
}
31+
32+
/**
33+
* @covers ::__construct
34+
* @covers ::click
35+
* @covers ::perform
36+
*/
37+
public function testShouldClickOnElement()
38+
{
39+
if ($this->desiredCapabilities->getBrowserName() === WebDriverBrowserType::HTMLUNIT) {
40+
$this->markTestSkipped('Not supported by HtmlUnit browser');
41+
}
42+
43+
$element = $this->driver->findElement(WebDriverBy::id('item-1'));
44+
45+
$this->driver->action()
46+
->click($element)
47+
->perform();
48+
49+
$this->assertSame(
50+
['mouseover item-1', 'mousedown item-1', 'mouseup item-1', 'click item-1'],
51+
$this->retrieveLoggedEvents()
52+
);
53+
}
54+
55+
/**
56+
* @covers ::__construct
57+
* @covers ::clickAndHold
58+
* @covers ::release
59+
* @covers ::perform
60+
*/
61+
public function testShouldClickAndHoldOnElementAndRelease()
62+
{
63+
if ($this->desiredCapabilities->getBrowserName() === WebDriverBrowserType::HTMLUNIT) {
64+
$this->markTestSkipped('Not supported by HtmlUnit browser');
65+
}
66+
67+
$element = $this->driver->findElement(WebDriverBy::id('item-1'));
68+
69+
$this->driver->action()
70+
->clickAndHold($element)
71+
->release()
72+
->perform();
73+
74+
$this->assertSame(
75+
['mouseover item-1', 'mousedown item-1', 'mouseup item-1', 'click item-1'],
76+
$this->retrieveLoggedEvents()
77+
);
78+
}
79+
80+
/**
81+
* @covers ::__construct
82+
* @covers ::contextClick
83+
* @covers ::perform
84+
*/
85+
public function testShouldContextClickOnElement()
86+
{
87+
if ($this->desiredCapabilities->getBrowserName() === WebDriverBrowserType::HTMLUNIT) {
88+
$this->markTestSkipped('Not supported by HtmlUnit browser');
89+
}
90+
91+
if ($this->desiredCapabilities->getBrowserName() === WebDriverBrowserType::MICROSOFT_EDGE) {
92+
$this->markTestSkipped('Getting stuck in EdgeDriver');
93+
}
94+
95+
$element = $this->driver->findElement(WebDriverBy::id('item-2'));
96+
97+
$this->driver->action()
98+
->contextClick($element)
99+
->perform();
100+
101+
$loggedEvents = $this->retrieveLoggedEvents();
102+
103+
$this->assertContains('mousedown item-2', $loggedEvents);
104+
$this->assertContains('mouseup item-2', $loggedEvents);
105+
$this->assertContains('contextmenu item-2', $loggedEvents);
106+
}
107+
108+
/**
109+
* @covers ::__construct
110+
* @covers ::doubleClick
111+
* @covers ::perform
112+
*/
113+
public function testShouldDoubleClickOnElement()
114+
{
115+
if ($this->desiredCapabilities->getBrowserName() === WebDriverBrowserType::HTMLUNIT) {
116+
$this->markTestSkipped('Not supported by HtmlUnit browser');
117+
}
118+
119+
$element = $this->driver->findElement(WebDriverBy::id('item-3'));
120+
121+
$this->driver->action()
122+
->doubleClick($element)
123+
->perform();
124+
125+
$this->assertSame(
126+
['mouseover item-3', 'mousedown item-3', 'mouseup item-3', 'click item-3', 'dblclick item-3'],
127+
$this->retrieveLoggedEvents()
128+
);
129+
}
130+
131+
/**
132+
* @return array
133+
*/
134+
private function retrieveLoggedEvents()
135+
{
136+
$logElement = $this->driver->findElement(WebDriverBy::id('log'));
137+
138+
return explode("\n", $logElement->getText());
139+
}
140+
}

tests/functional/web/events.html

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Events</title>
6+
<style>
7+
ul.active {
8+
background-color: red;
9+
font-style: italic;
10+
}
11+
</style>
12+
</head>
13+
<body>
14+
15+
<ul id="target"
16+
style="border: 1px solid black; height: 8em;"
17+
ondragover="handleDragover(event);"
18+
ondrop="handleDrop(event);"
19+
ondragenter="handleDragenter(event);"
20+
ondragleave="handleDragleave(event);"
21+
>
22+
23+
</ul>
24+
25+
<ul>
26+
<li id="item-1" draggable="true">First item</li>
27+
<li id="item-2" draggable="true">Second item</li>
28+
<li id="item-3" draggable="true">Third item</li>
29+
</ul>
30+
31+
<pre id="log" style="border: 1px solid black; height: 20em; overflow: auto;"></pre>
32+
33+
<script>
34+
function logEvent(event) {
35+
var logElement = document.getElementById('log');
36+
logElement.innerHTML += '<strong>' + event.type + '</strong> <span>' + event.target.id +'</span><br>';
37+
logElement.scrollTop = logElement.scrollHeight;
38+
console.log(event);
39+
}
40+
41+
function handleDragenter(e) {
42+
logEvent(e);
43+
e.target.classList.add('active');
44+
}
45+
46+
function handleDragleave(e) {
47+
logEvent(e);
48+
e.target.classList.remove('active');
49+
}
50+
51+
function handleDragover(e) {
52+
e.preventDefault();
53+
}
54+
55+
function handleDrop(e) {
56+
logEvent(e);
57+
e.preventDefault();
58+
59+
var data = e.dataTransfer.getData('text');
60+
e.target.appendChild(document.getElementById(data));
61+
e.target.classList.remove('active');
62+
}
63+
64+
var lis = document.querySelectorAll('ul > li'), el = null;
65+
for (var i = 0; i < lis.length; i++) {
66+
el = lis[i];
67+
68+
el.addEventListener(
69+
'dragstart',
70+
function (e) {
71+
logEvent(e);
72+
e.dataTransfer.setData('text/plain', e.target.id);
73+
}
74+
);
75+
76+
el.addEventListener('click', logEvent);
77+
el.addEventListener('contextmenu', logEvent);
78+
el.addEventListener('dblclick', logEvent);
79+
el.addEventListener('mouseover', logEvent);
80+
el.addEventListener('mousedown', logEvent);
81+
el.addEventListener('mouseup', logEvent);
82+
}
83+
</script>
84+
85+
</body>
86+
</html>

tests/functional/web/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ <h1 id='welcome'>Welcome to the facebook/php-webdriver testing page.</h1>
1818
<a href="slow_loading.html" id="a-slow-loading">Slow loading page</a>
1919
|
2020
<a href="alert.html" id="a-alerts">Javascript alerts</a>
21+
|
22+
<a href="events.html" id="a-drag">Events</a>
2123

2224
<p id="id_test">Test by ID</p>
2325
<p class="test_class">Test by Class</p>

0 commit comments

Comments
 (0)