|
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\Themes\ThemeConfigurationInterface; |
|
11
|
|
|
use Magium\WebDriver\ExpectedCondition; |
|
12
|
|
|
use Magium\WebDriver\WebDriver; |
|
13
|
|
|
|
|
14
|
|
|
class InstructionNavigator |
|
15
|
|
|
{ |
|
16
|
|
|
const NAVIGATOR = 'InstructionNavigator'; |
|
17
|
|
|
|
|
18
|
|
|
const INSTRUCTION_MOUSE_CLICK = 'mouseClick'; |
|
19
|
|
|
const INSTRUCTION_MOUSE_MOVETO = 'mouseMoveTo'; |
|
20
|
|
|
const INSTRUCTION_WAIT_FOR_DISPLAYED = 'waitForDisplayed'; |
|
21
|
|
|
const INSTRUCTION_WAIT_FOR_EXISTS = 'waitForExists'; |
|
22
|
|
|
const INSTRUCTION_WAIT_FOR_NOT_EXISTS = 'waitForNotExists'; |
|
23
|
|
|
const INSTRUCTION_WAIT_FOR_HIDDEN = 'waitForHidden'; |
|
24
|
|
|
const INSTRUCTION_PAUSE = 'pause'; |
|
25
|
|
|
const INSTRUCTION_USE_MANUAL_TIMING = 'manualTiming'; |
|
26
|
|
|
|
|
27
|
|
|
protected $webdriver; |
|
28
|
|
|
protected $testCase; |
|
29
|
|
|
protected $loaded; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
AbstractTestCase $testCase, |
|
33
|
|
|
WebDriver $webdriver, |
|
34
|
|
|
WaitForPageLoaded $loaded |
|
35
|
|
|
) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->testCase = $testCase; |
|
38
|
|
|
$this->webdriver = $webdriver; |
|
39
|
|
|
$this->loaded = $loaded; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $instructions |
|
44
|
|
|
* @throws ElementNotVisibleException |
|
45
|
|
|
* @throws InvalidConfigurationException |
|
46
|
|
|
* @throws \Facebook\WebDriver\Exception\NoSuchElementException |
|
47
|
|
|
* @throws \Facebook\WebDriver\Exception\TimeOutException |
|
48
|
|
|
*/ |
|
49
|
|
|
|
|
50
|
|
|
public function navigateTo(array $instructions) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->testCase->assertGreaterThan(0, count($instructions), 'Instruction navigator requires at least one instruction'); |
|
53
|
|
|
$useAutomaticTiming = true; |
|
54
|
|
|
foreach ($instructions as $key => $instruction) { |
|
55
|
|
|
if (count($instruction) > 0 && $instruction[0] == self::INSTRUCTION_USE_MANUAL_TIMING) { |
|
56
|
|
|
$useAutomaticTiming = false; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
foreach ($instructions as $instruction) { |
|
61
|
|
|
$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'); |
|
62
|
|
|
list($instruction, $xpath) = $instruction; |
|
63
|
|
|
if ($useAutomaticTiming) { |
|
64
|
|
|
$this->testCase->sleep('100ms'); // Courtesy sleep of 100ms |
|
65
|
|
|
if ($instruction == self::INSTRUCTION_MOUSE_MOVETO || $instruction == self::INSTRUCTION_MOUSE_CLICK) { |
|
66
|
|
|
$this->webdriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH)); |
|
67
|
|
|
$this->webdriver->wait(5)->until(ExpectedCondition::elementToBeClickable(WebDriverBy::xpath($xpath))); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
switch ($instruction) { |
|
72
|
|
|
case self::INSTRUCTION_MOUSE_CLICK: |
|
73
|
|
|
$element = $this->webdriver->byXpath($xpath); |
|
74
|
|
|
if (!$element->isDisplayed()) { |
|
75
|
|
|
throw new ElementNotVisibleException('The element is not visible: ' . $xpath); |
|
76
|
|
|
} |
|
77
|
|
|
$element->click(); |
|
78
|
|
|
break; |
|
79
|
|
|
case self::INSTRUCTION_MOUSE_MOVETO: |
|
80
|
|
|
$element = $this->webdriver->byXpath($xpath); |
|
81
|
|
|
$this->webdriver->getMouse()->mouseMove($element->getCoordinates()); |
|
82
|
|
|
break; |
|
83
|
|
|
case self::INSTRUCTION_WAIT_FOR_EXISTS: |
|
84
|
|
|
$this->webdriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH)); |
|
85
|
|
|
break; |
|
86
|
|
|
case self::INSTRUCTION_WAIT_FOR_NOT_EXISTS: |
|
87
|
|
|
$this->webdriver->wait()->until( |
|
88
|
|
|
ExpectedCondition::not( |
|
89
|
|
|
ExpectedCondition::elementExists( $xpath, WebDriver::BY_XPATH) |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
break; |
|
93
|
|
View Code Duplication |
case self::INSTRUCTION_WAIT_FOR_DISPLAYED: |
|
|
|
|
|
|
94
|
|
|
$element = $this->webdriver->byXpath($xpath); |
|
95
|
|
|
$this->webdriver->wait()->until(ExpectedCondition::visibilityOf($element)); |
|
96
|
|
|
break; |
|
97
|
|
View Code Duplication |
case self::INSTRUCTION_WAIT_FOR_HIDDEN: |
|
|
|
|
|
|
98
|
|
|
$element = $this->webdriver->byXpath($xpath); |
|
99
|
|
|
$this->webdriver->wait()->until( |
|
100
|
|
|
ExpectedCondition::not( |
|
101
|
|
|
ExpectedCondition::visibilityOf( |
|
102
|
|
|
$element |
|
103
|
|
|
) |
|
104
|
|
|
) |
|
105
|
|
|
); |
|
106
|
|
|
break; |
|
107
|
|
|
default: |
|
108
|
|
|
throw new InvalidConfigurationException('Unknown login instruction: ' .$instruction ); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.