|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\Magento\Extractors\Catalog\Products; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Facebook\WebDriver\WebDriverElement; |
|
7
|
|
|
use Magium\Extractors\AbstractExtractor; |
|
8
|
|
|
use Magium\WebDriver\WebDriver; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractProductCollection extends AbstractExtractor |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
protected $products = []; |
|
14
|
|
|
protected $testElement; |
|
15
|
|
|
|
|
16
|
|
|
abstract function getElementXpath($type, $count); |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return ProductSummary[] |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
public function getProductList() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->extract(); |
|
25
|
|
|
return $this->products; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function getByXpath($type, $count, $attribute = null, $getElement = false) |
|
29
|
|
|
{ |
|
30
|
|
|
$xpath = $this->getElementXpath($type, $count); |
|
31
|
|
|
if ($this->webDriver->elementExists($xpath, WebDriver::BY_XPATH)) { |
|
32
|
|
|
$element = $this->webDriver->byXpath($xpath); |
|
33
|
|
|
if ($getElement) { |
|
34
|
|
|
return $element; |
|
35
|
|
|
} else if ($attribute) { |
|
36
|
|
|
return $element->getAttribute($attribute); |
|
37
|
|
|
} else { |
|
38
|
|
|
return trim($element->getText()); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
return null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function extract() |
|
45
|
|
|
{ |
|
46
|
|
|
if ($this->testElement instanceof WebDriverElement && $this->webDriver->elementAttached($this->testElement)) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
$this->testElement = $this->webDriver->byXpath('//body'); |
|
50
|
|
|
$this->products = []; |
|
51
|
|
|
$count = 0; |
|
52
|
|
|
while ($this->webDriver->elementExists($this->getElementXpath(ProductSummary::TITLE, ++$count), WebDriver::BY_XPATH)) { |
|
53
|
|
|
$title = $this->getByXpath(ProductSummary::TITLE, $count); |
|
54
|
|
|
$price = $this->getByXpath(ProductSummary::PRICE, $count); |
|
55
|
|
|
$link = $this->getByXpath(ProductSummary::LINK, $count, 'href'); |
|
56
|
|
|
$image = $this->getByXpath(ProductSummary::IMAGE, $count, 'src'); |
|
57
|
|
|
$originalPrice = $this->getByXpath(ProductSummary::ORIGINAL_PRICE, $count); |
|
58
|
|
|
if ($originalPrice === null) { |
|
59
|
|
|
$originalPrice = $price; |
|
60
|
|
|
} |
|
61
|
|
|
$wishlistLink = $this->getByXpath(ProductSummary::WISHLIST_LINK, $count, null, true); |
|
62
|
|
|
$compareLink = $this->getByXpath(ProductSummary::COMPARE_LINK, $count, null, true); |
|
63
|
|
|
$description = $this->getByXpath(ProductSummary::DESCRIPTION, $count); |
|
64
|
|
|
$addToCartLink = $this->getByXpath(ProductSummary::ADD_TO_CART_LINK, $count, null, true); |
|
65
|
|
|
|
|
66
|
|
|
$product = new ProductSummary( |
|
67
|
|
|
$title, |
|
68
|
|
|
$price, |
|
69
|
|
|
$link, |
|
70
|
|
|
$image, |
|
71
|
|
|
$originalPrice, |
|
72
|
|
|
$wishlistLink, |
|
|
|
|
|
|
73
|
|
|
$compareLink, |
|
|
|
|
|
|
74
|
|
|
$description, |
|
75
|
|
|
$addToCartLink |
|
|
|
|
|
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$this->products[] = $product; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.