1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk/) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDev/ for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2018 Webino, s. r. o. (http://webino.sk/) |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WebinoDev\Test\Selenium; |
11
|
|
|
|
12
|
|
|
use PHPWebDriver_WebDriverElement as WebDriverElement; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait InputTrait |
16
|
|
|
*/ |
17
|
|
|
trait InputTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Return select input object |
21
|
|
|
* |
22
|
|
|
* @param string $name |
23
|
|
|
* @return WebDriver\Select |
24
|
|
|
*/ |
25
|
|
|
protected function getSelectInput($name) |
26
|
|
|
{ |
27
|
|
|
return new WebDriver\Select($this->elementByName($name)); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $name |
32
|
|
|
* @return WebDriver\Select |
33
|
|
|
* @deprecated use getSelectInput() instead |
34
|
|
|
* @todo remove, deprecated |
35
|
|
|
*/ |
36
|
|
|
protected function getSelect($name) |
37
|
|
|
{ |
38
|
|
|
return $this->getSelectInput($name); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Enters the input value |
43
|
|
|
* |
44
|
|
|
* @param string|WebDriverElement $name |
45
|
|
|
* @param string $value |
46
|
|
|
* @param callable|false|null $callback |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
protected function enterInput($name, $value, $callback = null) |
50
|
|
|
{ |
51
|
|
|
$resolveElm = function () use ($name) { |
52
|
|
|
if ($name instanceof WebDriver\ElementInterface |
53
|
|
|
|| $name instanceof WebDriverElement |
|
|
|
|
54
|
|
|
) { |
55
|
|
|
return $name; |
56
|
|
|
} |
57
|
|
|
return $this->elementByName($name); |
|
|
|
|
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
/** @var WebDriverElement|WebDriver\ElementInterface $elm */ |
61
|
|
|
$elm = $resolveElm(); |
62
|
|
|
if (null === $callback) { |
63
|
|
|
$this->sleep(1); |
|
|
|
|
64
|
|
|
$elm->clear(); |
65
|
|
|
$elm->sendKeys(''); |
66
|
|
|
$this->sleep(1); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
$elm->clear(); |
69
|
|
|
$elm->sendKeys($value); |
70
|
|
|
is_callable($callback) and call_user_func($callback, $elm); |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Submit the input value |
76
|
|
|
* |
77
|
|
|
* @param string|WebDriverElement $name |
78
|
|
|
* @param string $value |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
protected function submitInput($name, $value) |
82
|
|
|
{ |
83
|
|
|
$this->enterInput($name, $value, function ($elm) { |
84
|
|
|
/** @var $elm WebDriver\ElementInterface */ |
85
|
|
|
$elm->submit(); |
86
|
|
|
}); |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Assert that input value is same than expected |
92
|
|
|
* |
93
|
|
|
* @param string|WebDriverElement|WebDriver\ElementInterface $name |
94
|
|
|
* @param string $expectedValue |
95
|
|
|
* @param callable $callback |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function assertInput($name, $expectedValue, callable $callback = null) |
99
|
|
|
{ |
100
|
|
|
$elm = ($name instanceof WebDriverElement) ? $name : $this->elementByName($name); |
|
|
|
|
101
|
|
|
$this->assertSame($expectedValue, $elm->attribute('value')); |
|
|
|
|
102
|
|
|
$callback and call_user_func($callback, $elm); |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.