|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\Magento\Actions\Cart; |
|
4
|
|
|
|
|
5
|
|
|
use Magium\Actions\StaticActionInterface; |
|
6
|
|
|
use Magium\Magento\Extractors\Catalog\Cart\AddToCart; |
|
7
|
|
|
use Magium\Magento\Themes\AbstractThemeConfiguration; |
|
8
|
|
|
use Magium\WebDriver\ExpectedCondition; |
|
9
|
|
|
use Magium\WebDriver\WebDriver; |
|
10
|
|
|
|
|
11
|
|
|
class AddSimpleProductToCart implements StaticActionInterface |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
const ACTION = 'Cart\AddSimpleProductToCart'; |
|
15
|
|
|
|
|
16
|
|
|
const EXCEPTION_NO_ELEMENT = 'Magium\Magento\Actions\Cart\NoSuchElementException'; |
|
17
|
|
|
const EXCEPTION_ADD_TO_CART_FAILED = 'Magium\Magento\Actions\Cart\AddToCartFailedException'; |
|
18
|
|
|
|
|
19
|
|
|
protected $webDriver; |
|
20
|
|
|
protected $theme; |
|
21
|
|
|
protected $addToCart; |
|
22
|
|
|
|
|
23
|
|
|
protected $requireQty; |
|
24
|
|
|
protected $addQty; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
WebDriver $webDriver, |
|
28
|
|
|
AbstractThemeConfiguration $themeConfiguration, |
|
29
|
|
|
AddToCart $addToCart |
|
30
|
|
|
) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->webDriver = $webDriver; |
|
33
|
|
|
$this->theme = $themeConfiguration; |
|
34
|
|
|
$this->addToCart = $addToCart; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function requireQty($require = true) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->requireQty = $require; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setQty($number) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->requireQty(); |
|
45
|
|
|
$this->addQty = $number; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function execute() |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->requireQty || $this->addQty > 1) { |
|
51
|
|
View Code Duplication |
if (!$this->webDriver->elementExists($this->theme->getSimpleProductQtyXpath(), WebDriver::BY_XPATH)) { |
|
|
|
|
|
|
52
|
|
|
throw new NoSuchElementException('Could not find the simple add to cart element with the Xpath: ' . $this->theme->getAddToCartXpath()); |
|
53
|
|
|
} |
|
54
|
|
|
$element = $this->webDriver->byXpath($this->theme->getSimpleProductQtyXpath()); |
|
55
|
|
|
$element->clear(); |
|
56
|
|
|
$element->sendKeys($this->addQty); |
|
57
|
|
|
} |
|
58
|
|
|
$this->clickAddToCart(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function clickAddToCart() |
|
62
|
|
|
{ |
|
63
|
|
|
$element = $this->addToCart->getElement(); |
|
64
|
|
|
// Because, for some reason, the M2 design HIDES the add-to-cart button as part of the default theme. Why |
|
65
|
|
|
// would you want to hide the second most important button on the site? |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
|
|
$element->click(); |
|
69
|
|
|
} catch (\Exception $e) { |
|
70
|
|
|
$e2 = $this->webDriver->byXpath($this->theme->getAddToCartXpath() . '/ancestor::li'); |
|
71
|
|
|
$this->webDriver->getMouse()->mouseMove($e2->getCoordinates()); |
|
72
|
|
|
$element->click(); |
|
73
|
|
|
} |
|
74
|
|
|
$this->webDriver->wait()->until(ExpectedCondition::elementExists($this->theme->getAddToCartSuccessXpath(), WebDriver::BY_XPATH)); |
|
|
|
|
|
|
75
|
|
|
$element = $this->webDriver->byXpath($this->theme->getAddToCartSuccessXpath()); |
|
76
|
|
|
$this->webDriver->wait()->until(ExpectedCondition::visibilityOf($element)); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
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.