1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Magento\Navigators; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Facebook\WebDriver\WebDriverElement; |
7
|
|
|
use Magium\Actions\WaitForPageLoaded; |
8
|
|
|
use Magium\Magento\Themes\NavigableThemeInterface; |
9
|
|
|
use Magium\Navigators\ConfigurableNavigatorInterface; |
10
|
|
|
use Magium\Util\Log\Logger; |
11
|
|
|
use Magium\WebDriver\ExpectedCondition; |
12
|
|
|
use Magium\WebDriver\WebDriver; |
13
|
|
|
|
14
|
|
|
class BaseMenu implements ConfigurableNavigatorInterface |
15
|
|
|
{ |
16
|
|
|
const NAVIGATOR = 'BaseMenu'; |
17
|
|
|
protected $webdriver; |
18
|
|
|
protected $themeConfiguration; |
19
|
|
|
protected $loaded; |
20
|
|
|
protected $logger; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
NavigableThemeInterface $theme, |
24
|
|
|
WebDriver $webdriver, |
25
|
|
|
WaitForPageLoaded $loaded, |
26
|
|
|
Logger $logger |
27
|
|
|
) |
28
|
|
|
{ |
29
|
|
|
$this->themeConfiguration = $theme; |
30
|
|
|
$this->webdriver = $webdriver; |
31
|
|
|
$this->loaded = $loaded; |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function pathAction($path, &$xpath) |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
usleep(500000); // Give the UI some time to update |
39
|
|
|
$xpath .= '/descendant::' . $this->themeConfiguration->getNavigationChildXPathSelector($path); |
40
|
|
|
|
41
|
|
|
$element = $this->webdriver->byXpath($xpath . '/a'); |
42
|
|
|
|
43
|
|
|
$this->execute($element); |
44
|
|
|
|
45
|
|
|
return $element; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function execute(WebDriverElement $element) |
49
|
|
|
{ |
50
|
|
|
$this->webdriver->getMouse()->mouseMove($element->getCoordinates()); |
|
|
|
|
51
|
|
|
if ($this->themeConfiguration->getUseClicksToNavigate()) { |
52
|
|
|
$this->webdriver->getMouse()->click(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function navigateTo($path) |
57
|
|
|
{ |
58
|
|
|
$paths = explode('/', $path); |
59
|
|
|
$xpath = $this->themeConfiguration->getNavigationBaseXPathSelector(); |
60
|
|
|
|
61
|
|
|
$this->webdriver->wait()->until(ExpectedCondition::visibilityOf($this->webdriver->byXpath($xpath))); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$element = null; |
64
|
|
|
|
65
|
|
|
foreach ($paths as $p) { |
66
|
|
|
$element = $this->pathAction($p, $xpath); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!$this->themeConfiguration->getUseClicksToNavigate() && $element instanceof WebDriverElement) { |
70
|
|
|
$element->click(); |
71
|
|
|
$this->loaded->execute($element); |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: