1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Navigators; |
4
|
|
|
|
5
|
|
|
use Facebook\WebDriver\Exception\ElementNotVisibleException; |
6
|
|
|
use Facebook\WebDriver\WebDriverBy; |
7
|
|
|
use Magium\AbstractTestCase; |
8
|
|
|
use Magium\Actions\WaitForPageLoaded; |
9
|
|
|
use Magium\InvalidConfigurationException; |
10
|
|
|
use Magium\WebDriver\ExpectedCondition; |
11
|
|
|
use Magium\WebDriver\WebDriver; |
12
|
|
|
|
13
|
|
|
class InstructionNavigator implements NavigatorInterface |
14
|
|
|
{ |
15
|
|
|
const NAVIGATOR = 'InstructionNavigator'; |
16
|
|
|
|
17
|
|
|
const INSTRUCTION_MOUSE_CLICK = 'mouseClick'; |
18
|
|
|
const INSTRUCTION_MOUSE_MOVETO = 'mouseMoveTo'; |
19
|
|
|
const INSTRUCTION_WAIT_FOR_DISPLAYED = 'waitForDisplayed'; |
20
|
|
|
const INSTRUCTION_WAIT_FOR_EXISTS = 'waitForExists'; |
21
|
|
|
const INSTRUCTION_WAIT_FOR_NOT_EXISTS = 'waitForNotExists'; |
22
|
|
|
const INSTRUCTION_WAIT_FOR_HIDDEN = 'waitForHidden'; |
23
|
|
|
const INSTRUCTION_PAUSE = 'pause'; |
24
|
|
|
const INSTRUCTION_USE_MANUAL_TIMING = 'manualTiming'; |
25
|
|
|
|
26
|
|
|
protected $webdriver; |
27
|
|
|
protected $testCase; |
28
|
|
|
protected $loaded; |
29
|
|
|
|
30
|
|
|
public function __construct( |
31
|
|
|
AbstractTestCase $testCase, |
32
|
|
|
WebDriver $webdriver, |
33
|
|
|
WaitForPageLoaded $loaded |
34
|
|
|
) |
35
|
|
|
{ |
36
|
|
|
$this->testCase = $testCase; |
37
|
|
|
$this->webdriver = $webdriver; |
38
|
|
|
$this->loaded = $loaded; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $instructions |
43
|
|
|
* @throws ElementNotVisibleException |
44
|
|
|
* @throws InvalidConfigurationException |
45
|
|
|
* @throws \Facebook\WebDriver\Exception\NoSuchElementException |
46
|
|
|
* @throws \Facebook\WebDriver\Exception\TimeOutException |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
public function navigateTo(array $instructions) |
50
|
|
|
{ |
51
|
|
|
$this->testCase->assertGreaterThan(0, count($instructions), 'Instruction navigator requires at least one instruction'); |
52
|
|
|
$useAutomaticTiming = true; |
53
|
|
|
foreach ($instructions as $key => $instruction) { |
54
|
|
|
if (count($instruction) > 0 && $instruction[0] == self::INSTRUCTION_USE_MANUAL_TIMING) { |
55
|
|
|
$useAutomaticTiming = false; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($instructions as $instruction) { |
60
|
|
|
$this->testCase->assertCount(2, $instruction, 'Navigation instructions need to be a 2 member array. First item is the instruction type, the second is the XPath'); |
61
|
|
|
list($instruction, $xpath) = $instruction; |
62
|
|
|
if ($useAutomaticTiming) { |
63
|
|
|
$this->testCase->sleep('100ms'); // Courtesy sleep of 100ms |
64
|
|
|
if ($instruction == self::INSTRUCTION_MOUSE_MOVETO || $instruction == self::INSTRUCTION_MOUSE_CLICK) { |
65
|
|
|
$this->webdriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH)); |
|
|
|
|
66
|
|
|
$this->webdriver->wait(5)->until(ExpectedCondition::elementToBeClickable(WebDriverBy::xpath($xpath))); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
switch ($instruction) { |
71
|
|
|
case self::INSTRUCTION_MOUSE_CLICK: |
72
|
|
|
$element = $this->webdriver->byXpath($xpath); |
73
|
|
|
if (!$element->isDisplayed()) { |
74
|
|
|
throw new ElementNotVisibleException('The element is not visible: '.$xpath); |
75
|
|
|
} |
76
|
|
|
$element->click(); |
77
|
|
|
break; |
78
|
|
|
case self::INSTRUCTION_MOUSE_MOVETO: |
79
|
|
|
$element = $this->webdriver->byXpath($xpath); |
80
|
|
|
$this->webdriver->getMouse()->mouseMove($element->getCoordinates()); |
81
|
|
|
break; |
82
|
|
|
case self::INSTRUCTION_WAIT_FOR_EXISTS: |
83
|
|
|
$this->webdriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH)); |
|
|
|
|
84
|
|
|
break; |
85
|
|
|
case self::INSTRUCTION_WAIT_FOR_NOT_EXISTS: |
86
|
|
|
$this->webdriver->wait()->until( |
87
|
|
|
ExpectedCondition::not( |
|
|
|
|
88
|
|
|
ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH) |
|
|
|
|
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
break; |
92
|
|
View Code Duplication |
case self::INSTRUCTION_WAIT_FOR_DISPLAYED: |
|
|
|
|
93
|
|
|
$element = $this->webdriver->byXpath($xpath); |
94
|
|
|
$this->webdriver->wait()->until(ExpectedCondition::visibilityOf($element)); |
|
|
|
|
95
|
|
|
break; |
96
|
|
|
case self::INSTRUCTION_PAUSE: |
97
|
|
|
$this->testCase->sleep($xpath); |
98
|
|
|
break; |
99
|
|
View Code Duplication |
case self::INSTRUCTION_WAIT_FOR_HIDDEN: |
|
|
|
|
100
|
|
|
$element = $this->webdriver->byXpath($xpath); |
101
|
|
|
$this->webdriver->wait()->until( |
102
|
|
|
ExpectedCondition::not( |
|
|
|
|
103
|
|
|
ExpectedCondition::visibilityOf( |
|
|
|
|
104
|
|
|
$element |
105
|
|
|
) |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
break; |
109
|
|
|
default: |
110
|
|
|
throw new InvalidConfigurationException('Unknown login instruction: '.$instruction); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: