Skip to content

Commit e96aff0

Browse files
author
Oleksandr Manchenko
committed
MTA-550: Re-factor Tests for Assign Promoted Products
- Re-factor cross-sell products
1 parent 088b715 commit e96aff0

File tree

18 files changed

+566
-341
lines changed

18 files changed

+566
-341
lines changed

dev/tests/functional/tests/app/Magento/Bundle/Test/Fixture/BundleProduct.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,5 +510,23 @@
510510
<backend_type>virtual</backend_type>
511511
<group>websites</group>
512512
</field>
513+
<field name="cross_sell_products">
514+
<attribute_code>cross_sell_products</attribute_code>
515+
<backend_type>virtual</backend_type>
516+
<group>crosssells</group>
517+
<source>Magento\Catalog\Test\Fixture\CatalogProductSimple\CrossSellProducts</source>
518+
</field>
519+
<field name="up_sell_products">
520+
<attribute_code>up_sell_products</attribute_code>
521+
<backend_type>virtual</backend_type>
522+
<group>upsells</group>
523+
<source>Magento\Catalog\Test\Fixture\CatalogProductSimple\UpSellProducts</source>
524+
</field>
525+
<field name="related_products">
526+
<attribute_code>related_products</attribute_code>
527+
<backend_type>virtual</backend_type>
528+
<group>related-products</group>
529+
<source>Magento\Catalog\Test\Fixture\CatalogProductSimple\RelatedProducts</source>
530+
</field>
513531
</fields>
514532
</fixture>

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Product/ProductList/Crosssell.php

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,72 @@
99
use Magento\Mtf\Block\Block;
1010
use Magento\Mtf\Client\Locator;
1111
use Magento\Mtf\Fixture\FixtureInterface;
12-
use Magento\Mtf\Client\Element\SimpleElement;
13-
use Magento\Catalog\Test\Fixture\Product;
1412

1513
/**
16-
* Class Crosssell
17-
* Crosssell product block on the page
14+
* Cross-sell product block on the checkout page.
1815
*/
1916
class Crosssell extends Block
2017
{
2118
/**
22-
* Link selector
19+
* Product item block.
2320
*
2421
* @var string
2522
*/
26-
protected $linkSelector = '.product.name [title="%s"]';
23+
protected $productItem = 'li.product-item';
2724

2825
/**
29-
* Verify cross-sell item
26+
* Product item block by product name.
27+
*
28+
* @var string
29+
*/
30+
protected $productItemByName = './/*[contains(@class,"product-item-link") and @title="%s"]/ancestor::li';
31+
32+
/**
33+
* Check whether block is visible.
3034
*
31-
* @param FixtureInterface $crosssell
3235
* @return bool
3336
*/
34-
public function verifyProductCrosssell(FixtureInterface $crosssell)
37+
public function isVisible()
3538
{
36-
$match = $this->_rootElement->find(sprintf($this->linkSelector, $crosssell->getName()), Locator::SELECTOR_CSS);
37-
return $match->isVisible();
39+
return $this->_rootElement->isVisible();
3840
}
3941

4042
/**
41-
* Click on cross-sell product link
43+
* Return product item block.
4244
*
43-
* @param Product $product
44-
* @return SimpleElement
45+
* @param FixtureInterface $product
46+
* @return ProductItem
4547
*/
46-
public function clickLink($product)
48+
public function getProductItem(FixtureInterface $product)
4749
{
48-
$this->_rootElement->find(
49-
sprintf($this->linkSelector, $product->getName()),
50-
Locator::SELECTOR_CSS
51-
)->click();
50+
$locator = sprintf($this->productItemByName, $product->getName());
51+
52+
return $this->blockFactory->create(
53+
'Magento\Catalog\Test\Block\Product\ProductList\ProductItem',
54+
['element' => $this->_rootElement->find($locator, Locator::SELECTOR_XPATH)]
55+
);
56+
}
57+
58+
/**
59+
* Get list of product names.
60+
*
61+
* @return array
62+
*/
63+
public function getProductNames()
64+
{
65+
$productItems = $this->_rootElement->getElements($this->productItem, Locator::SELECTOR_CSS);
66+
$names = [];
67+
68+
foreach ($productItems as $productItem) {
69+
/** @var ProductItem $productItemBlock */
70+
$productItemBlock = $this->blockFactory->create(
71+
'Magento\Catalog\Test\Block\Product\ProductList\ProductItem',
72+
['element' => $productItem]
73+
);
74+
75+
$names[] = $productItemBlock->getProductName();
76+
}
77+
78+
return $names;
5279
}
5380
}

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Product/ProductList/ProductItem.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,50 @@
1414
*/
1515
class ProductItem extends Block
1616
{
17+
/**
18+
* Link to product view page.
19+
*
20+
* @var string
21+
*/
22+
protected $link = 'a.product-item-link';
23+
1724
/**
1825
* 'Add to Card' button.
1926
*
2027
* @var string
2128
*/
2229
protected $addToCard = "button.action.tocart";
2330

31+
/**
32+
* Check whether block is visible.
33+
*
34+
* @return bool
35+
*/
36+
public function isVisible()
37+
{
38+
return $this->_rootElement->isVisible();
39+
}
40+
41+
/**
42+
* Open product view page by link.
43+
*
44+
* @return void
45+
*/
46+
public function open()
47+
{
48+
$this->_rootElement->find($this->link, Locator::SELECTOR_CSS)->click();
49+
}
50+
51+
/**
52+
* Return product name.
53+
*
54+
* @return string
55+
*/
56+
public function getProductName()
57+
{
58+
return trim($this->_rootElement->find($this->link)->getText());
59+
}
60+
2461
/**
2562
* Checking that "Add to Card" button is visible
2663
*
@@ -30,4 +67,14 @@ public function isVisibleAddToCardButton()
3067
{
3168
return $this->_rootElement->find($this->addToCard, Locator::SELECTOR_CSS)->isVisible();
3269
}
70+
71+
/**
72+
* Click by "Add to Cart" button.
73+
*
74+
* @return void
75+
*/
76+
public function clickAddToCart()
77+
{
78+
$this->_rootElement->find($this->addToCard, Locator::SELECTOR_CSS)->click();
79+
}
3380
}

dev/tests/functional/tests/app/Magento/Catalog/Test/Constraint/AssertCrossSellsProductsSection.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
use Magento\Checkout\Test\Page\CheckoutCart;
1212
use Magento\Mtf\Client\BrowserInterface;
1313
use Magento\Mtf\Constraint\AbstractConstraint;
14-
use Magento\Mtf\Fixture\InjectableFixture;
1514

1615
/**
17-
* Class AssertCrossSellsProductsSection
18-
* Assert that product is displayed in cross-sell section
16+
* Assert that product is displayed in cross-sell section.
1917
*/
2018
class AssertCrossSellsProductsSection extends AbstractConstraint
2119
{
@@ -24,30 +22,30 @@ class AssertCrossSellsProductsSection extends AbstractConstraint
2422
/* end tags */
2523

2624
/**
27-
* Assert that product is displayed in cross-sell section
25+
* Assert that product is displayed in cross-sell section.
2826
*
2927
* @param BrowserInterface $browser
3028
* @param CheckoutCart $checkoutCart
3129
* @param CatalogProductSimple $product
3230
* @param CatalogProductView $catalogProductView
33-
* @param InjectableFixture[] $relatedProducts
3431
* @return void
3532
*/
3633
public function processAssert(
3734
BrowserInterface $browser,
3835
CheckoutCart $checkoutCart,
3936
CatalogProductSimple $product,
40-
CatalogProductView $catalogProductView,
41-
array $relatedProducts
37+
CatalogProductView $catalogProductView
4238
) {
39+
$relatedProducts = $product->getDataFieldConfig('cross_sell_products')['source']->getProducts();
40+
$errors = [];
41+
4342
$checkoutCart->open();
4443
$checkoutCart->getCartBlock()->clearShoppingCart();
4544

4645
$browser->open($_ENV['app_frontend_url'] . $product->getUrlKey() . '.html');
4746
$catalogProductView->getViewBlock()->addToCart($product);
48-
$errors = [];
4947
foreach ($relatedProducts as $relatedProduct) {
50-
if (!$checkoutCart->getCrosssellBlock()->verifyProductCrosssell($relatedProduct)) {
48+
if (!$checkoutCart->getCrosssellBlock()->getProductItem($relatedProduct)->isVisible()) {
5149
$errors[] = 'Product \'' . $relatedProduct->getName() . '\' is absent in cross-sell section.';
5250
}
5351
}
@@ -56,7 +54,7 @@ public function processAssert(
5654
}
5755

5856
/**
59-
* Text success product is displayed in cross-sell section
57+
* Text success product is displayed in cross-sell section.
6058
*
6159
* @return string
6260
*/

dev/tests/functional/tests/app/Magento/Catalog/Test/Constraint/AssertNoCrossSellsProductsSection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
use Magento\Mtf\Fixture\InjectableFixture;
1515

1616
/**
17-
* Class AssertNoCrossSellsProductsSection
18-
* Assert that product is not displayed in cross-sell section
17+
* Assert that product is not displayed in cross-sell section.
1918
*/
2019
class AssertNoCrossSellsProductsSection extends AbstractConstraint
2120
{
@@ -24,7 +23,7 @@ class AssertNoCrossSellsProductsSection extends AbstractConstraint
2423
/* end tags */
2524

2625
/**
27-
* Assert that product is not displayed in cross-sell section
26+
* Assert that product is not displayed in cross-sell section.
2827
*
2928
* @param BrowserInterface $browser
3029
* @param CatalogProductSimple $product
@@ -47,14 +46,14 @@ public function processAssert(
4746
$catalogProductView->getViewBlock()->addToCart($product);
4847
foreach ($relatedProducts as $relatedProduct) {
4948
\PHPUnit_Framework_Assert::assertFalse(
50-
$checkoutCart->getCrosssellBlock()->verifyProductCrosssell($relatedProduct),
49+
$checkoutCart->getCrosssellBlock()->getProductItem($relatedProduct)->isVisible(),
5150
'Product \'' . $relatedProduct->getName() . '\' is exist in cross-sell section.'
5251
);
5352
}
5453
}
5554

5655
/**
57-
* Text success product is not displayed in cross-sell section
56+
* Text success product is not displayed in cross-sell section.
5857
*
5958
* @return string
6059
*/

dev/tests/functional/tests/app/Magento/Catalog/Test/Fixture/CatalogProductSimple/AbstractRelatedProducts.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,26 @@
1111
use Magento\Mtf\Fixture\FixtureInterface;
1212

1313
/**
14-
* Class AbstractRelatedProducts
15-
* Base class for create related products
14+
* Base class for create related products.
1615
*/
1716
class AbstractRelatedProducts implements FixtureInterface
1817
{
1918
/**
20-
* Data set configuration settings
19+
* Data set configuration settings.
2120
*
2221
* @var array
2322
*/
2423
protected $params;
2524

2625
/**
27-
* Data of the created products
26+
* Data of the created products.
2827
*
2928
* @var array
3029
*/
3130
protected $data = [];
3231

3332
/**
34-
* Products fixture
33+
* Products fixture.
3534
*
3635
* @var array
3736
*/
@@ -41,7 +40,7 @@ class AbstractRelatedProducts implements FixtureInterface
4140
* @constructor
4241
* @param FixtureFactory $fixtureFactory
4342
* @param array $params
44-
* @param array $data
43+
* @param array $data [optional]
4544
*/
4645
public function __construct(FixtureFactory $fixtureFactory, array $params, array $data = [])
4746
{
@@ -51,29 +50,33 @@ public function __construct(FixtureFactory $fixtureFactory, array $params, array
5150
$presets = array_map('trim', explode(',', $data['presets']));
5251
foreach ($presets as $preset) {
5352
list($fixtureCode, $dataSet) = explode('::', $preset);
54-
55-
/** @var CatalogProductSimple $product */
56-
$product = $fixtureFactory->createByCode($fixtureCode, ['dataSet' => $dataSet]);
57-
if (!$product->hasData('id')) {
58-
$product->persist();
59-
}
60-
53+
$this->products[] = $fixtureFactory->createByCode($fixtureCode, ['dataSet' => $dataSet]);
54+
}
55+
}
56+
if (isset($data['products'])) {
57+
foreach ($data['products'] as $product) {
6158
$this->products[] = $product;
62-
$this->data[] = [
63-
'entity_id' => $product->getId(),
64-
'name' => $product->getName(),
65-
'sku' => $product->getSku(),
66-
];
6759
}
6860
}
6961

62+
foreach ($this->products as $product) {
63+
if (!$product->hasData('id')) {
64+
$product->persist();
65+
}
66+
67+
$this->data[] = [
68+
'entity_id' => $product->getId(),
69+
'name' => $product->getName(),
70+
'sku' => $product->getSku(),
71+
];
72+
}
7073
if (isset($data['data'])) {
7174
$this->data = array_replace_recursive($this->data, $data['data']);
7275
}
7376
}
7477

7578
/**
76-
* Persist related products
79+
* Persist related products.
7780
*
7881
* @return void
7982
*/
@@ -83,7 +86,7 @@ public function persist()
8386
}
8487

8588
/**
86-
* Return prepared data set
89+
* Return prepared data set.
8790
*
8891
* @param string|null $key
8992
* @return array
@@ -96,7 +99,7 @@ public function getData($key = null)
9699
}
97100

98101
/**
99-
* Return data set configuration settings
102+
* Return data set configuration settings.
100103
*
101104
* @return array
102105
*/
@@ -106,7 +109,7 @@ public function getDataConfig()
106109
}
107110

108111
/**
109-
* Return related products
112+
* Return related products.
110113
*
111114
* @return array
112115
*/

0 commit comments

Comments
 (0)