Skip to content

Commit d790fc1

Browse files
author
Oleksandr Manchenko
committed
MTA-551: Re-factor Test for Out of Stock Item
1 parent f4e6ee2 commit d790fc1

File tree

7 files changed

+163
-184
lines changed

7 files changed

+163
-184
lines changed

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

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@
66

77
namespace Magento\Catalog\Test\Block\Product;
88

9+
use Magento\Catalog\Test\Block\Product\ProductList\ProductItem;
910
use Magento\Mtf\Block\Block;
1011
use Magento\Mtf\Client\Locator;
1112
use Magento\Mtf\Client\Element\SimpleElement;
1213
use Magento\Mtf\Factory\Factory;
14+
use Magento\Mtf\Fixture\InjectableFixture;
1315

1416
/**
15-
* Product list.
17+
* Product list block.
1618
*/
1719
class ListProduct extends Block
1820
{
21+
/**
22+
* Locator for product item block.
23+
*
24+
* @var string
25+
*/
26+
protected $productItem = './/*[contains(@class,"product-item-link") and @title="%s"]/ancestor::li';
27+
1928
/**
2029
* This member holds the class name of the regular price block.
2130
*
@@ -58,13 +67,6 @@ class ListProduct extends Block
5867
*/
5968
protected $oldPrice = ".old-price .price-container";
6069

61-
/**
62-
* 'Add to Card' button.
63-
*
64-
* @var string
65-
*/
66-
protected $addToCard = "button.action.tocart";
67-
6870
/**
6971
* Price box CSS selector.
7072
*
@@ -86,16 +88,35 @@ class ListProduct extends Block
8688
*/
8789
protected $sorter = '#sorter';
8890

91+
/**
92+
* Return product item block.
93+
*
94+
* @param InjectableFixture $product
95+
* @return ProductItem
96+
*/
97+
public function getProductItem(InjectableFixture $product)
98+
{
99+
$locator = sprintf($this->productItem, $product->getName());
100+
101+
return $this->blockFactory->create(
102+
'Magento\Catalog\Test\Block\Product\ProductList\ProductItem',
103+
['element' => $this->_rootElement->find($locator, Locator::SELECTOR_XPATH)]
104+
);
105+
}
106+
89107
/**
90108
* This method returns the price box block for the named product.
91109
*
92-
* @param string $productName String containing the name of the product to find.
110+
* @param string $productName
93111
* @return Price
94112
*/
95113
public function getProductPriceBlock($productName)
96114
{
97-
return Factory::getBlockFactory()->getMagentoCatalogProductPrice(
98-
$this->getProductDetailsElement($productName)->find($this->priceBlockClass, Locator::SELECTOR_CLASS_NAME)
115+
$productDetails = $this->getProductDetailsElement($productName);
116+
117+
return $this->blockFactory->create(
118+
'Magento\Catalog\Test\Block\Product\Price',
119+
['element' => $productDetails->find($this->priceBlockClass, Locator::SELECTOR_CLASS_NAME)]
99120
);
100121
}
101122

@@ -186,18 +207,7 @@ public function getOldPriceCategoryPage()
186207
*/
187208
public function getPrice($productId)
188209
{
189-
return $this->_rootElement->find(sprintf($this->priceBox, $productId), Locator::SELECTOR_CSS)
190-
->getText();
191-
}
192-
193-
/**
194-
* Check 'Add To Card' button availability.
195-
*
196-
* @return bool
197-
*/
198-
public function checkAddToCardButton()
199-
{
200-
return $this->_rootElement->find($this->addToCard, Locator::SELECTOR_CSS)->isVisible();
210+
return $this->_rootElement->find(sprintf($this->priceBox, $productId), Locator::SELECTOR_CSS)->getText();
201211
}
202212

203213
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Block\Product\ProductList;
8+
9+
use Magento\Mtf\Block\Block;
10+
use Magento\Mtf\Client\Locator;
11+
12+
/**
13+
* Product item block on frontend category view.
14+
*/
15+
class ProductItem extends Block
16+
{
17+
/**
18+
* 'Add to Card' button.
19+
*
20+
* @var string
21+
*/
22+
protected $addToCard = "button.action.tocart";
23+
24+
/**
25+
* Checking that "Add to Card" button is visible
26+
*
27+
* @return bool
28+
*/
29+
public function isVisibleAddToCardButton()
30+
{
31+
return $this->_rootElement->find($this->addToCard, Locator::SELECTOR_CSS)->isVisible();
32+
}
33+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function openMapBlockOnProductPage()
351351
*
352352
* @return bool
353353
*/
354-
public function checkAddToCardButton()
354+
public function isVisibleAddToCardButton()
355355
{
356356
return $this->_rootElement->find($this->addToCart, Locator::SELECTOR_CSS)->isVisible();
357357
}

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

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
use Magento\Mtf\Constraint\AbstractConstraint;
1414

1515
/**
16-
* Class AssertAddToCartButtonAbsent
17-
* Checks the button on the category/product pages
16+
* Checks the button on the category/product pages.
1817
*/
1918
class AssertAddToCartButtonAbsent extends AbstractConstraint
2019
{
@@ -23,35 +22,7 @@ class AssertAddToCartButtonAbsent extends AbstractConstraint
2322
/* end tags */
2423

2524
/**
26-
* Category Page
27-
*
28-
* @var CatalogCategoryView
29-
*/
30-
protected $catalogCategoryView;
31-
32-
/**
33-
* Index Page
34-
*
35-
* @var CmsIndex
36-
*/
37-
protected $cmsIndex;
38-
39-
/**
40-
* Product simple fixture
41-
*
42-
* @var CatalogProductSimple
43-
*/
44-
protected $product;
45-
46-
/**
47-
* Product Page on Frontend
48-
*
49-
* @var CatalogProductView
50-
*/
51-
protected $catalogProductView;
52-
53-
/**
54-
* Assert that "Add to cart" button is not display on page
25+
* Assert that "Add to cart" button is not display on page.
5526
*
5627
* @param CmsIndex $cmsIndex
5728
* @param CatalogCategoryView $catalogCategoryView
@@ -66,52 +37,29 @@ public function processAssert(
6637
CatalogProductSimple $product,
6738
CatalogProductView $catalogProductView
6839
) {
69-
$this->catalogCategoryView = $catalogCategoryView;
70-
$this->cmsIndex = $cmsIndex;
71-
$this->product = $product;
72-
$this->catalogProductView = $catalogProductView;
40+
$cmsIndex->open();
41+
$cmsIndex->getTopmenu()->selectCategoryByName($product->getCategoryIds()[0]);
7342

74-
$this->addToCardAbsentOnCategory();
75-
$this->addToCardAbsentOnProduct();
76-
}
43+
$isProductVisible = $catalogCategoryView->getListProductBlock()->isProductVisible($product->getName());
44+
while (!$isProductVisible && $catalogCategoryView->getBottomToolbar()->nextPage()) {
45+
$isProductVisible = $catalogCategoryView->getListProductBlock()->isProductVisible($product->getName());
46+
}
47+
\PHPUnit_Framework_Assert::assertTrue($isProductVisible, 'Product is absent on category page.');
7748

78-
/**
79-
* "Add to cart" button is not displayed on Category page
80-
*
81-
* @return void
82-
*/
83-
protected function addToCardAbsentOnCategory()
84-
{
85-
$this->cmsIndex->open();
86-
$this->cmsIndex->getTopmenu()->selectCategoryByName(
87-
$this->product->getCategoryIds()[0]
88-
);
8949
\PHPUnit_Framework_Assert::assertFalse(
90-
$this->catalogCategoryView->getListProductBlock()->checkAddToCardButton(),
50+
$catalogCategoryView->getListProductBlock()->getProductItem($product)->isVisibleAddToCardButton(),
9151
"Button 'Add to Card' is present on Category page"
9252
);
93-
}
9453

95-
/**
96-
* "Add to cart" button is not display on Product page
97-
*
98-
* @return void
99-
*/
100-
protected function addToCardAbsentOnProduct()
101-
{
102-
$this->cmsIndex->open();
103-
$this->cmsIndex->getTopmenu()->selectCategoryByName(
104-
$this->product->getCategoryIds()[0]
105-
);
106-
$this->catalogCategoryView->getListProductBlock()->openProductViewPage($this->product->getName());
54+
$catalogCategoryView->getListProductBlock()->openProductViewPage($product->getName());
10755
\PHPUnit_Framework_Assert::assertFalse(
108-
$this->catalogProductView->getViewBlock()->checkAddToCardButton(),
56+
$catalogProductView->getViewBlock()->isVisibleAddToCardButton(),
10957
"Button 'Add to Card' is present on Product page."
11058
);
11159
}
11260

11361
/**
114-
* Text absent button "Add to Cart" on the category/product pages
62+
* Text absent button "Add to Cart" on the category/product pages.
11563
*
11664
* @return string
11765
*/

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

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use Magento\Catalog\Test\Page\Product\CatalogProductView;
1212
use Magento\Cms\Test\Page\CmsIndex;
1313
use Magento\Mtf\Constraint\AbstractConstraint;
14+
use Magento\Mtf\Fixture\InjectableFixture;
1415

1516
/**
16-
* Class AssertAddToCartButtonPresent
17-
* Checks the button on the category/product pages
17+
* Checks the button on the category/product pages.
1818
*/
1919
class AssertAddToCartButtonPresent extends AbstractConstraint
2020
{
@@ -23,35 +23,7 @@ class AssertAddToCartButtonPresent extends AbstractConstraint
2323
/* end tags */
2424

2525
/**
26-
* Category Page on Frontend
27-
*
28-
* @var CatalogCategoryView
29-
*/
30-
protected $catalogCategoryView;
31-
32-
/**
33-
* Index Page
34-
*
35-
* @var CmsIndex
36-
*/
37-
protected $cmsIndex;
38-
39-
/**
40-
* Product simple fixture
41-
*
42-
* @var CatalogProductSimple
43-
*/
44-
protected $product;
45-
46-
/**
47-
* Product Page on Frontend
48-
*
49-
* @var CatalogProductView
50-
*/
51-
protected $catalogProductView;
52-
53-
/**
54-
* Assert that "Add to cart" button is present on page
26+
* Assert that "Add to cart" button is present on page.
5527
*
5628
* @param CmsIndex $cmsIndex
5729
* @param CatalogCategoryView $catalogCategoryView
@@ -66,52 +38,29 @@ public function processAssert(
6638
CatalogProductSimple $product,
6739
CatalogProductView $catalogProductView
6840
) {
69-
$this->catalogCategoryView = $catalogCategoryView;
70-
$this->cmsIndex = $cmsIndex;
71-
$this->product = $product;
72-
$this->catalogProductView = $catalogProductView;
41+
$cmsIndex->open();
42+
$cmsIndex->getTopmenu()->selectCategoryByName($product->getCategoryIds()[0]);
7343

74-
$this->addToCardPresentOnCategory();
75-
$this->addToCardPresentOnProduct();
76-
}
44+
$isProductVisible = $catalogCategoryView->getListProductBlock()->isProductVisible($product->getName());
45+
while (!$isProductVisible && $catalogCategoryView->getBottomToolbar()->nextPage()) {
46+
$isProductVisible = $catalogCategoryView->getListProductBlock()->isProductVisible($product->getName());
47+
}
48+
\PHPUnit_Framework_Assert::assertTrue($isProductVisible, 'Product is absent on category page.');
7749

78-
/**
79-
* "Add to cart" button is display on Category page
80-
*
81-
* @return void
82-
*/
83-
protected function addToCardPresentOnCategory()
84-
{
85-
$this->cmsIndex->open();
86-
$this->cmsIndex->getTopmenu()->selectCategoryByName(
87-
$this->product->getCategoryIds()[0]
88-
);
8950
\PHPUnit_Framework_Assert::assertTrue(
90-
$this->catalogCategoryView->getListProductBlock()->checkAddToCardButton(),
51+
$catalogCategoryView->getListProductBlock()->getProductItem($product)->isVisibleAddToCardButton(),
9152
"Button 'Add to Card' is absent on Category page."
9253
);
93-
}
9454

95-
/**
96-
* "Add to cart" button is display on Product page
97-
*
98-
* @return void
99-
*/
100-
protected function addToCardPresentOnProduct()
101-
{
102-
$this->cmsIndex->open();
103-
$this->cmsIndex->getTopmenu()->selectCategoryByName(
104-
$this->product->getCategoryIds()[0]
105-
);
106-
$this->catalogCategoryView->getListProductBlock()->openProductViewPage($this->product->getName());
55+
$catalogCategoryView->getListProductBlock()->openProductViewPage($product->getName());
10756
\PHPUnit_Framework_Assert::assertTrue(
108-
$this->catalogProductView->getViewBlock()->checkAddToCardButton(),
57+
$catalogProductView->getViewBlock()->isVisibleAddToCardButton(),
10958
"Button 'Add to Card' is absent on Product page."
11059
);
11160
}
11261

11362
/**
114-
* Text present button "Add to Cart" on the category/product pages
63+
* Text present button "Add to Cart" on the category/product pages.
11564
*
11665
* @return string
11766
*/

0 commit comments

Comments
 (0)