Skip to content

Commit b52584a

Browse files
author
Valeriy Nayda
committed
MAGETWO-65113: [Performance] Add an opportunity to turn off swatches for category page
1 parent 97289ca commit b52584a

11 files changed

+416
-171
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\TestStep;
7+
8+
use Magento\Mtf\Client\BrowserInterface;
9+
use Magento\Mtf\Fixture\FixtureInterface;
10+
use Magento\Mtf\TestStep\TestStepInterface;
11+
12+
/**
13+
* Open product on frontend via url
14+
*/
15+
class OpenProductOnFrontendStep implements TestStepInterface
16+
{
17+
/**
18+
* Product fixture
19+
*
20+
* @var FixtureInterface
21+
*/
22+
private $product;
23+
24+
/**
25+
* Browser
26+
*
27+
* @var BrowserInterface
28+
*/
29+
private $browser;
30+
31+
/**
32+
* Preparing step properties
33+
*
34+
* @param FixtureInterface $product
35+
* @param BrowserInterface $browser
36+
*/
37+
public function __construct(FixtureInterface $product, BrowserInterface $browser)
38+
{
39+
$this->product = $product;
40+
$this->browser = $browser;
41+
}
42+
43+
/**
44+
* Open product on frontend via url
45+
*
46+
* @return void
47+
*/
48+
public function run()
49+
{
50+
$this->browser->open($_ENV['app_frontend_url'] . $this->product->getUrlKey() . '.html');
51+
}
52+
}

dev/tests/functional/tests/app/Magento/Swatches/Test/Block/Product/ViewWithSwatches.php

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Swatches\Test\Block\Product;
88

99
use Magento\Catalog\Test\Block\Product\View;
10+
use Magento\Mtf\Client\ElementInterface;
1011
use Magento\Mtf\Fixture\InjectableFixture;
1112

1213
/**
@@ -15,14 +16,78 @@
1516
class ViewWithSwatches extends View
1617
{
1718
/**
18-
* Selector for swatch attribute value
19+
* Selector for all swatch attributes
1920
*
2021
* @var string
2122
*/
22-
private $swatchAttributeSelector = '.swatch-attribute.%s .swatch-attribute-selected-option';
23+
private $swatchAttributesSelector = '.swatch-attribute';
2324

2425
/**
25-
* Get chosen options from the product view page.
26+
* Selector for swatch attribute label
27+
*
28+
* @var string
29+
*/
30+
private $swatchAttributesLabelSelector = '.swatch-attribute-label';
31+
32+
/**
33+
* Selector for all swatch attribute options
34+
*
35+
* @var string
36+
*/
37+
private $swatchAttributeOptionsSelector = '.swatch-option';
38+
39+
/**
40+
* Selector for selected swatch attribute options
41+
*
42+
* @var string
43+
*/
44+
private $selectedSwatchAttributeSelector = '.swatch-attribute.%s .swatch-attribute-selected-option';
45+
46+
/**
47+
* Get swatch attributes data from the product page. Key is attribute code
48+
*
49+
* @return array
50+
*/
51+
public function getSwatchAttributesData()
52+
{
53+
$this->waitForElementVisible($this->swatchAttributesSelector);
54+
55+
$swatchAttributesData = [];
56+
$swatchAttributes = $this->_rootElement->getElements($this->swatchAttributesSelector);
57+
foreach ($swatchAttributes as $swatchAttribute) {
58+
$attributeCode = $swatchAttribute->getAttribute('attribute-code');
59+
$swatchAttributesData[$attributeCode] = [
60+
'attribute_code' => $attributeCode,
61+
'attribute_id' => $swatchAttribute->getAttribute('attribute-id'),
62+
'label' => $swatchAttribute->find($this->swatchAttributesLabelSelector)->getText(),
63+
'options' => $this->getSwatchAttributeOptionsData($swatchAttribute),
64+
];
65+
}
66+
return $swatchAttributesData;
67+
}
68+
69+
/**
70+
* Get swatch attribute options data. Key is option id
71+
*
72+
* @param ElementInterface $swatchAttribute
73+
* @return array
74+
*/
75+
private function getSwatchAttributeOptionsData(ElementInterface $swatchAttribute)
76+
{
77+
$optionsData = [];
78+
$options = $swatchAttribute->getElements($this->swatchAttributeOptionsSelector);
79+
foreach ($options as $option) {
80+
$optionId = $option->getAttribute('option-id');
81+
$optionsData[$optionId] = [
82+
'option_id' => $optionId,
83+
'label' => $option->getText(),
84+
];
85+
}
86+
return $optionsData;
87+
}
88+
89+
/**
90+
* Get chosen options from the product page
2691
*
2792
* @param InjectableFixture $product
2893
* @return array
@@ -34,7 +99,10 @@ public function getSelectedSwatchOptions(InjectableFixture $product)
3499
$attributesData = $availableAttributes['attributes_data'];
35100
$formData = [];
36101
foreach ($checkoutData['options']['configurable_options'] as $item) {
37-
$selector = sprintf($this->swatchAttributeSelector, $attributesData[$item['title']]['attribute_code']);
102+
$selector = sprintf(
103+
$this->selectedSwatchAttributeSelector,
104+
$attributesData[$item['title']]['attribute_code']
105+
);
38106
$this->waitForElementVisible($selector);
39107
$selected = $this->_rootElement->find($selector)->getText();
40108
$formData[$item['title']] = $selected;

dev/tests/functional/tests/app/Magento/Swatches/Test/Constraint/AssertSwatchConfigurableProductPage.php renamed to dev/tests/functional/tests/app/Magento/Swatches/Test/Constraint/AssertSelectedSwatchOptionsOnProductPage.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Swatches\Test\Constraint;
87

98
use Magento\Catalog\Test\Constraint\AssertProductPage;
@@ -12,9 +11,9 @@
1211
use Magento\Mtf\Client\BrowserInterface;
1312

1413
/**
15-
* Assert that product with swatches and regular dropdown redirect can't be add to cart from catalog catergory page.
14+
* Assert that selected(add to cart from category page) swatch attributes are displayed and selected on product page
1615
*/
17-
class AssertSwatchConfigurableProductPage extends AssertProductPage
16+
class AssertSelectedSwatchOptionsOnProductPage extends AssertProductPage
1817
{
1918
/**
2019
* {@inheritdoc}
@@ -27,7 +26,6 @@ public function processAssert(
2726
$this->product = $product;
2827
$this->productView = $catalogProductView->getProductViewWithSwatchesBlock();
2928

30-
// we need this line for waiti until page will be fully loaded
3129
$this->productView->getSelectedSwatchOptions($this->product);
3230
$errors = $this->verify();
3331
\PHPUnit_Framework_Assert::assertEmpty(
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Swatches\Test\Constraint;
7+
8+
use Magento\Catalog\Test\Constraint\AssertProductPage;
9+
use Magento\Catalog\Test\TestStep\OpenProductOnFrontendStep;
10+
use Magento\Mtf\Constraint\AbstractConstraint;
11+
use Magento\Mtf\Fixture\FixtureInterface;
12+
use Magento\Catalog\Test\Page\Product\CatalogProductView;
13+
use Magento\Mtf\TestStep\TestStepFactory;
14+
15+
/**
16+
* Assert that swatch attributes are displayed on product page
17+
*/
18+
class AssertSwatchOptionsOnProductPage extends AbstractConstraint
19+
{
20+
/**
21+
* @param TestStepFactory $stepFactory
22+
* @param CatalogProductView $catalogProductView
23+
* @param FixtureInterface $product
24+
*/
25+
public function processAssert(
26+
TestStepFactory $stepFactory,
27+
CatalogProductView $catalogProductView,
28+
FixtureInterface $product
29+
) {
30+
$stepFactory->create(OpenProductOnFrontendStep::class, ['product' => $product])
31+
->run();
32+
33+
$actualData = $catalogProductView->getProductViewWithSwatchesBlock()
34+
->getSwatchAttributesData();
35+
$expectedData = $product->getConfigurableAttributesData()['attributes_data'];
36+
37+
foreach ($expectedData as $expectedAttributeData) {
38+
\PHPUnit_Framework_Assert::assertArrayHasKey(
39+
$expectedAttributeData['attribute_code'],
40+
$actualData,
41+
'Attribute with code ' . $expectedAttributeData['attribute_code'] . ' is absent on Product page'
42+
);
43+
$actualAttributeData = $actualData[$expectedAttributeData['attribute_code']];
44+
$this->verifyAttribute($expectedAttributeData, $actualAttributeData);
45+
$this->verifyAttributeOptions($expectedAttributeData, $actualAttributeData);
46+
}
47+
}
48+
49+
/**
50+
* Verify attribute data
51+
*
52+
* @param array $expectedAttributeData
53+
* @param array $actualAttributeData
54+
*/
55+
private function verifyAttribute(array $expectedAttributeData, array $actualAttributeData)
56+
{
57+
\PHPUnit_Framework_Assert::assertEquals(
58+
$expectedAttributeData['attribute_code'],
59+
$actualAttributeData['attribute_code'],
60+
sprintf(
61+
'Attribute code "%s" is not equal to expected "%s"',
62+
$actualAttributeData['attribute_code'],
63+
$expectedAttributeData['attribute_code']
64+
)
65+
);
66+
\PHPUnit_Framework_Assert::assertEquals(
67+
$expectedAttributeData['attribute_id'],
68+
$actualAttributeData['attribute_id'],
69+
sprintf(
70+
'Attribute id "%s" is not equal to expected "%s"',
71+
$actualAttributeData['attribute_id'],
72+
$expectedAttributeData['attribute_id']
73+
)
74+
);
75+
\PHPUnit_Framework_Assert::assertEquals(
76+
$expectedAttributeData['label'],
77+
$actualAttributeData['label'],
78+
sprintf(
79+
'Attribute label "%s" is not equal to expected "%s"',
80+
$actualAttributeData['label'],
81+
$expectedAttributeData['label']
82+
)
83+
);
84+
}
85+
86+
/**
87+
* Verify attribute options data
88+
*
89+
* @param array $expectedAttributeData
90+
* @param array $actualAttributeData
91+
*/
92+
private function verifyAttributeOptions(array $expectedAttributeData, array $actualAttributeData)
93+
{
94+
if (isset($expectedAttributeData['options'])) {
95+
\PHPUnit_Framework_Assert::assertArrayHasKey(
96+
'options',
97+
$actualAttributeData,
98+
'Swatch attribute options are missed on Product page'
99+
);
100+
101+
$expectedOptionsCount = count($expectedAttributeData['options']);
102+
$actualOptionsCount = count($actualAttributeData['options']);
103+
\PHPUnit_Framework_Assert::assertEquals(
104+
$expectedOptionsCount,
105+
$actualOptionsCount,
106+
sprintf(
107+
'Attribute options count "%d" is not equal to expected "%d"',
108+
$actualOptionsCount,
109+
$expectedOptionsCount
110+
)
111+
);
112+
} else {
113+
\PHPUnit_Framework_Assert::assertArrayNotHasKey(
114+
'options',
115+
$actualAttributeData,
116+
'Product page must be without swatch attribute options'
117+
);
118+
}
119+
}
120+
121+
/**
122+
* Return string representation of the object.
123+
*
124+
* @return string
125+
*/
126+
public function toString()
127+
{
128+
return 'Swatch attributes are displayed on product page';
129+
}
130+
}

dev/tests/functional/tests/app/Magento/Swatches/Test/Constraint/AssertSwatchesVisibilityInCategory.php renamed to dev/tests/functional/tests/app/Magento/Swatches/Test/Constraint/AssertSwatchesAreNotVisibleOnCategoryPage.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,34 @@
1111
use Magento\Mtf\Fixture\FixtureInterface;
1212

1313
/**
14-
* Assert swatches visibility in category page.
14+
* Assert swatches are not visible on category page
1515
*/
16-
class AssertSwatchesVisibilityInCategory extends AbstractConstraint
16+
class AssertSwatchesAreNotVisibleOnCategoryPage extends AbstractConstraint
1717
{
1818
/**
19-
* Checking swatches in the category page.
20-
*
2119
* @param CatalogCategoryView $catalogCategoryView
2220
* @param CmsIndex $cmsIndex
2321
* @param FixtureInterface $product
24-
* @param bool $visible
2522
* @return void
2623
*/
2724
public function processAssert(
2825
CatalogCategoryView $catalogCategoryView,
2926
CmsIndex $cmsIndex,
30-
FixtureInterface $product,
31-
$visible = true
27+
FixtureInterface $product
3228
) {
3329
$this->openCategoryPage($cmsIndex, $product);
3430

35-
$swatchesBlockVisible = $catalogCategoryView->getListSwatchesProductBlock()
31+
$isSwatchesBlockVisible = $catalogCategoryView->getListSwatchesProductBlock()
3632
->getProductItem($product)->isSwatchesBlockVisible();
3733

38-
if ($visible) {
39-
\PHPUnit_Framework_Assert::assertTrue(
40-
$swatchesBlockVisible,
41-
'Swatches are absent on category page.'
42-
);
43-
} else {
44-
\PHPUnit_Framework_Assert::assertFalse(
45-
$swatchesBlockVisible,
46-
'Swatches are still present on category page.'
47-
);
48-
}
34+
\PHPUnit_Framework_Assert::assertFalse(
35+
$isSwatchesBlockVisible,
36+
'Swatches are still present on category page.'
37+
);
4938
}
5039

5140
/**
52-
* Open category view page.
41+
* Open category view page
5342
*
5443
* @param CmsIndex $cmsIndex
5544
* @param FixtureInterface $product
@@ -66,7 +55,7 @@ private function openCategoryPage(
6655
}
6756

6857
/**
69-
* Returns a string representation of the object.
58+
* Returns a string representation of the object
7059
*
7160
* @return string
7261
*/

0 commit comments

Comments
 (0)